Quick Start
Copy
acontext create my-openai-project --template-path "python/openai-basic"
Install CLI first:
curl -fsSL https://install.acontext.io | shManual Setup
Run agent with Acontext
Complete example
Complete example
Copy
import os
import json
from openai import OpenAI
from acontext import AcontextClient
openai_client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
client = AcontextClient(
api_key=os.getenv("ACONTEXT_API_KEY"),
)
# If you're using self-hosted Acontext:
# client = AcontextClient(
# base_url="http://localhost:8029/api/v1",
# api_key="sk-ac-your-root-api-bearer-token",
# )
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Returns weather info for the specified city.",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
},
},
},
]
def get_weather(city: str) -> str:
return f"The weather in {city} is sunny"
def execute_tool(name: str, args: dict) -> str:
if name == "get_weather":
return get_weather(**args)
return f"Unknown tool: {name}"
def run_agent(conversation: list) -> tuple[str, list]:
messages = list(conversation)
new_messages = []
while True:
response = openai_client.chat.completions.create(
model="gpt-4o-mini",
messages=messages,
tools=tools,
tool_choice="auto",
)
message = response.choices[0].message
msg_dict = {"role": message.role, "content": message.content}
if message.tool_calls:
msg_dict["tool_calls"] = [
{"id": tc.id, "type": "function", "function": {"name": tc.function.name, "arguments": tc.function.arguments}}
for tc in message.tool_calls
]
messages.append(msg_dict)
new_messages.append(msg_dict)
for tc in message.tool_calls:
result = execute_tool(tc.function.name, json.loads(tc.function.arguments))
tool_msg = {"role": "tool", "tool_call_id": tc.id, "content": result}
messages.append(tool_msg)
new_messages.append(tool_msg)
else:
new_messages.append(msg_dict)
return message.content, new_messages
# Create session
session = client.sessions.create()
conversation = []
# User message
user_msg = {"role": "user", "content": "What's the weather in Helsinki?"}
conversation.append(user_msg)
client.sessions.store_message(session_id=session.id, blob=user_msg)
# Run agent
response_content, new_messages = run_agent(conversation)
# Store all messages
for msg in new_messages:
conversation.append(msg)
client.sessions.store_message(session_id=session.id, blob=msg)
# Extract tasks
client.sessions.flush(session.id)
tasks = client.sessions.get_tasks(session.id)
for task in tasks.items:
print(f"Task: {task.data.task_description} | Status: {task.status}")
Resume Sessions
Copy
messages = client.sessions.get_messages(session_id)
conversation = messages.items
conversation.append({"role": "user", "content": "Summarize our conversation"})
response = openai_client.chat.completions.create(model="gpt-4o-mini", messages=conversation)