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.

相关推荐
miracletiger1 小时前
uv 新的包管理工具总结
linux·人工智能·python
我不会编程5552 小时前
Python Cookbook-6.10 保留对被绑定方法的引用且支持垃圾回收
开发语言·python
ʚɞ 短腿欧尼2 小时前
关系数据的可视化
python·pycharm·可视化·数据可视化·图表
FAREWELL000753 小时前
C#进阶学习(十六)C#中的迭代器
开发语言·学习·c#·迭代器模式·迭代器
DXM05214 小时前
牟乃夏《ArcGIS Engine地理信息系统开发教程》学习笔记3-地图基本操作与实战案例
开发语言·笔记·学习·arcgis·c#·ae·arcgis engine
PXM的算法星球4 小时前
【软件工程】面向对象编程(OOP)概念详解
java·python·软件工程
qsmyhsgcs5 小时前
Java程序员转人工智能入门学习路线图(2025版)
java·人工智能·学习·机器学习·算法工程师·人工智能入门·ai算法工程师
Humbunklung5 小时前
PySide6 GUI 学习笔记——常用类及控件使用方法(常用类矩阵QRectF)
笔记·python·学习·pyqt
蹦蹦跳跳真可爱5895 小时前
Python----深度学习(基于DNN的吃鸡预测)
python·深度学习·dnn
JJ1M86 小时前
Git技巧:Git Hook,自动触发,含实战分享
git·python·自动化