@@ -85,62 +85,125 @@ Here's a basic example of how to use the LLM Assistant Framework:
85
85
86
86
``` python
87
87
import asyncio
88
- from assinstants.core.assistant_manager import AssistantManager
89
- from assinstants.core.thread_manager import ThreadManager
90
- from assinstants.core.run_manager import RunManager
91
- from assinstants.models.function import FunctionDefinition, FunctionParameter
92
-
93
- # Define a custom LLM function (replace with your actual implementation)
94
- async def custom_llm_function (model : str , prompt : str , ** kwargs ):
95
- # Implement your LLM API call here
96
- return " LLM response"
97
-
98
- # Initialize managers
99
- assistant_manager = AssistantManager()
100
- thread_manager = ThreadManager()
101
- run_manager = RunManager(assistant_manager, thread_manager)
102
-
103
- # Define a tool
104
- def calculate_sum (a : float , b : float ) -> float :
105
- return a + b
106
-
107
- sum_function = FunctionDefinition(
108
- name = " calculate_sum" ,
109
- description = " Calculate the sum of two numbers" ,
110
- parameters = {
111
- " a" : FunctionParameter(type = " number" , description = " First number" ),
112
- " b" : FunctionParameter(type = " number" , description = " Second number" )
113
- },
114
- implementation = calculate_sum
88
+ import aiohttp
89
+ from assinstants import AssistantManager, ThreadManager, RunManager
90
+ from assinstants.models.function import (
91
+ FunctionDefinition,
92
+ FunctionParameter,
115
93
)
94
+ from assinstants.models.tool import Tool, FunctionTool
95
+ from assinstants.utils.exceptions import (
96
+ RunExecutionError,
97
+ FunctionNotFoundError,
98
+ FunctionExecutionError,
99
+ )
100
+ import logging
101
+
102
+ logging.basicConfig(level = logging.DEBUG )
103
+
104
+ OPENWEATHERMAP_API_KEY = " "
105
+
106
+
107
+ async def custom_llm_function (model : str , prompt : str , ** kwargs ) -> str :
108
+ async with aiohttp.ClientSession() as session:
109
+ try :
110
+ async with session.post(
111
+ " http://localhost:11434/api/generate" ,
112
+ json = {" model" : model, " prompt" : prompt, " stream" : False , ** kwargs},
113
+ timeout = aiohttp.ClientTimeout(total = 30 ),
114
+ ) as response:
115
+ result = await response.json()
116
+ return result.get(" response" , " " )
117
+ except asyncio.TimeoutError:
118
+ return " LLM request timed out"
119
+ except Exception as e:
120
+ return f " Error: { str (e)} "
121
+
122
+
123
+ async def get_weather (city : str , country_code : str ) -> dict :
124
+ url = f " http://api.openweathermap.org/data/2.5/weather?q= { city} , { country_code} &appid= { OPENWEATHERMAP_API_KEY } &units=metric "
125
+ async with aiohttp.ClientSession() as session:
126
+ async with session.get(url) as response:
127
+ data = await response.json()
128
+ print (f " Weather data: { data} " )
129
+ if response.status == 200 :
130
+ logging.debug(f " Weather data: { data} " )
131
+ return {
132
+ " temperature" : data[" main" ][" temp" ],
133
+ " description" : data[" weather" ][0 ][" description" ],
134
+ " humidity" : data[" main" ][" humidity" ],
135
+ " wind_speed" : data[" wind" ][" speed" ],
136
+ }
137
+ else :
138
+ raise Exception (
139
+ f " Error fetching weather data for { city} , { country_code} : { data.get(' message' , ' Unknown error' )} "
140
+ )
141
+
116
142
117
143
async def main ():
118
- # Create an assistant
144
+ assistant_manager = AssistantManager()
145
+ thread_manager = ThreadManager()
146
+ run_manager = RunManager(assistant_manager, thread_manager)
147
+
148
+ tools = [
149
+ Tool(
150
+ tool = FunctionTool(
151
+ function = FunctionDefinition(
152
+ name = " get_weather" ,
153
+ description = " Get current weather for a city" ,
154
+ parameters = {
155
+ " city" : FunctionParameter(
156
+ type = " string" , description = " City name"
157
+ ),
158
+ " country_code" : FunctionParameter(
159
+ type = " string" , description = " Two-letter country code"
160
+ ),
161
+ },
162
+ implementation = get_weather,
163
+ )
164
+ )
165
+ )
166
+ ]
167
+
119
168
assistant = await assistant_manager.create_assistant(
120
- name = " Math Tutor " ,
121
- instructions = " You are a helpful math tutor. " ,
122
- model = " llama3.1 " ,
169
+ name = " Weather Assistant " ,
170
+ instructions = """ You are a weather assistant. "" " ,
171
+ model = " llama3" ,
123
172
custom_llm_function = custom_llm_function,
124
- tools = [sum_function]
173
+ tools = tools,
174
+ temperature = 0.7 ,
125
175
)
126
176
127
- # Create a thread
128
177
thread = await thread_manager.create_thread()
129
-
130
- # Add the assistant to the thread
131
178
await thread_manager.add_assistant_to_thread(thread.id, assistant)
132
179
133
- # Add a message to the thread
134
- await thread_manager.add_message(thread.id, " user" , " What's 2 + 2?" )
135
-
136
- # Run the assistant
137
- run = await run_manager.create_and_execute_run(thread.id)
138
-
139
- # Get the assistant's response
140
- messages = await thread_manager.get_messages(thread.id)
141
- print (messages[- 1 ].content)
142
-
143
- asyncio.run(main())
180
+ print (" Welcome to the Weather Assistant! Type 'exit' to end the conversation." )
181
+
182
+ while True :
183
+ user_query = input (" You: " )
184
+ if user_query.lower() == " exit" :
185
+ print (" Thank you for using the Weather Assistant. Goodbye!" )
186
+ break
187
+
188
+ await thread_manager.add_message(thread.id, " user" , user_query)
189
+
190
+ try :
191
+ run = await run_manager.create_and_execute_run(thread.id)
192
+ messages = await thread_manager.get_messages(thread.id)
193
+ final_answer = messages[- 1 ].content if messages else " No response generated"
194
+ print (f " Assistant: { final_answer} " )
195
+ except RunExecutionError as e:
196
+ print (f " Run execution error: { str (e)} " )
197
+ except FunctionNotFoundError as e:
198
+ print (f " Function not found error: { str (e)} " )
199
+ except FunctionExecutionError as e:
200
+ print (f " Function execution error: { str (e)} " )
201
+ except Exception as e:
202
+ print (f " An error occurred: { str (e)} " )
203
+
204
+
205
+ if __name__ == " __main__" :
206
+ asyncio.run(main())
144
207
```
145
208
146
209
## Core Components
0 commit comments