-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbasic_usage.py
More file actions
53 lines (40 loc) · 1.58 KB
/
basic_usage.py
File metadata and controls
53 lines (40 loc) · 1.58 KB
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
47
48
49
50
51
52
53
"""Basic usage: chat, streaming, async."""
import asyncio
from miiflow_agent import LLMClient
from miiflow_agent.core import Message
def sync_chat():
client = LLMClient.create("openai", model="gpt-4o-mini")
response = client.chat([Message.user("What is Python?")])
print(f"Response: {response.message.content[:80]}...")
print(f"Tokens: {response.usage.total_tokens}\n")
def sync_streaming():
client = LLMClient.create("openai", model="gpt-4o-mini")
print("Streaming: ", end="")
for chunk in client.stream_chat([Message.user("Count 1 to 5")]):
print(chunk.delta, end="", flush=True)
print("\n")
async def async_chat():
client = LLMClient.create("openai", model="gpt-4o-mini")
response = await client.achat([Message.user("What is async/await?")])
print(f"Async response: {response.message.content[:80]}...\n")
async def async_streaming():
client = LLMClient.create("openai", model="gpt-4o-mini")
print("Async streaming: ", end="")
async for chunk in client.astream_chat([Message.user("Say hello")]):
print(chunk.delta, end="", flush=True)
print("\n")
def conversation_history():
client = LLMClient.create("openai", model="gpt-4o-mini")
messages = [
Message.user("My name is Alice"),
Message.assistant("Hello Alice!"),
Message.user("What's my name?")
]
response = client.chat(messages)
print(f"Conversation: {response.message.content}")
if __name__ == "__main__":
sync_chat()
sync_streaming()
conversation_history()
asyncio.run(async_chat())
asyncio.run(async_streaming())