forked from xai-org/xai-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.py
More file actions
154 lines (118 loc) · 4.7 KB
/
search.py
File metadata and controls
154 lines (118 loc) · 4.7 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
from datetime import datetime, timedelta, timezone
from xai_sdk import Client
from xai_sdk.chat import user
from xai_sdk.search import SearchParameters, news_source, rss_source, web_source, x_source
def live_search(client: Client):
chat = client.chat.create(
model="grok-3",
# When no search sources are explicitly specified, the model will use the default
# search sources which are web and X.
# Mode auto means that the model will decide when to perform a search based on the
# prompt.
search_parameters=SearchParameters(mode="auto"),
)
user_input = input("Prompt: ")
chat.append(user(user_input))
response = chat.sample()
print(f"Content: {response.content}")
print(f"Citations ({len(response.citations)}): {response.citations}")
print(f"Unique search sources: {response.usage.num_sources_used}")
def live_search_streaming(client: Client):
chat = client.chat.create(
model="grok-3",
search_parameters=SearchParameters(mode="auto"),
)
user_input = input("Prompt: ")
chat.append(user(user_input))
print("Grok: ", end="", flush=True)
latest_response = None
for response, chunk in chat.stream():
latest_response = response
print(chunk.content, end="", flush=True)
assert latest_response is not None
print("\n")
print(f"Citations ({len(latest_response.citations)}): {latest_response.citations}")
print(f"Unique search sources: {latest_response.usage.num_sources_used}")
def live_search_with_x(client: Client):
chat = client.chat.create(
model="grok-3",
search_parameters=SearchParameters(
mode="auto",
sources=[x_source(included_x_handles=["xai"], post_favorite_count=1000)],
),
)
chat.append(user("What are some recent releases from xAI?"))
response = chat.sample()
print(f"Content: {response.content}")
print(f"Citations: {response.citations}")
print(f"Unique search sources: {response.usage.num_sources_used}")
def live_search_with_web(client: Client):
chat = client.chat.create(
model="grok-3",
search_parameters=SearchParameters(
# Mode on means the model will always perform a search and only use the explicit
# sources that are specified.
mode="on",
sources=[web_source(country="US", excluded_websites=["wikipedia.org"])],
),
)
chat.append(user("Tell me about Nikola Tesla"))
response = chat.sample()
print(f"Content: {response.content}")
print(f"Citations: {response.citations}")
print(f"Unique search sources: {response.usage.num_sources_used}")
def live_search_with_news(client: Client):
chat = client.chat.create(
model="grok-3",
search_parameters=SearchParameters(
mode="on",
sources=[news_source(country="US", excluded_websites=["foxnews.com"])],
),
)
chat.append(user("What is the latest news on the stock market?"))
response = chat.sample()
print(f"Content: {response.content}")
print(f"Citations: {response.citations}")
print(f"Unique search sources: {response.usage.num_sources_used}")
def live_search_with_web_and_news(client: Client):
chat = client.chat.create(
model="grok-3",
search_parameters=SearchParameters(
mode="on",
# Only consider news and web articles from the last 7 days
from_date=datetime.now(timezone.utc) - timedelta(days=7),
sources=[web_source(country="US"), news_source(country="US")],
),
)
chat.append(user("What is the latest news on the stock market?"))
response = chat.sample()
print(f"Content: {response.content}")
print(f"Citations: {response.citations}")
print(f"Unique search sources: {response.usage.num_sources_used}")
def live_search_with_rss(client: Client):
chat = client.chat.create(
model="grok-3",
search_parameters=SearchParameters(
mode="on",
sources=[
rss_source(links=["https://status.x.ai/feed.xml"]),
],
),
)
chat.append(user("What is the status of xAI's services?"))
response = chat.sample()
print(f"Content: {response.content}")
print(f"Citations: {response.citations}")
print(f"Unique search sources: {response.usage.num_sources_used}")
def main():
client = Client()
live_search(client)
# Uncomment the respective line to run the example.
# live_search_streaming(client)
# live_search_with_x(client)
# live_search_with_web(client)
# live_search_with_news(client)
# live_search_with_web_and_news(client)
# live_search_with_rss(client)
if __name__ == "__main__":
main()