Skip to content

Commit 6d14cd5

Browse files
committed
Update README
1 parent 3b6b0db commit 6d14cd5

File tree

1 file changed

+29
-87
lines changed

1 file changed

+29
-87
lines changed

README.md

Lines changed: 29 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -63,101 +63,49 @@ pip install git+https://github.com/OpenCSGs/coagent.git
6363

6464
## Quick Start
6565

66-
### Simple Agent
67-
68-
Create a Ping-pong agent:
69-
70-
```python
71-
# server.py
72-
73-
import asyncio
74-
75-
from coagent.core import (
76-
BaseAgent,
77-
Context,
78-
handler,
79-
idle_loop,
80-
Message,
81-
new,
82-
)
83-
from coagent.runtimes import NATSRuntime
84-
85-
86-
class Ping(Message):
87-
pass
88-
89-
90-
class Pong(Message):
91-
pass
92-
93-
94-
class Server(BaseAgent):
95-
"""The Ping-pong agent."""
96-
97-
@handler
98-
async def handle(self, msg: Ping, ctx: Context) -> Pong:
99-
"""Handle the Ping message and return a Pong message."""
100-
return Pong()
101-
102-
103-
async def main():
104-
async with NATSRuntime.from_servers("nats://localhost:4222") as runtime:
105-
await runtime.register("server", new(Server))
106-
await idle_loop()
107-
108-
109-
if __name__ == "__main__":
110-
asyncio.run(main())
111-
```
112-
113-
Run the agent:
114-
115-
```bash
116-
python server.py
117-
```
118-
119-
Communicate with the agent:
120-
121-
```bash
122-
coagent server -H type:Ping
123-
```
124-
125-
### Chat Agent (LLM-based)
126-
127-
Create a Translator agent:
128-
12966
```python
13067
# translator.py
13168

13269
import asyncio
13370
import os
13471

135-
from coagent.agents import ChatAgent, ModelClient
136-
from coagent.core import idle_loop, new, set_stderr_logger
137-
from coagent.runtimes import NATSRuntime
138-
139-
140-
class Translator(ChatAgent):
141-
"""The translator agent."""
72+
from coagent.agents import ChatAgent, ChatHistory, ChatMessage, ModelClient
73+
from coagent.core import AgentSpec, new, set_stderr_logger
74+
from coagent.runtimes import LocalRuntime
14275

143-
system = "You are a professional translator that can translate Chinese to English."
76+
client = ModelClient(
77+
model=os.getenv("MODEL_NAME"),
78+
api_base=os.getenv("MODEL_API_BASE"),
79+
api_version=os.getenv("MODEL_API_VERSION"),
80+
api_key=os.getenv("MODEL_API_KEY"),
81+
)
14482

145-
client = ModelClient(
146-
model=os.getenv("MODEL_NAME"),
147-
api_base=os.getenv("MODEL_API_BASE"),
148-
api_version=os.getenv("MODEL_API_VERSION"),
149-
api_key=os.getenv("MODEL_API_KEY"),
150-
)
83+
translator = AgentSpec(
84+
"translator",
85+
new(
86+
ChatAgent,
87+
system="You are a professional translator that can translate Chinese to English.",
88+
client=client,
89+
),
90+
)
15191

15292

15393
async def main():
154-
async with NATSRuntime.from_servers("nats://localhost:4222") as runtime:
155-
await runtime.register("translator", new(Translator))
156-
await idle_loop()
94+
async with LocalRuntime() as runtime:
95+
await runtime.register_spec(translator)
96+
97+
result = translator.run_stream(
98+
ChatHistory(
99+
messages=[ChatMessage(role="user", content="你好,世界")]
100+
).encode()
101+
)
102+
async for chunk in result:
103+
msg = ChatMessage.decode(chunk)
104+
print(msg.content, end="")
157105

158106

159107
if __name__ == "__main__":
160-
set_stderr_logger("TRACE")
108+
set_stderr_logger("ERROR")
161109
asyncio.run(main())
162110
```
163111

@@ -171,12 +119,6 @@ export MODEL_API_KEY=<YOUR_MODEL_API_KEY>
171119
python translator.py
172120
```
173121

174-
Communicate with the agent:
175-
176-
```bash
177-
coagent translator -H type:ChatHistory -d '{"messages":[{"role":"user","content":"你好"}]}' --chat
178-
```
179-
180122

181123
## Examples
182124

0 commit comments

Comments
 (0)