news 2026/6/3 3:45:55

【OpenAI标准调用和异步调用Python代码例子】

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
【OpenAI标准调用和异步调用Python代码例子】
fromconfigimportload_or_create_configfrommodelsimportMessagefromtypingimportOptional,List,Dict,AnyfromopenaiimportOpenAI,AsyncOpenAIimportasynciofromasyncioimportSemaphoreimportaiohttpimporttime# 可选的配置管理类def_build_messages(text:str,system_prompt:Optional[str]=None,history:Optional[List[Message]]=None)->List[Dict[str,Any]]:"""构建消息列表"""messages=[]ifsystem_prompt:messages.append({"role":"system","content":system_prompt})ifhistory:formsginhistory:messages.append({"role":msg.role,"content":msg.content})messages.append({"role":"user","content":text})returnmessagesclassLLMClient:def__init__(self,config_path:str="config.yaml",client_name:str="openai",history_max:int=10,system_prompt:Optional[str]=None,max_concurrent_requests:int=5,# 新增:最大并发数timeout:int=30,# 新增:超时时间):self.llm_client_list={}self.async_llm_client_list={}# 新增:异步客户端列表self.config=load_or_create_config(config_path)self.history=[]self.history_max=history_max self.client_name=client_name self.max_concurrent_requests=max_concurrent_requests self.timeout=timeout self.semaphore=Semaphore(max_concurrent_requests)# 并发控制# 初始化同步客户端ifself.client_name=="openai":self.llm_client_list["openai"]=OpenAI(api_key=self.config.models.openai.api_key,base_url=self.config.models.openai.base_url,timeout=timeout)# 新增:初始化异步客户端self.async_llm_client_list["openai"]=AsyncOpenAI(api_key=self.config.models.openai.api_key,base_url=self.config.models.openai.base_url,timeout=timeout)self.system_prompt=system_promptdefllm_call(self,text:str,model_name:str=None)->Optional[Message]:"""同步调用(保持原样)"""history=Noneifmodel_nameisNone:model_name=self.config.models.openai.model_nameifself.client_name=="openai":ifself.history_max>0andself.history:history=self.history[-self.history_max:]res_text=self.llm_openai_call(text,self.system_prompt,history=history,model_name=model_name,max_per_tokens=self.config.models.openai.max_per_tokens)self._add_to_history("user",text)self._add_to_history("assistant",res_text)returnres_textreturnNone# 新增:异步调用方法asyncdefallm_call(self,text:str,model_name:str=None,stream:bool=False)->Optional[str]:""" 异步调用LLM Args: text: 用户输入文本 model_name: 模型名称 stream: 是否使用流式响应 Returns: 模型回复内容 """ifmodel_nameisNone:model_name=self.config.models.openai.model_nameifself.client_name=="openai":# 获取历史记录history=Noneifself.history_max>0andself.history:history=self.history[-self.history_max:]asyncwithself.semaphore:# 并发控制ifstream:# 流式响应returnawaitself.allm_openai_call_stream(text,self.system_prompt,history=history,model_name=model_name,max_per_tokens=self.config.models.openai.max_per_tokens)else:# 普通响应res_text=awaitself.allm_openai_call(text,self.system_prompt,history=history,model_name=model_name,max_per_tokens=self.config.models.openai.max_per_tokens)self._add_to_history("user",text)self._add_to_history("assistant",res_text)returnres_textreturnNone# 新增:批量异步调用asyncdefbatch_allm_call(self,texts:List[str],model_name:str=None,max_workers:int=None)->List[str]:""" 批量异步调用LLM Args: texts: 文本列表 model_name: 模型名称 max_workers: 最大并发数 Returns: 回复列表 """ifmax_workers:self.semaphore=Semaphore(max_workers)ifmodel_nameisNone:model_name=self.config.models.openai.model_name tasks=[]fortextintexts:task=self.allm_call(text,model_name,stream=False)tasks.append(task)# 使用asyncio.gather并发执行try:results=awaitasyncio.gather(*tasks,return_exceptions=True)# 处理结果final_results=[]fori,resultinenumerate(results):ifisinstance(result,Exception):print(f"第{i}个请求失败:{str(result)}")final_results.append(f"请求失败:{str(result)}")else:final_results.append(result)returnfinal_resultsexceptExceptionase:raiseException(f"批量调用失败:{str(e)}")# 新增:异步OpenAI调用asyncdefallm_openai_call(self,text:str,system_prompt:Optional[str]=None,history:Optional[List[Message]]=None,model_name:str=None,max_per_tokens:int=4096,temperature:float=0.2,)->Optional[str]:"""异步OpenAI调用"""messages=_build_messages(text,system_prompt,history)try:response=awaitself.async_llm_client_list["openai"].chat.completions.create(model=model_nameorself.config.models.openai.model_name,messages=messages,stream=False,timeout=self.timeout)ifresponse.choicesandlen(response.choices)>0:assistant_reply=response.choices[0].message.contentreturnassistant_replyexceptasyncio.TimeoutError:raiseException("请求超时")exceptaiohttp.ClientErrorase:raiseException(f"网络错误:{str(e)}")exceptExceptionase:raiseException(f"调用模型时出错:{str(e)}")# 新增:流式响应异步调用asyncdefallm_openai_call_stream(self,text:str,system_prompt:Optional[str]=None,history:Optional[List[Message]]=None,model_name:str=None,max_per_tokens:int=4096,temperature:float=0.2,):"""流式响应异步调用(返回生成器)"""messages=_build_messages(text,system_prompt,history)try:response=awaitself.async_llm_client_list["openai"].chat.completions.create(model=model_nameorself.config.models.openai.model_name,messages=messages,stream=True,timeout=self.timeout)asyncforchunkinresponse:ifchunk.choicesandchunk.choices[0].delta.content:yieldchunk.choices[0].delta.contentexceptasyncio.TimeoutError:yield"【请求超时】"exceptExceptionase:yieldf"【错误:{str(e)}】"defllm_openai_call(self,text:str,system_prompt:Optional[str]=None,history:Optional[List[Message]]=None,model_name:str=None,max_per_tokens:int=4096,temperature:float=0.2,)->Optional[str]:"""同步OpenAI调用(保持原样)"""messages=_build_messages(text,system_prompt,history)try:response=self.llm_client_list["openai"].chat.completions.create(model=model_nameorself.config.models.openai.model_name,messages=messages,max_tokens=max_per_tokens,temperature=temperature,stream=False,)ifresponse.choicesandlen(response.choices)>0:assistant_reply=response.choices[0].message.contentreturnassistant_replyexceptExceptionase:error_msg=f"调用模型时出错:{str(e)}"raiseException(error_msg)def_add_to_history(self,role:str,content:str):"""添加消息到历史,自动管理长度"""self.history.append(Message(role=role,content=content))iflen(self.history)>self.history_max:to_remove=len(self.history)-self.history_max to_remove=to_remove-(to_remove%2)ifto_remove>0:self.history=self.history[to_remove:]# 新增:上下文管理器支持asyncdef__aenter__(self):returnselfasyncdef__aexit__(self,exc_type,exc_val,exc_tb):awaitself.close()# 新增:关闭异步客户端asyncdefclose(self):"""关闭异步客户端连接"""forclientinself.async_llm_client_list.values():ifhasattr(client,'close'):awaitclient.close()# 使用示例asyncdefexample_usage():# 1. 创建客户端client=LLMClient(config_path="config.yaml",history_max=10,system_prompt="你是一个有帮助的助手",max_concurrent_requests=3)# 2. 异步单次调用response=awaitclient.allm_call("介绍一下Python")print(f"Response:{response}")# 3. 批量调用questions=["什么是人工智能?","机器学习有哪些类型?","深度学习是什么?"]responses=awaitclient.batch_allm_call(questions,max_workers=2)fori,respinenumerate(responses):print(f"Q{i+1}:{questions[i][:30]}...")print(f"A{i+1}:{resp[:50]}...")print()# 运行示例if__name__=="__main__":# 运行异步示例asyncio.run(example_usage())
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/23 12:47:21

【IEEE Transactions系列期刊全览:计算机领域核心期刊深度解析】

在计算机研究的星空中,IEEE Transactions期刊如同璀璨的星座,指引着学术探索的方向。本文为您揭开这些顶级期刊的神秘面纱。一、IEEE Transactions系列概览 IEEE Transactions系列期刊是美国电气电子工程师学会(IEEE)旗下的旗舰学…

作者头像 李华
网站建设 2026/5/20 11:16:25

调节语速快慢:CosyVoice3通过文本密度间接控制

调节语速快慢:CosyVoice3通过文本密度间接控制 在语音合成技术不断逼近“以假乱真”的今天,一个常被忽视却极为关键的问题浮出水面:如何让AI说话不只是“说得清”,而是“说得好”? 传统TTS系统中,调节语速往…

作者头像 李华
网站建设 2026/5/30 17:56:34

BongoCat桌面伴侣终极指南:让可爱猫咪为你的数字生活增添无限乐趣

BongoCat桌面伴侣终极指南:让可爱猫咪为你的数字生活增添无限乐趣 【免费下载链接】BongoCat 让呆萌可爱的 Bongo Cat 陪伴你的键盘敲击与鼠标操作,每一次输入都充满趣味与活力! 项目地址: https://gitcode.com/gh_mirrors/bong/BongoCat …

作者头像 李华
网站建设 2026/5/29 20:50:25

有道云笔记数据安全备份完整指南

还在为云端笔记数据安全而担忧吗?这款强大的Python工具能帮你将有道云笔记中的所有内容完整导出到本地,彻底解决数据备份和迁移难题。无论你是笔记重度用户还是偶尔使用者,都能轻松掌握这个数据保护的终极解决方案。 【免费下载链接】youdaon…

作者头像 李华
网站建设 2026/6/2 7:32:37

终极指南:5分钟快速掌握drawio专业图标库,轻松绘制专业图表

终极指南:5分钟快速掌握drawio专业图标库,轻松绘制专业图表 【免费下载链接】drawio-libs Libraries for draw.io 项目地址: https://gitcode.com/gh_mirrors/dr/drawio-libs 还在为绘制专业图表而头疼吗?drawio-libs项目正是你需要的…

作者头像 李华
网站建设 2026/6/2 7:32:55

Bad Apple病毒项目终极指南:Windows窗口动画的完整实现方案

Bad Apple病毒项目终极指南:Windows窗口动画的完整实现方案 【免费下载链接】bad_apple_virus Bad Apple using Windows windows 项目地址: https://gitcode.com/gh_mirrors/ba/bad_apple_virus Bad Apple病毒项目是一个巧妙利用Windows窗口系统实现高性能实…

作者头像 李华