当前位置: 首页 > 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.jsqmd.com/news/167369/

相关文章:

  • 使用GitHub Actions集成Miniconda-Python3.10进行CI/CD测试
  • 使用Miniconda-Python3.10镜像在Jupyter中运行PyTorch代码的完整步骤
  • 利用Miniconda-Python3.10镜像简化TensorFlow和PyTorch共存配置
  • 洛谷 P5047
  • 环境仿真软件:AnyLogic_(11).模型参数优化
  • 将Markdown转为HTML的技术路径:基于Miniconda环境的实现
  • 环境仿真软件:AnyLogic_(11).数据收集与分析
  • 微前端系列:核心概念、价值与应用场景
  • 基于Circle混沌映射的麻雀搜索算法Circle-SSA(Matlab代码及23个基准测试函数)
  • prometheus监控
  • 企业级AI开发环境标准化:Miniconda镜像的应用实践
  • Leecode_6.Z 字形变换
  • GameAssist智能游戏助手:从菜鸟到高手的秘密武器
  • extern
  • Jupyter Notebook集成Miniconda-Python3.10:打造交互式AI开发平台
  • C#之return
  • MySQL中的timediff、timestampdiff、datediff详解
  • 如何通过Docker Run命令加载Miniconda镜像并启用GPU支持
  • javaCV简单解析gb28181的rtp ps流,并推流到rtmp服务
  • 解决‘CondaValueError: prefix already exists’冲突提示
  • C#之ref与out
  • Docker inspect获取Miniconda容器详细元数据
  • C#之类型与实例
  • 使用Miniconda-Python3.10进行大规模Token统计分析
  • 程序员必备!一款免费的(原文/译文)AI 双语对照网页翻译插件,信息获取效率飙升!
  • 使用Miniconda创建独立环境避免PyTorch与TensorFlow版本冲突
  • 【Week2_Day5】【软件测试学习记录与反思】【坚定职业规划、数据库的了解、navicat操作、MairaDB配置、创建远程登录用户、连接服务器数据库、SQL语句练习】
  • 高效配置PyTorch环境:Miniconda与Anaconda的对比及最佳实践
  • 模拟登录验证三次机会 - GLORY-TO-THE
  • 合作文章|ChIP-seq联合RNA-seq揭示FOXS1-BSCL2轴调控胆固醇代谢与炎症的新机制