-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
82 lines (70 loc) · 2.13 KB
/
test.py
File metadata and controls
82 lines (70 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from pydantic_ai.messages import (
ModelMessage,
ModelRequest,
ModelResponse,
SystemPromptPart,
UserPromptPart,
TextPart,
ToolCallPart,
ToolReturnPart,
RetryPromptPart,
ModelMessagesTypeAdapter
)
import json
import logfire
from supabase import Client, create_client
from openai import AsyncOpenAI
from pydantic_ai_expert import pydantic_ai_expert, PydanticAIDeps
# Load environment variables
from dotenv import load_dotenv
load_dotenv()
openai_api_key = os.getenv("OPENAI_API_KEY")
db_password = os.getenv("DB_PASSWORD")
supabase_url = os.getenv("SUPABASE_URL")
supabase_secret = os.getenv("SUPABASE_SECRET")
openai_client = AsyncOpenAI(api_key=openai_api_key)
supabase: Client = create_client(
supabase_url,
supabase_secret
)
import asyncio
from typing import Any, List, Optional
# Assumes `pydantic_ai_expert` is your Agent instance (e.g., PydanticAI Agent)
# and that it supports `run_stream(...)` returning an object with `stream_text(...)`
async def _run_agent_once(
agent: Any,
user_input: str,
deps: Optional[dict] = None,
message_history: Optional[List[Any]] = None,
) -> str:
"""Run the agent once and return the full response text."""
deps = deps or {}
message_history = message_history or []
async with agent.run_stream(
user_input,
deps=deps,
message_history=message_history,
) as result:
chunks = []
async for chunk in result.stream_text(delta=True):
chunks.append(chunk)
return "".join(chunks)
def run_and_print_response(
agent: Any,
user_input: str,
deps: Optional[dict] = None,
message_history: Optional[List[Any]] = None,
) -> str:
"""
Synchronous wrapper: runs the agent and prints the full response text.
Returns the text as well.
"""
deps = PydanticAIDeps(
supabase=supabase,
openai_client=openai_client
)
full_text = asyncio.run(_run_agent_once(agent, user_input, deps, message_history))
print(full_text)
return full_text
# --- Example usage ---
full_text = run_and_print_response(pydantic_ai_expert, "Tell me about seadatanet dataset.")