-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaugmented_llm.py
46 lines (35 loc) · 1.25 KB
/
augmented_llm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import asyncio
import os
from coagent.agents import ChatAgent, ModelClient, tool
from coagent.agents.messages import ChatMessage
from coagent.core import AgentSpec, new, set_stderr_logger
from coagent.runtimes import LocalRuntime
class Assistant(ChatAgent):
system = """You are an agent who can use tools."""
client = ModelClient(
model=os.getenv("MODEL_NAME"),
api_base=os.getenv("MODEL_API_BASE"),
api_version=os.getenv("MODEL_API_VERSION"),
api_key=os.getenv("MODEL_API_KEY"),
)
@tool
async def query_weather(self, city: str) -> str:
"""Query the weather in the given city."""
return f"The weather in {city} is sunny."
assistant = AgentSpec("assistant", new(Assistant))
async def main():
async with LocalRuntime() as runtime:
await runtime.register(assistant)
result = await assistant.run(
ChatMessage(
role="user",
content="What's the weather like in Beijing?",
).encode(),
stream=True,
)
async for chunk in result:
msg = ChatMessage.decode(chunk)
print(msg.content, end="", flush=True)
if __name__ == "__main__":
set_stderr_logger("TRACE")
asyncio.run(main())