如何在Azure Bot Service中实现机器人调用LLM智能回答问题?

摘要:问题描述 使用Azure Bot Service来部署机器人服务,如何把大模型嵌入其中呢? 比如在对话消息时候,能让大模型来提供回答? 问题解答 其实很简单,在Bot代码中添加大模型的调用就行。 以Python代码为例, 首先是准备好调用L
问题描述 使用Azure Bot Service来部署机器人服务,如何把大模型嵌入其中呢? 比如在对话消息时候,能让大模型来提供回答? 问题解答 其实很简单,在Bot代码中添加大模型的调用就行。 以Python代码为例, 首先是准备好调用LLM的请求代码 ## 示例中使用的是Azure OpenAI的模型 import requests # Azure OpenAI 客户端 def get_openai_answer(prompt): api_key = "your api key" endpoint = "your deployment endpoint, like: https://<your azure ai name>.openai.azure.com/openai/deployments/<LLM Name>/chat/completions?api-version=2025-01-01-preview" if not api_key or not endpoint: raise ValueError("请配置 Azure OpenAI 信息") headers = { "Content-Type": "application/json", "api-key": api_key } systemprompt = f"""" 您是一个有趣的聊天智能体,能愉快的和人类聊天""" data = { "messages": [ {"role": "system", "content": systemprompt}, {"role": "user", "content": prompt} ], "max_tokens": 5000, "temperature": 0.7 } response = requests.post(endpoint, headers=headers, json=data) response.raise_for_status() result = response.json() return result["choices"][0]["message"]["content"] # 示例用法 if __name__ == "__main__": prompt = "请介绍一下Azure OpenAI的主要功能。
阅读全文