langchian_aws模块学习

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

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.

相关推荐

  1. langchian_aws模块学习

    2024-06-08 10:18:01       33 阅读
  2. 爬虫学习————request模块

    2024-06-08 10:18:01       32 阅读
  3. [python学习]--模块管理

    2024-06-08 10:18:01       34 阅读
  4. 模块与包及json模块学习

    2024-06-08 10:18:01       41 阅读
  5. TS学习4-模块

    2024-06-08 10:18:01       74 阅读
  6. 爬虫学习--3.Requests模块

    2024-06-08 10:18:01       27 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-08 10:18:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-08 10:18:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-08 10:18:01       87 阅读
  4. Python语言-面向对象

    2024-06-08 10:18:01       96 阅读

热门阅读

  1. npm发布自己的插件包

    2024-06-08 10:18:01       36 阅读
  2. npm发布自己的插件包

    2024-06-08 10:18:01       28 阅读
  3. ubantu安装第三库到指定目录

    2024-06-08 10:18:01       31 阅读
  4. C#面:AJAX的底层实现原理

    2024-06-08 10:18:01       31 阅读
  5. 类的定义和对象的引用

    2024-06-08 10:18:01       30 阅读
  6. c++ 左右值与引用折叠

    2024-06-08 10:18:01       29 阅读
  7. 【例0808】create daxis using face 使用面创建基准轴

    2024-06-08 10:18:01       31 阅读