Last active
March 13, 2023 01:10
-
-
Save Mikubill/378cd880abbe43f46877589df941682f to your computer and use it in GitHub Desktop.
Simple Toolformer with OpenAI API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import json | |
import openai | |
import requests | |
from copy import deepcopy | |
from datetime import date | |
today = date.today() | |
openai.api_key = "" | |
google_search_id = "" | |
google_search_api_key = "" | |
google_search_url = f"https://www.googleapis.com/customsearch/v1?key={google_search_api_key}&cx={google_search_id}&q=" | |
prefix = f""" | |
您是一名 AI 助手,可以使用多种工具。 这些工具如下: | |
MATH: This allows you to evaluate mathematical expressions using the sympy library. | |
NOW: This returns the current date and time. You must not pass any arguments to this tool! | |
SEARCH: This returns a web search result for the given string argument. | |
DO NOT USE TOOLS WITHIN TOOLS! KEEP ALL TOOL CALLS SEPARATE FROM EACH OTHER! | |
User:10 乘以 14 是多少? | |
Query:MATH(10 * 14) | |
Hint:MATH(10 * 14)=140 | |
Assistant:10 * 14 是 140。 | |
User: 唐纳德特朗普多大了? | |
Query: SEARCH(Donald Trump 年龄) | |
Hint: SEARCH(Donald Trump 年龄)=76 | |
Assistant: 唐纳德·特朗普今年 76 岁。 | |
User: 今天是什么日子? | |
Query: NOW() | |
Hint: NOW()=2023/02/23 | |
Assistant: 现在是2023年2月23日。 | |
User: 电灯泡的发明者是哪里人,他的祖国最后一次与哪个国家交战? | |
Query: SEARCH(Thomas Edison birthplace) | |
Hint: SEARCH(Thomas Edison birthplace)=Milan, Ohio | |
Query: SEARCH(last country US at war with) | |
Hint: SEARCH(最后一个与美国交战的国家)=Iraq | |
Assistant: 托马斯·爱迪生,灯泡的发明者,出生于俄亥俄州的米兰。 美国上一个与之交战的国家是伊拉克。 | |
""" | |
thread_storage = {} | |
def query(inputs=""): | |
result = "Hint: " | |
term = inputs.replace("Query: ", "") | |
if term.startswith("NOW"): | |
g = today.strftime("%d/%m/%Y") | |
result += f"{term}={g}" | |
elif term.startswith("SEARCH"): | |
res = requests.get(google_search_url+term.replace("SEARCH: ", "")) | |
j = json.loads(res.text) | |
result += f"{term}=" | |
for item in j["items"]: | |
title = item["title"] | |
content = item["snippet"] | |
result += f"{title} - {content}," | |
elif term.startswith("MATH"): | |
f = sympify(term.replace("MATH ", "")) | |
result += f"{term}={f}" | |
print(result) | |
return result | |
def gen(inputs="", thread_id=""): | |
"""get incoming message""" | |
inb_msg = deepcopy(prefix) | |
inb_msg += f"User: {inputs.strip()}\n" | |
print(inb_msg) | |
counter = 0 | |
while counter < 4: | |
counter += 1 | |
print(f"request t={counter}") | |
openai_response = openai.Completion.create( | |
model="text-davinci-003", | |
prompt=inb_msg, | |
max_tokens=500, | |
temperature=0.7, | |
stream=True, | |
) | |
combined = "" | |
total = [] | |
for resp in openai_response: | |
text = resp["choices"][0]["text"] | |
if text == "\n": | |
print(combined) | |
if combined.strip().startswith("Assistant"): | |
return combined.strip() | |
if combined.startswith("Query: "): | |
inb_msg += combined | |
inb_msg += "\n" | |
inb_msg += query(combined) | |
break | |
combined = "" | |
continue | |
combined += text | |
print(combined) | |
if combined.strip().startswith("Assistant"): | |
return combined.strip() | |
return combined | |
if __name__ == "__main__": | |
print(gen()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment