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.

相关推荐
xinzheng新政28 分钟前
纸板制造制胶工艺学习记录4
学习·制造
王者鳜錸30 分钟前
PYTHON让繁琐的工作自动化-猜数字游戏
python·游戏·自动化
若天明1 小时前
深度学习-计算机视觉-微调 Fine-tune
人工智能·python·深度学习·机器学习·计算机视觉·ai·cnn
倔强青铜三2 小时前
苦练Python第39天:海象操作符 := 的入门、实战与避坑指南
人工智能·python·面试
我们从未走散2 小时前
JVM学习笔记-----类加载
笔记·学习
一百天成为python专家3 小时前
Python循环语句 从入门到精通
开发语言·人工智能·python·opencv·支持向量机·计算机视觉
Sunhen_Qiletian3 小时前
朝花夕拾(五)--------Python 中函数、库及接口的详解
开发语言·python
前路不黑暗@4 小时前
C语言:操作符详解(二)
c语言·开发语言·经验分享·笔记·学习·学习方法·visual studio
三年呀4 小时前
标题:移动端安全加固:发散创新,筑牢安全防线引言:随着移动互联网
网络·python·安全
xiaoxiaoxiaolll4 小时前
金刚石基植入体新突破!Adv. Funct. Mater. 报道首例增材制造固态摩擦电能量收集器
学习