Files
secure-agent/agent/loop.py
T

38 lines
946 B
Python

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, 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=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}")