looping chatbot with ephemeral history
This commit is contained in:
+23
-7
@@ -3,19 +3,35 @@ import asyncio
|
||||
from anthropic import AsyncAnthropic
|
||||
|
||||
from agent.config import settings
|
||||
from agent.history import ConversationHistory
|
||||
|
||||
client = AsyncAnthropic(api_key=settings.anthropic_api_key)
|
||||
history = ConversationHistory()
|
||||
|
||||
|
||||
async def run_turn(user_message: str) -> str:
|
||||
async def run_turn(user_message: str, history: list[dict] = None) -> str:
|
||||
|
||||
if history is None:
|
||||
history = []
|
||||
|
||||
# add the new user message to history
|
||||
messages = history + [{"role": "user", "content": user_message}]
|
||||
|
||||
message = await client.messages.create(
|
||||
model=settings.model,
|
||||
max_tokens=settings.max_tokens,
|
||||
messages=[
|
||||
{
|
||||
"role": "user",
|
||||
"content": user_message,
|
||||
}
|
||||
],
|
||||
messages=messages,
|
||||
)
|
||||
|
||||
return message
|
||||
|
||||
|
||||
async def run_session():
|
||||
while True:
|
||||
user_input = input("You: ")
|
||||
history.add_message("user", user_input)
|
||||
|
||||
response = await run_turn(user_input, history.get_all())
|
||||
history.add_message("assistant", response.content[0].text)
|
||||
|
||||
print(f"Assistant: {response.content[0].text}")
|
||||
|
||||
Reference in New Issue
Block a user