dm-aioaiagent 0.1.3__tar.gz → 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: dm-aioaiagent
3
- Version: 0.1.3
3
+ Version: 0.2.0
4
4
  Summary: This is my custom aioaiagent client
5
5
  Home-page: https://pypi.org/project/dm-aioaiagent
6
6
  Author: dimka4621
@@ -43,7 +43,7 @@ async def main():
43
43
  # define a system message
44
44
  system_message = "Your custom system message with role, backstory and goal"
45
45
 
46
- # define a list of tools, if you want to use them
46
+ # (optional) define a list of tools, if you want to use them
47
47
  tools = [...]
48
48
 
49
49
  # define a openai model, default is "gpt-4o-mini"
@@ -51,7 +51,39 @@ async def main():
51
51
 
52
52
  # create an agent
53
53
  ai_agent = DMAioAIAgent(system_message, tools, model=model_name)
54
- # you can set input_output_logging=False, if you don't want to see the input and output messages from agent
54
+ # if you don't want to see the input and output messages from agent
55
+ # you can set input_output_logging=False
56
+
57
+ # define the conversation messages
58
+ messages = [
59
+ {"role": "user", "content": "Hello!"},
60
+ {"role": "ai", "content": "How can I help you?"},
61
+ {"role": "user", "content": "I want to know the weather in Kyiv"},
62
+ ]
63
+
64
+ # call an agent
65
+ answer = await ai_agent.run(messages)
66
+
67
+
68
+ if __name__ == "__main__":
69
+ asyncio.run(main())
70
+ ```
71
+
72
+ ### Return context of the tools with answer
73
+ ```python
74
+ import asyncio
75
+ from dm_aioaiagent import DMAioAIAgent
76
+
77
+
78
+ async def main():
79
+ # define a system message
80
+ system_message = "Your custom system message with role, backstory and goal"
81
+
82
+ # define a list of tools, if you want to use them
83
+ tools = [...]
84
+
85
+ # create an agent
86
+ ai_agent = DMAioAIAgent(system_message, tools, return_context=True)
55
87
 
56
88
  # define the conversation messages
57
89
  messages = [
@@ -65,7 +97,7 @@ async def main():
65
97
 
66
98
  # if you define tools, you can see the context of the tools
67
99
  answer = state["answer"]
68
- print(state["context"])
100
+ context = state["context"]
69
101
 
70
102
 
71
103
  if __name__ == "__main__":
@@ -95,7 +127,7 @@ class MyLogger:
95
127
  print(message)
96
128
 
97
129
 
98
- # create agent
130
+ # create an agent
99
131
  ai_agent = DMAioAIAgent()
100
132
 
101
133
  # set up custom logger for this agent
@@ -20,7 +20,7 @@ async def main():
20
20
  # define a system message
21
21
  system_message = "Your custom system message with role, backstory and goal"
22
22
 
23
- # define a list of tools, if you want to use them
23
+ # (optional) define a list of tools, if you want to use them
24
24
  tools = [...]
25
25
 
26
26
  # define a openai model, default is "gpt-4o-mini"
@@ -28,7 +28,39 @@ async def main():
28
28
 
29
29
  # create an agent
30
30
  ai_agent = DMAioAIAgent(system_message, tools, model=model_name)
31
- # you can set input_output_logging=False, if you don't want to see the input and output messages from agent
31
+ # if you don't want to see the input and output messages from agent
32
+ # you can set input_output_logging=False
33
+
34
+ # define the conversation messages
35
+ messages = [
36
+ {"role": "user", "content": "Hello!"},
37
+ {"role": "ai", "content": "How can I help you?"},
38
+ {"role": "user", "content": "I want to know the weather in Kyiv"},
39
+ ]
40
+
41
+ # call an agent
42
+ answer = await ai_agent.run(messages)
43
+
44
+
45
+ if __name__ == "__main__":
46
+ asyncio.run(main())
47
+ ```
48
+
49
+ ### Return context of the tools with answer
50
+ ```python
51
+ import asyncio
52
+ from dm_aioaiagent import DMAioAIAgent
53
+
54
+
55
+ async def main():
56
+ # define a system message
57
+ system_message = "Your custom system message with role, backstory and goal"
58
+
59
+ # define a list of tools, if you want to use them
60
+ tools = [...]
61
+
62
+ # create an agent
63
+ ai_agent = DMAioAIAgent(system_message, tools, return_context=True)
32
64
 
33
65
  # define the conversation messages
34
66
  messages = [
@@ -42,7 +74,7 @@ async def main():
42
74
 
43
75
  # if you define tools, you can see the context of the tools
44
76
  answer = state["answer"]
45
- print(state["context"])
77
+ context = state["context"]
46
78
 
47
79
 
48
80
  if __name__ == "__main__":
@@ -72,7 +104,7 @@ class MyLogger:
72
104
  print(message)
73
105
 
74
106
 
75
- # create agent
107
+ # create an agent
76
108
  ai_agent = DMAioAIAgent()
77
109
 
78
110
  # set up custom logger for this agent
@@ -1,6 +1,6 @@
1
1
  import json
2
2
  import os
3
- from typing import Optional, Literal
3
+ from typing import Optional, Literal, Union
4
4
  from typing_extensions import TypedDict
5
5
  from pydantic import BaseModel, Field
6
6
  from threading import Thread
@@ -46,13 +46,15 @@ class DMAIAgent:
46
46
  model: str = "gpt-4o-mini",
47
47
  temperature: int = 1,
48
48
  agent_name: str = None,
49
- input_output_logging: bool = True
49
+ input_output_logging: bool = True,
50
+ return_context: bool = False
50
51
  ):
51
52
  if not os.getenv("OPENAI_API_KEY"):
52
53
  raise EnvironmentError("OPENAI_API_KEY environment variable is not set!")
53
54
 
54
55
  self._logger = DMLogger(agent_name or self.agent_name)
55
56
  self._input_output_logging = input_output_logging
57
+ self._return_context = return_context
56
58
  self._is_tools_exists = bool(tools)
57
59
 
58
60
  prompt = ChatPromptTemplate.from_messages([SystemMessage(content=system_message),
@@ -78,8 +80,11 @@ class DMAIAgent:
78
80
  workflow.set_finish_point("Exit")
79
81
  self._graph = workflow.compile()
80
82
 
81
- def run(self, messages: list[Message]) -> OutputState:
82
- return self._graph.invoke({"messages": messages})
83
+ def run(self, messages: list[Message]) -> Union[str, OutputState]:
84
+ state = self._graph.invoke({"messages": messages})
85
+ if self._return_context:
86
+ return state
87
+ return state["answer"]
83
88
 
84
89
  def _prepare_messages_node(self, state: InputState) -> InputState:
85
90
  state.messages = state.messages or [{"role": "user", "content": "Привіт"}]
@@ -1,21 +1,25 @@
1
1
  import json
2
2
  import sys
3
3
  import asyncio
4
+ from typing import Union
4
5
  from langchain_core.messages import ToolMessage
5
6
 
6
- from .ai_agent import DMAIAgent, InputState, OutputState
7
-
8
- __all__ = ["DMAioAIAgent"]
7
+ from .ai_agent import DMAIAgent, InputState, OutputState, Message
9
8
 
10
9
  if sys.platform == "win32":
11
10
  asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
12
11
 
12
+ __all__ = ["DMAioAIAgent"]
13
+
13
14
 
14
15
  class DMAioAIAgent(DMAIAgent):
15
16
  agent_name = "AsyncAIAgent"
16
17
 
17
- async def run(self, messages: list[dict[str, str]]) -> OutputState:
18
- return await self._graph.ainvoke({"messages": messages})
18
+ async def run(self, messages: list[Message]) -> Union[str, OutputState]:
19
+ state = await self._graph.ainvoke({"messages": messages})
20
+ if self._return_context:
21
+ return state
22
+ return state["answer"]
19
23
 
20
24
  async def _invoke_llm_node(self, state: InputState) -> InputState:
21
25
  self._logger.debug("Run node: Invoke LLM")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: dm-aioaiagent
3
- Version: 0.1.3
3
+ Version: 0.2.0
4
4
  Summary: This is my custom aioaiagent client
5
5
  Home-page: https://pypi.org/project/dm-aioaiagent
6
6
  Author: dimka4621
@@ -43,7 +43,7 @@ async def main():
43
43
  # define a system message
44
44
  system_message = "Your custom system message with role, backstory and goal"
45
45
 
46
- # define a list of tools, if you want to use them
46
+ # (optional) define a list of tools, if you want to use them
47
47
  tools = [...]
48
48
 
49
49
  # define a openai model, default is "gpt-4o-mini"
@@ -51,7 +51,39 @@ async def main():
51
51
 
52
52
  # create an agent
53
53
  ai_agent = DMAioAIAgent(system_message, tools, model=model_name)
54
- # you can set input_output_logging=False, if you don't want to see the input and output messages from agent
54
+ # if you don't want to see the input and output messages from agent
55
+ # you can set input_output_logging=False
56
+
57
+ # define the conversation messages
58
+ messages = [
59
+ {"role": "user", "content": "Hello!"},
60
+ {"role": "ai", "content": "How can I help you?"},
61
+ {"role": "user", "content": "I want to know the weather in Kyiv"},
62
+ ]
63
+
64
+ # call an agent
65
+ answer = await ai_agent.run(messages)
66
+
67
+
68
+ if __name__ == "__main__":
69
+ asyncio.run(main())
70
+ ```
71
+
72
+ ### Return context of the tools with answer
73
+ ```python
74
+ import asyncio
75
+ from dm_aioaiagent import DMAioAIAgent
76
+
77
+
78
+ async def main():
79
+ # define a system message
80
+ system_message = "Your custom system message with role, backstory and goal"
81
+
82
+ # define a list of tools, if you want to use them
83
+ tools = [...]
84
+
85
+ # create an agent
86
+ ai_agent = DMAioAIAgent(system_message, tools, return_context=True)
55
87
 
56
88
  # define the conversation messages
57
89
  messages = [
@@ -65,7 +97,7 @@ async def main():
65
97
 
66
98
  # if you define tools, you can see the context of the tools
67
99
  answer = state["answer"]
68
- print(state["context"])
100
+ context = state["context"]
69
101
 
70
102
 
71
103
  if __name__ == "__main__":
@@ -95,7 +127,7 @@ class MyLogger:
95
127
  print(message)
96
128
 
97
129
 
98
- # create agent
130
+ # create an agent
99
131
  ai_agent = DMAioAIAgent()
100
132
 
101
133
  # set up custom logger for this agent
@@ -8,7 +8,7 @@ def readme():
8
8
 
9
9
  setup(
10
10
  name='dm-aioaiagent',
11
- version='v0.1.3',
11
+ version='v0.2.0',
12
12
  author='dimka4621',
13
13
  author_email='mismartconfig@gmail.com',
14
14
  description='This is my custom aioaiagent client',
File without changes