-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-example-calls.py
More file actions
executable file
Β·360 lines (292 loc) Β· 11.1 KB
/
api-example-calls.py
File metadata and controls
executable file
Β·360 lines (292 loc) Β· 11.1 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/usr/bin/env python3
"""
API Example Calls for You.com Python SDK
Run this file to see interactive examples of all available API endpoints.
Setup Instructions:
-------------------
1. Create and activate a virtual environment:
python3 -m venv venv
source .venv/bin/activate # On Windows: .venv\\Scripts\\activate
2. Install the package:
pip install youdotcom
3. Run the script:
python api-example-calls.py
The script will prompt you to enter your API key at runtime.
"""
from typing import Optional
from youdotcom import You
from youdotcom.models import (
ResearchTool,
ExpressAgentRunsRequest,
AdvancedAgentRunsRequest,
SearchEffort,
ReportVerbosity,
CustomAgentRunsRequest,
LiveCrawl,
LiveCrawlFormats,
ResponseCreated,
ResponseStarting,
ResponseOutputItemAdded,
ResponseOutputContentFull,
ResponseOutputItemDone,
ResponseOutputTextDelta,
ResponseDone,
AgentRunsBatchResponse,
AgentRunsStreamingResponse,
ContentsFormats,
WebSearchTool,
ResearchEffort,
ResearchResponse,
)
from youdotcom.utils import eventstreaming
# Will be initialized with API key in main()
you: Optional[You] = None
def express_batch_request():
"""
Express agent with batch (non-streaming) response
"""
print("\nπ Running Express Batch Request...\n")
assert you is not None, "SDK client not initialized"
results = you.agents.runs.create(request=ExpressAgentRunsRequest(
input="What is the capital of France?",
stream=False,
tools=[
WebSearchTool()
]
))
# Access the results - check if it's a batch response
if isinstance(results, AgentRunsBatchResponse):
if results.output:
for output in results.output:
if output.text:
print(output.text)
break
else:
print("No text response found in agent output")
else:
print("No response from agent")
else:
print("Unexpected response type")
def express_streaming_request():
"""
Express agent with streaming response
"""
print("\nπ Running Express Streaming Request...\n")
assert you is not None, "SDK client not initialized"
response = you.agents.runs.create(request=ExpressAgentRunsRequest(
input="Restaurants in San Francisco",
stream=True,
tools=[
WebSearchTool()
]
))
# Type narrow to ensure we have a streaming response
assert isinstance(response, eventstreaming.EventStream), "Expected streaming response"
with response as stream:
# Iterate through the stream and handle each event type
# Each chunk is an AgentRunsStreamingResponse with a 'data' field
for chunk in stream:
# The data field contains the actual event (discriminated by TYPE)
event_data = chunk.data
# Use isinstance() to narrow the type and handle each event
# This is the proper way to do a "switch statement" on Union types in Python
if isinstance(event_data, ResponseCreated):
print(f"β¨ Response created (seq: {event_data.seq_id})")
elif isinstance(event_data, ResponseStarting):
print(f"π Response starting (seq: {event_data.seq_id})")
elif isinstance(event_data, ResponseOutputItemAdded):
print(f"β Output item added: {event_data.seq_id}")
elif isinstance(event_data, ResponseOutputContentFull):
print("\nπ Web Search Results:")
if event_data.response.full:
for idx, result in enumerate(event_data.response.full, 1):
print(f" {idx}. {result.title} - {result.url}")
elif isinstance(event_data, ResponseOutputTextDelta):
# Print the delta text as it streams in (without newline)
print(event_data.response.delta, end='', flush=True)
elif isinstance(event_data, ResponseOutputItemDone):
print(f"\nβ
Output item done (index: {event_data.response.output_index})")
elif isinstance(event_data, ResponseDone):
print("\nπ Response completed!")
print(f" Runtime: {event_data.response.run_time_ms} seconds")
print(f" Finished: {event_data.response.finished}")
else:
print(f"β οΈ Unknown event type: {type(event_data).__name__}")
def advanced_batch_request():
"""
Advanced agent with batch response
"""
print("\nπ Running Advanced Batch Request...\n")
assert you is not None, "SDK client not initialized"
request = AdvancedAgentRunsRequest(
input="What is the capital of France?",
stream=False,
tools=[
ResearchTool(
search_effort=SearchEffort.LOW,
report_verbosity=ReportVerbosity.MEDIUM
)
]
)
results = you.agents.runs.create(request=request)
# Access the results - check if it's a batch response
if isinstance(results, AgentRunsBatchResponse):
if results.output:
for output in results.output:
if output.text:
print(output.text)
break
else:
print("No text response found in agent output")
else:
print("No response from agent")
else:
print("Unexpected response type")
def custom_batch_request():
"""
Custom agent with batch response
Note: Replace the agent ID with your own custom agent ID
"""
print("\nπ Running Custom Batch Request...\n")
assert you is not None, "SDK client not initialized"
# Replace this with your actual custom agent ID
custom_agent_id = "63773261-b4de-4d8f-9dfd-cff206a5cb51"
request = CustomAgentRunsRequest(
agent=custom_agent_id,
input="What is the capital of France?",
stream=False
)
try:
results = you.agents.runs.create(request=request)
print(results)
except Exception as e:
print(f"Error: {e}")
print("Note: Make sure to use a valid custom agent ID")
def search_request():
"""
Search API endpoint with livecrawl
"""
print("\nπ Running Search Request...\n")
assert you is not None, "SDK client not initialized"
results = you.search.unified(
query="artificial intelligence in farming",
count=1,
livecrawl=LiveCrawl.WEB,
livecrawl_formats=LiveCrawlFormats.MARKDOWN
)
print("Metadata:")
print(results.metadata)
print("\nWeb Results:")
if results.results and results.results.web:
web_urls = [result.url for result in results.results.web]
print(web_urls)
else:
print("No web results found")
def content_request():
"""
Contents API endpoint to fetch page content
In 2.0.0, the Contents API now uses:
- formats: Array of format types ('html', 'markdown', 'metadata')
- crawl_timeout: Optional timeout between 1-60 seconds
"""
print("\nπ Running Content Request...\n")
assert you is not None, "SDK client not initialized"
# Example 1: Get markdown content
print("Example 1: Fetching markdown content...")
results = you.contents.generate(
urls=["https://you.com"],
formats=[ContentsFormats.MARKDOWN]
)
print(f"Received {len(results)} result(s)")
for result in results:
print(f" URL: {result.url}")
print(f" Title: {result.title}")
if result.markdown:
print(f" Markdown preview: {result.markdown[:100]}...")
print("\n" + "-" * 40 + "\n")
# Example 2: Get multiple formats including metadata (json+ld, opengraph info)
print("Example 2: Fetching HTML + metadata...")
results = you.contents.generate(
urls=["https://you.com"],
formats=[ContentsFormats.HTML, ContentsFormats.METADATA],
crawl_timeout=30 # Optional: set custom timeout (1-60 seconds)
)
print(f"Received {len(results)} result(s)")
for result in results:
print(f" URL: {result.url}")
print(f" Title: {result.title}")
if result.metadata:
print(f" Metadata - Site Name: {result.metadata.site_name}")
print(f" Metadata - Favicon: {result.metadata.favicon_url}")
print()
def research_request():
"""
Research API endpoint for comprehensive, multi-step research answers
"""
print("\nπ Running Research Request...\n")
assert you is not None, "SDK client not initialized"
res = you.research(
input="Which global cities improved air quality the most over the past 10 years, and what measurable actions contributed?",
research_effort=ResearchEffort.STANDARD,
)
assert isinstance(res, ResearchResponse)
print("Research Answer:")
print(res.output.content[:500] + "..." if len(res.output.content) > 500 else res.output.content)
if res.output.sources:
print(f"\nSources ({len(res.output.sources)}):")
for source in res.output.sources:
print(f" - {source.title or 'Untitled'}: {source.url}")
# Available functions menu
FUNCTIONS = [
{"name": "Express Batch Request", "fn": express_batch_request},
{"name": "Express Streaming Request", "fn": express_streaming_request},
{"name": "Advanced Batch Request", "fn": advanced_batch_request},
{"name": "Custom Batch Request", "fn": custom_batch_request},
{"name": "Search Request", "fn": search_request},
{"name": "Content Request", "fn": content_request},
{"name": "Research Request", "fn": research_request},
]
def main():
"""
Main interactive CLI menu
"""
global you
print("\nββββββββββββββββββββββββββββββββββββββββββ")
print("β You.com API Examples Menu β")
print("ββββββββββββββββββββββββββββββββββββββββββ\n")
# Get API key from user
api_key = input("π Enter your You.com API key: ").strip()
if not api_key:
print("β API key is required to use the You.com API.")
return
# Initialize the SDK
try:
you = You(api_key_auth=api_key)
print("\nβ
API key set!\n")
except Exception as e:
print(f"β Error initializing SDK: {e}")
return
# Show menu options
for index, item in enumerate(FUNCTIONS, start=1):
print(f" [{index}] {item['name']}")
print(" [0] Exit\n")
# Get user choice
try:
choice = int(input("Select an option: "))
except ValueError:
print("β Invalid input. Please enter a number.")
return
if choice == 0:
print("Goodbye!")
return
if 1 <= choice <= len(FUNCTIONS):
selected = FUNCTIONS[choice - 1]
print(f"\nRunning: {selected['name']}...\n")
try:
selected['fn']()
except Exception as e:
print(f"\nβ Error running {selected['name']}: {e}")
else:
print("β Invalid selection. Please try again.")
if __name__ == "__main__":
main()