Skip to content

Commit 0487b65

Browse files
committed
docs(README): Update example code with weather assistant implementation
- Add comprehensive example of using AssistantManager, ThreadManager, and RunManager - Include custom LLM function implementation using local API - Demonstrate creation of weather assistant with get_weather function - Show interactive conversation loop with error handling
1 parent 911a984 commit 0487b65

File tree

1 file changed

+109
-46
lines changed

1 file changed

+109
-46
lines changed

README.md

Lines changed: 109 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -85,62 +85,125 @@ Here's a basic example of how to use the LLM Assistant Framework:
8585

8686
```python
8787
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,
11593
)
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+
116142

117143
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+
119168
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",
123172
custom_llm_function=custom_llm_function,
124-
tools=[sum_function]
173+
tools=tools,
174+
temperature=0.7,
125175
)
126176

127-
# Create a thread
128177
thread = await thread_manager.create_thread()
129-
130-
# Add the assistant to the thread
131178
await thread_manager.add_assistant_to_thread(thread.id, assistant)
132179

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())
144207
```
145208

146209
## Core Components

0 commit comments

Comments
 (0)