初始化提交
Some checks failed
CI / Check / macos-latest (push) Has been cancelled
CI / Check / ubuntu-latest (push) Has been cancelled
CI / Check / windows-latest (push) Has been cancelled
CI / Test / macos-latest (push) Has been cancelled
CI / Test / ubuntu-latest (push) Has been cancelled
CI / Test / windows-latest (push) Has been cancelled
CI / Clippy (push) Has been cancelled
CI / Format (push) Has been cancelled
CI / Security Audit (push) Has been cancelled
CI / Secrets Scan (push) Has been cancelled
CI / Install Script Smoke Test (push) Has been cancelled
Some checks failed
CI / Check / macos-latest (push) Has been cancelled
CI / Check / ubuntu-latest (push) Has been cancelled
CI / Check / windows-latest (push) Has been cancelled
CI / Test / macos-latest (push) Has been cancelled
CI / Test / ubuntu-latest (push) Has been cancelled
CI / Test / windows-latest (push) Has been cancelled
CI / Clippy (push) Has been cancelled
CI / Format (push) Has been cancelled
CI / Security Audit (push) Has been cancelled
CI / Secrets Scan (push) Has been cancelled
CI / Install Script Smoke Test (push) Has been cancelled
This commit is contained in:
35
sdk/python/examples/client_basic.py
Normal file
35
sdk/python/examples/client_basic.py
Normal file
@@ -0,0 +1,35 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Basic example — create an agent and chat with it via the REST API.
|
||||
|
||||
Usage:
|
||||
python client_basic.py
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
|
||||
from openfang_client import OpenFang
|
||||
|
||||
client = OpenFang("http://localhost:3000")
|
||||
|
||||
# Check server health
|
||||
health = client.health()
|
||||
print("Server:", health)
|
||||
|
||||
# List existing agents
|
||||
agents = client.agents.list()
|
||||
print(f"Agents: {len(agents)}")
|
||||
|
||||
# Create a new agent from the "assistant" template
|
||||
agent = client.agents.create(template="assistant")
|
||||
print(f"Created agent: {agent['id']}")
|
||||
|
||||
# Send a message and get the full response
|
||||
reply = client.agents.message(agent["id"], "What can you help me with?")
|
||||
print(f"Reply: {reply}")
|
||||
|
||||
# Clean up
|
||||
client.agents.delete(agent["id"])
|
||||
print("Agent deleted.")
|
||||
33
sdk/python/examples/client_streaming.py
Normal file
33
sdk/python/examples/client_streaming.py
Normal file
@@ -0,0 +1,33 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Streaming example — stream agent responses token by token.
|
||||
|
||||
Usage:
|
||||
python client_streaming.py
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
|
||||
from openfang_client import OpenFang
|
||||
|
||||
client = OpenFang("http://localhost:3000")
|
||||
|
||||
# Create an agent
|
||||
agent = client.agents.create(template="assistant")
|
||||
print(f"Agent: {agent['id']}")
|
||||
|
||||
# Stream the response
|
||||
print("\n--- Streaming response ---")
|
||||
for event in client.agents.stream(agent["id"], "Tell me a short story about a robot."):
|
||||
event_type = event.get("type", "")
|
||||
if event_type == "text_delta" and event.get("delta"):
|
||||
print(event["delta"], end="", flush=True)
|
||||
elif event_type == "tool_call":
|
||||
print(f"\n[Tool call: {event.get('tool')}]")
|
||||
elif event_type == "done":
|
||||
print("\n--- Done ---")
|
||||
|
||||
# Clean up
|
||||
client.agents.delete(agent["id"])
|
||||
20
sdk/python/examples/echo_agent.py
Normal file
20
sdk/python/examples/echo_agent.py
Normal file
@@ -0,0 +1,20 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Example OpenFang agent: echoes back messages with a friendly greeting."""
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add parent directory to path for openfang_sdk import
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
|
||||
from openfang_sdk import Agent
|
||||
|
||||
agent = Agent()
|
||||
|
||||
|
||||
@agent.on_message
|
||||
def handle(message: str, context: dict) -> str:
|
||||
agent_id = context.get("agent_id", os.environ.get("OPENFANG_AGENT_ID", "unknown"))
|
||||
return f"Hello from Python agent {agent_id}! You said: {message}"
|
||||
|
||||
|
||||
agent.run()
|
||||
Reference in New Issue
Block a user