Skip to content

7.1.2 - RavenDB AI Agents in Python 🎉

Latest
Compare
Choose a tag to compare
@poissoncorp poissoncorp released this 27 Aug 15:10
4e383d9

Starting v7.1.2, the RavenDB Python client ships with the AI Agents API. This way you can spin up safe, tool-driven agents right from your Python app.

If you haven’t seen what our Agents are or why we built them, grab 2 minutes and read the feature announcement (it’s genuinely good):

👉 AI Agents Feature Announcement: ravendb/ravendb#21249
👉 AI Agents Documentation: https://docs.ravendb.net/ai-integration/ai-agents/ai-agents-api


Usage example (create → chat → handle actions)

from ravendb import DocumentStore, AiAgentConfiguration, AiAgentToolAction

# 1) Connect
store = DocumentStore(["http://localhost:8080"], "AwesomeDatabase")
store.initialize()

# 2) Define an agent (connection string configured in RavenDB Studio)
agent = AiAgentConfiguration(
    name="orders-manager-test",
    connection_string_name="OpenAI",
    system_prompt="You help users with their orders and can email results."
)
agent.sample_object = '{"answer":"text"}'

# Optional: let the agent request supervised actions
send_email = AiAgentToolAction(
    name="SendEmail",
    description="Send an email with provided content to the given address."
)
send_email.parameters_sample_object = '{"Address":"user@example.com","EmailContent":"text"}'
agent.actions.append(send_email)

agent_id = store.ai.add_or_update_agent(agent).identifier

# 3) Run a conversation
with store.ai.conversation(agent_id) as chat:
    chat.set_user_prompt("Send message 'Raven on Python has agents now lmao' to josh@example.com.")
    result = chat.run()

    # 4) If the agent requested an action, handle it and reply with the result
    if result.action_requests:
        for action in result.action_requests:
            if action.name == "SendEmail":
                # ...your app actually sends the email here...
                chat.add_action_response(action.tool_id, "email sent ✅")

        # Continue the conversation after fulfilling actions
        result = chat.run()

    print("Agent says:", result.response)

Which results in:

Agent says: {'answer': 'The message has been sent to josh@example.com!'}

That’s the core loop. The API also supports parameters, query tools, persistence, and chat-trimming see the README examples:
https://github.com/ravendb/ravendb-python-client/blob/v7.1/AI_AGENT_README.md

Enjoy, happy coding! 🤖💙