library
import asyncio
import json
import os
import openai
from docx import Document
from langchain.llms import OpenAIChat
步骤描述
- 构造针对单独 prompt 的请求函数
async_generate
- 构造并发请求的函数
generate_concurrently
- 在函数中将每个单独请求的
async_generate
的数据进行整合
- 整合完了之后进行数据持久化
- 完成
async def async_generate(llm, text):
"""
:param llm:
:param text:
:return:
"""
prompt = """
....
""" + text
resp = await llm.agenerate([f"{prompt}"])
data = json.loads(resp.generations[0][0].text)
data['original_text'] = text
return data
async def generate_concurrently(all_texts):
"""
:param all_texts:
:return:
"""
llm = OpenAIChat(temperature=0.5)
tasks = [async_generate(llm, text) for text in all_texts]
result = await asyncio.gather(*tasks)
with open("output_syn.json", "w", encoding="utf-8") as f:
json.dump(result, f, ensure_ascii=False, indent=4)
print(result)
def main_asyn():
"""
:return:
"""
paths = [os.path.join("./Abstract", path) for path in os.listdir("./Abstract")]
all_texts = []
for path in paths:
all_texts += read_doc(path)
asyncio.run(generate_concurrently(all_texts[:2]))
if __name__ == '__main__':
os.environ["OPENAI_API_KEY"] = "填入你自己的 OPENAI key"
main_asyn()