-
Notifications
You must be signed in to change notification settings - Fork 2
/
websearch_ai_summary.py
59 lines (48 loc) · 1.72 KB
/
websearch_ai_summary.py
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
# License: MIT License
# This sample code shows how to use the Bing Web Search API to search
# the web for a query and summarize the results using the OpenAI API.
# The code defines a function web_search that takes a query and
# returns a list of search results. The function summarize_results
# takes the query, titles, and snippets of the search results and
# uses the OpenAI API to generate a 100-word summary of the results.
# You can try this out in the spreadsheet here:
# https://docs.google.com/spreadsheets/d/1nn0Ybnpr062Tf3oUebxEmTjymkeZ2mapT1SBZwtm6DQ/edit#gid=0
import requests
import neptyne as nt
from openai import OpenAI
BING_ENDPOINT = "https://api.bing.microsoft.com/v7.0/search"
def web_search(q):
params = {"q": q, "count": 10, "mkt": "en-US"}
response = requests.get(BING_ENDPOINT, params=params)
response.raise_for_status()
return [
[rec.get('url'), rec.get('name'), rec.get('snippet')]
for rec in response.json()['webPages']['value']
]
def summarize_results(query, titles, snippets):
results = [
title + ":" + snippet
for title, snippet in zip(titles, snippets)
if title and snippet
]
if not results:
return ""
prompt = (
"Formulate a 100 word answer to this question\n"
+ query
+ "\n"
+ "Based on these web search results (titles and snippets):\n"
+ "\n".join(results)
)
# No key needed; Neptyne provides one:
client = OpenAI()
completion = client.chat.completions.create(
model="gpt-4o",
messages=[
{
"role": "assistant",
"content": prompt,
},
],
)
return completion.choices[0].message.content