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.

相关推荐
yaoh.wang几秒前
力扣(LeetCode) 58: 最后一个单词的长度 - 解法思路
python·程序人生·算法·leetcode·面试·职场和发展·跳槽
深蓝海拓3 分钟前
PySide6从0开始学习的笔记(七) 控件(Widget)之文字输入类控件
笔记·python·qt·学习·pyqt
free-elcmacom3 分钟前
机器学习高阶教程<4>因果机器学习:因果推断、可解释AI与科学发现的新革命
人工智能·python·机器学习·因果机器学习
smile_Iris4 分钟前
Day 41 早停策略和模型权重的保存
开发语言·python
重生之我在番茄自学网安拯救世界5 分钟前
网络安全中级阶段学习笔记(八):upload靶场实战(1-13关)-文件上传漏洞绕过1
笔记·学习·网络安全·文件上传漏洞·靶场实战
一过菜只因6 分钟前
Git入门学习
git·学习
MediaTea11 分钟前
Python:接口隔离原则(ISP)
开发语言·网络·python·接口隔离原则
承渊政道12 分钟前
C++学习之旅【C++内存管理、模板初阶以及STL简介】
c++·学习·visual studio
遇印记13 分钟前
java期末复习(构造方法和成员方法,重写和重载)
java·开发语言·学习
破烂pan15 分钟前
Elasticsearch 8.x + Python 官方客户端实战教程
python·elasticsearch