langchian_aws模块学习

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

python 复制代码
from langchain_aws.chat_models import ChatBedrock
import json

def 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.content
    print(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.content
    print(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.

相关推荐
DES 仿真实践家1 小时前
【Day 11-N22】Python类(3)——Python的继承性、多继承、方法重写
开发语言·笔记·python
云泽野5 小时前
【Java|集合类】list遍历的6种方式
java·python·list
IMPYLH7 小时前
Python 的内置函数 reversed
笔记·python
小赖同学啊9 小时前
物联网数据安全区块链服务
开发语言·python·区块链
码荼9 小时前
学习开发之hashmap
java·python·学习·哈希算法·个人开发·小白学开发·不花钱不花时间crud
武昌库里写JAVA10 小时前
Oracle如何使用序列 Oracle序列使用教程
java·开发语言·spring boot·学习·课程设计
小陈phd10 小时前
李宏毅机器学习笔记——梯度下降法
人工智能·python·机器学习
kk爱闹10 小时前
【挑战14天学完python和pytorch】- day01
android·pytorch·python
Blossom.11811 小时前
机器学习在智能建筑中的应用:能源管理与环境优化
人工智能·python·深度学习·神经网络·机器学习·机器人·sklearn
祁思妙想11 小时前
八股学习(三)---MySQL
数据库·学习·mysql