looping chatbot with ephemeral history

This commit is contained in:
2026-02-12 22:29:12 -07:00
parent bc7c5009f9
commit f8a68b9d02
4 changed files with 71 additions and 13 deletions
+23 -7
View File
@@ -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}")