# 添加自己的APIKEY (openai-env) zrf@debian:~/git/web-crawl-q-and-a-example$ cat .env # Once you add your API key below, make sure to not share it with anyone! The API key should remain private. OPENAI_API_KEY=abc123
client = OpenAI() # defaults to getting the key using os.environ.get("OPENAI_API_KEY") # if you saved the key under a different environment variable name, you can do something like: # client = OpenAI( # api_key=os.environ.get("CUSTOM_ENV_NAME"), # )
三. 一个小测试
测试代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
import os from openai import OpenAI from dotenv import load_dotenv load_dotenv()
(openai-env) zrf@debian:~/git/web-crawl-q-and-a-example$ cat processed/embeddings.csv ,text,n_tokens,embeddings 0,". This year is 2023. My name is zrf, female, and I'm 18 years old this year. zrf has a friend of the opposite sex who was born in 1998.",43,\ "[-0.027196646, -0.022042233, -0.010211693, 0.0008644648, -0.0007859507, 0.0085734185, -0.037790384, -0.011888819, -0.026989432, -0.026393697, 0.024930257, 0.011176526, 0.0037103994,....很长,不贴了]"
3.3 提问
1 2
print(answer_question(df, question="zrf的朋友是男性还是女性,zrf的朋友比zrf大几岁")) print(answer_question(df, question="zrf's friend is male or female, and how many years older is zrf's friend than zrf?"))
# Create a class to parse the HTML and get the hyperlinks classHyperlinkParser(HTMLParser): def__init__(self): # Override the HTMLParser's handle_starttag method to get the hyperlinks defhandle_starttag(self, tag, attrs):
下一个函数将 URL 作为参数,打开 URL,并读取 HTML 内容。然后,它返回在该页面上找到的所有超链接。
1 2 3
# Function to get the hyperlinks from a URL defget_hyperlinks(url): return parser.hyperlinks
defanswer_question( df, model="text-davinci-003", question="Am I allowed to publish model outputs to Twitter, without a human review?", max_len=1800, size="ada", debug=False, max_tokens=150, stop_sequence=None ):