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

langchian_aws模块学习

利用langchain_aws模块实现集成bedrock调用模型,测试源码

from langchain_aws.chat_models import ChatBedrock
import jsondef invoke_with_text(model_id, message):llm = ChatBedrock(model_id=model_id, region_name="us-east-1")res = llm.invoke(message)print(f"模型返回的结果:\n {res.content}")# print(f"id: {res.id}")# print(f"additional_kwargs: {res.additional_kwargs}")# print(f"response_metadata: {res.response_metadata}")def invoke_with_json(model_id, input_text):llm = ChatBedrock(model_id=model_id, region_name="us-east-1")prompt = {"text_prompts": [{"text": f"您好,我是一个翻译官。请将以下句子翻译成英文:\n\n{input_text}"}],"cfg_scale": 10,"seed": 0,"steps": 50}response = llm.invoke(json.dumps(prompt))result = response.contentprint(f"模型返回的结果: {result}")def translate(model_id, input_text, target_lang="en"):llm = ChatBedrock(model_id=model_id, region_name="us-east-1")prompt = {"text_prompts": [{"text": f"您好,我是一个翻译官。请将以下句子翻译成{target_lang}语:\n\n{input_text}"}],"cfg_scale": 10,"seed": 0,"steps": 50}response = llm.invoke(json.dumps(prompt))result = response.contentprint(f"模型返回的结果: {result}")if __name__=="__main__":# 使用文本输入调用model_id = "anthropic.claude-3-sonnet-20240229-v1:0"message = "您好,您是一个翻译官,烦请将如下语句翻译成英文: 关于这个问题的解决方案,我们将会在后面测试验证下,然后在将手册同步给您"invoke_with_text(model_id, message)# 使用 JSON 输入调用input_text = "关于这个问题的解决方案,我们将会在后面测试验证下,然后在将手册同步给您"invoke_with_json(model_id, input_text)# 翻译成英文translate(model_id, input_text, target_lang="en")# 翻译成西班牙语translate(model_id, input_text, target_lang="es")   

运行之后的结果如下

模型返回的结果:
 Here is the translation to English:

Regarding the solution to this issue, we will conduct tests and verification later, then we will sync the manual to you.
模型返回的结果: Here is the translation to English:

Regarding the solution to this issue, we will validate it through further testing later on, and then synchronize the manual with you.
模型返回的结果: Here is the translation to English:

Regarding the solution to this issue, we will perform further tests and verification, and then synchronize the manual with you.
模型返回的结果: Aquí está la traducción al español:

Sobre la solución a este problema, la validaremos con pruebas más adelante y luego sincronizaremos el manual con usted.

http://www.lryc.cn/news/366993.html

相关文章:

  • 归并排序-成绩输出-c++
  • ✔️Vue基础+
  • 基于VS2022编译GDAL
  • C语言之字符函数总结(全部!),一篇记住所有的字符函数
  • vite常识性报错解决方案
  • 2024.06.08【读书笔记】丨生物信息学与功能基因组学(第十二章 全基因组和系统发育树 第四部分)【AI测试版】
  • IO进程线程(八)线程
  • Linux基础指令网络管理003
  • 在Android中使用 MQTT 服务实现消息通信
  • qsort函数
  • 你可以直接和数据库对话了!DB-GPT 用LLM定义数据库下一代交互方式,数据库领域的GPT、开启数据3.0 时代
  • 数据结构笔记2 栈和队列
  • Python | 刷题笔记
  • 软件三班20240605
  • http和https数据传输与协议区分
  • 天才程序员周弈帆 | Stable Diffusion 解读(一):回顾早期工作
  • 软件架构初探
  • Python01 -分解整包数据到各个变量操作和生成器
  • flutter image_picker 执行拍照的图片怎么保存到本地
  • 基于Python的北京天气数据可视化分析
  • Linux编译器-gcc或g++的使用
  • 一条sql的执行流程
  • Android音乐播放器的思路处理
  • 算法课程笔记——可撤销并查集
  • 【排序算法】快速排序
  • OS复习笔记ch7-2
  • 4.通用编程概念
  • iBeacon赋能AR导航:室内定位技术的原理与优势
  • 【sklearn】【逻辑回归1】
  • java(kotlin)和 python 通过DoubleCloud的kafka进行线程间通信