当前位置: 首页 > news >正文

【Azure Bot Service】在机器人服务中如何调用LLM来回答问题呢?

问题描述

使用Azure Bot Service来部署机器人服务,如何把大模型嵌入其中呢?

比如在对话消息时候,能让大模型来提供回答?

image

 

问题解答

其实很简单,在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的主要功能。"print(get_openai_answer(prompt))

 

然后,在 EchoBot 的 on_message_activity 中调用OpenAI接口即可

# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.from botbuilder.core import ActivityHandler, MessageFactory, TurnContext
from botbuilder.schema import ChannelAccount
from bots.call_openai import get_openai_answerclass EchoBot(ActivityHandler):async def on_members_added_activity(self, members_added: [ChannelAccount], turn_context: TurnContext):for member in members_added:if member.id != turn_context.activity.recipient.id:await turn_context.send_activity("Hello and welcome, this is python code.")async def on_message_activity(self, turn_context: TurnContext):#call LLM API to get response llmresponse = get_openai_answer(turn_context.activity.text)return await turn_context.send_activity(MessageFactory.text(f"{llmresponse}"))

测试效果:

image

[完]

 

参考资料

发送和接收文本消息:https://docs.azure.cn/zh-cn/bot-service/bot-builder-howto-send-messages?view=azure-bot-service-4.0&tabs=python#send-a-typing-indicator

 

http://www.zskr.cn/news/182978.html

相关文章:

  • 基于Miniconda的PyTorch安装教程:专为GPU加速设计的轻量环境
  • 使用Miniconda创建独立环境避免PyTorch与TensorFlow版本冲突
  • 2025.10.25-26
  • 远程日志采集:集中管理多个Miniconda容器的日志
  • 【技术复盘】 设备跨机迁移后的 ARP 缓存连通性故障分析
  • Docker port查看Miniconda容器端口映射情况
  • PyTorch安装教程GPU版:基于Miniconda-Python3.10镜像的一键部署方案
  • Docker cp在宿主机与Miniconda容器间传输文件
  • Miniconda环境去重:合并重复的依赖项减少冗余
  • Java20243718今日学习!
  • 补一下学了啥,直接提交了。。。
  • Docker build cache提高Miniconda镜像构建效率
  • Python虚拟环境最佳实践:Miniconda取代传统venv方案
  • AI应用架构师重塑智能家居生态系统AI应用格局
  • Markdown语法进阶:制作美观的技术文档记录环境搭建过程
  • 拆分的第一性原理——按业务域、一致性与团队边界来切,避免“为拆而拆”
  • AUTO TECH China 2026 广州国际汽车底盘系统技术展览会
  • 学长亲荐10个AI论文软件,继续教育论文写作必备!
  • 【遗传算法(GA)和模拟退火(SA)对翼型升阻比进行优化】基于神经网络和无导数算法的翼型优化附Matlab代码
  • 读书笔记5-11.13
  • 程序员必看!收藏这篇:知识图谱如何解决大模型的幻觉问题
  • Markdown表格对比:Miniconda与Anaconda功能差异一览
  • AI大模型时代程序员生存指南:从职业转型到高薪岗位的完整路径_大龄程序员想转行大模型,应该往哪个方向转?
  • 四轴桥板卧加编程:AB轴坐标转换宏程序与VT送出
  • Miniconda环境合并:将多个env整合为统一平台
  • AdisInsight数据库的3个应用场景与5个内容模块
  • Java学习。
  • 2025大模型完全指南:从原理到实战,一篇就够了,建议收藏学习!初识大模型(非常详细)
  • GitHub Pull Request流程:贡献Miniconda相关开源项目
  • Linux终端操作进阶:自动化脚本部署Miniconda环境