Skip to content

Instantly share code, notes, and snippets.

@toriato
Last active March 4, 2023 17:13
Show Gist options
  • Save toriato/35be9328dbea90961c6d32c96f77c7ba to your computer and use it in GitHub Desktop.
Save toriato/35be9328dbea90961c6d32c96f77c7ba to your computer and use it in GitHub Desktop.
from typing import List, Dict, Any
import re
from datetime import datetime
import openai
from requests import PreparedRequest, Session
bing_endpoint = 'https://api.bing.microsoft.com/v7.0/search'
bing_subscription_key = 'changemeeeeeeeeeeeeeeeeeeeeeeeee'
openai_api_key = 'sk-myasssssssssssssssssssssssssssssssssssssssss'
model = 'gpt-3.5-turbo'
openai.api_key = openai_api_key
def search(query: str) -> List[Dict[str, Any]]:
req = PreparedRequest()
req.prepare_method('GET')
req.prepare_url(bing_endpoint, {
'q': query,
'count': 15
})
req.prepare_headers({
'Ocp-Apim-Subscription-Key': bing_subscription_key
})
res = Session().send(req)
data = res.json()
return data['webPages']['value']
def querify_question(question: str) -> str:
response = openai.ChatCompletion.create(
model=model,
messages=[
{
'role': 'system',
'content': f'''
Please convert the received question into a search term or search keyword format that is easy to handle by search engines, and return it.
The returned string must be in the same language as the question.
Please do not enclose the string in quotation marks and avoid using special characters as much as possible.
Only show the converted search term.
Current date: {datetime.now()}
'''.strip()
},
{
'role': 'user',
'content': f'{question}'
}
]
)
content: str = response['choices'][0]['message']['content'] # type: ignore
if content.startswith('FAILED: '):
raise ValueError(content)
return content.strip()
def start_summary(question: str):
query = querify_question(question)
# query = question
print(f'다음과 같이 검색해보겠습니다: {query}')
results = search(query)
raw_results = '\n'.join([
f"{i}. {r['name']}, {r['snippet']}"
for i, r in enumerate(results)
])
messages = [
{
'role': 'system',
'content': f'''
Using the following guidelines, please create a newly organized text in step-by-step fashion.
(1) Use the given references related to the question to create a completely new text.
(1-1) The context should be as smooth as possible.
(1-2) Use vocabulary that is easy to read.
(1-3) It is okay to mix the context back and forth.
(2) Mark the relevant reference number in the form of a comment [number] at the end of the word, sentence, or paragraph.
(2-1) Always include a reference for proper nouns.
(2-2) e.g. His name is MC Moo-Hyun[1][3], and he was South Korea's president[2].
(3) Use the following criteria for references.
(3-1) Do not use subjective or biased references.
(3-2) Do not use uncertain or factually unsupported references.
(3-3) Try to avoid using references other than Wikipedia, media, government, or corporate websites.
(3-4) Do not make up stories when there are no references available.
The given references are as follows:
{raw_results}
The question is
'''.strip()
},
{
'role': 'user',
'content': question
}
]
response = openai.ChatCompletion.create(
model=model,
messages=messages
)
message: Dict[str, Any] = response['choices'][0]['message'] # type: ignore
messages.append(message)
used_result_indexes = set([
int(m[1])
for m in re.finditer(r'\[(\d+)\]', message['content'])
])
print('-------------------')
print(message['content'].strip())
print('-------------------')
print(
'\n'.join([f"[{i}] {results[i]['url']}" for i in used_result_indexes])
)
print('-------------------')
while True:
messages.append({
'role': 'user',
'content': input('> ')
})
response = openai.ChatCompletion.create(
model=model,
messages=messages
)
msg: Dict[str, Any] = response['choices'][0]['message'] # type: ignore
messages.append(msg)
print('-------------------')
print(msg['content'].strip())
print('-------------------')
start_summary(input('어떤게 궁금하신가요?: '))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment