chatgpd接口调用、chatgpd异步调用、 智能 、异步例子
import asyncio import openai class getChar: def __init__(self,prompts): """ :param prompts: 提问组 """ self.task = list() self.p = prompts async def go_work(self,prompt): openai.api_key = "填写你在openai上面的key" # 这里要改 completions = openai.Completion.create( engine="text-davinci-002", prompt=prompt, max_tokens=4000, # 一次回复至多4097个字,不要写多了写多了倒漠子啦! n=1, temperature=0.5, ) print('问:%s:+++++++++++++++++++++'%prompt) print('答:',str(completions.choices[0].text)) def go(self): for i in self.p: self.task.append(self.go_work(i)) loop = asyncio.get_event_loop() loop.run_until_complete(asyncio.wait(self.task)) if __name__ == '__main__': q = [ '问题1',
'问题2',
'...', # 这里写多个问题异步回答 ] Q = getChar(q) Q.go()
我是有多无聊才会写这个......