本指南提供了快速入门 Amazon Nova 聊天模型 的概述。Amazon Nova 模型与 OpenAI 兼容,并通过指向 Nova 端点的 OpenAI SDK 访问,从而与 LangChain 的标准接口无缝集成。Amazon Nova API 提供免费层级,但有限制。对于需要更高吞吐量和企业功能的生产部署,请考虑通过 Amazon Bedrock 使用 Amazon Nova 模型。您可以在 Amazon Nova 文档 中找到有关 Amazon Nova 模型、其功能和 API 详细信息。
import getpassimport osif "NOVA_API_KEY" not in os.environ: os.environ["NOVA_API_KEY"] = getpass.getpass("Enter your Nova API key: ")if "NOVA_BASE_URL" not in os.environ: os.environ["NOVA_BASE_URL"] = getpass.getpass("Enter your Nova base URL: ")
messages = [ ( "system", "You are a helpful assistant that translates English to French. Translate the user sentence.", ), ("user", "I love programming."),]ai_msg = model.invoke(messages)ai_msg
from pydantic import BaseModel, Fieldclass GetWeather(BaseModel): """Get the weather for a location.""" location: str = Field(description="City name")model_with_tools = model.bind_tools([GetWeather])response = model_with_tools.invoke("What's the weather in Paris?")print(response.tool_calls)
from langchain.tools import tool@tooldef get_weather(location: str) -> str: """Get the weather for a location.""" return f"Weather in {location}: Sunny, 72°F"model_with_tools = model.bind_tools([get_weather])response = model_with_tools.invoke("What's the weather in San Francisco?")
from langchain_amazon_nova import ChatAmazonNovamodel_with_grounding = ChatAmazonNova( model="nova-2-lite-v1", system_tools=["nova_grounding"],)response = model_with_grounding.invoke("What are the latest developments in AI?")
from langchain_amazon_nova import ChatAmazonNovamodel_with_code = ChatAmazonNova( model="nova-2-lite-v1", system_tools=["nova_code_interpreter"],)response = model_with_code.invoke("Calculate the fibonacci sequence up to the 10th number")
from langchain_amazon_nova import ChatAmazonNovamodel_with_tools = ChatAmazonNova( model="nova-2-lite-v1", system_tools=["nova_grounding", "nova_code_interpreter"],)response = model_with_tools.invoke( "Search for the current price of Bitcoin and calculate its 7-day moving average")
from pydantic import BaseModel, Fieldclass Person(BaseModel): """Information about a person.""" name: str = Field(description="The person's name") age: int = Field(description="The person's age")structured_model = model.with_structured_output(Person)for chunk in structured_model.stream("Michael is 35 years old"): print(chunk)
from pydantic import BaseModel, Fieldfrom typing import Listclass Address(BaseModel): """A physical address.""" street: str city: str country: strclass Person(BaseModel): """Information about a person.""" name: str age: int addresses: List[Address] = Field(description="List of addresses")structured_model = model.with_structured_output(Person)result = structured_model.invoke( "John is 30 years old. He lives at 123 Main St in Seattle, USA " "and has a vacation home at 456 Beach Rd in Miami, USA.")print(result)
实现细节结构化输出在底层使用 Nova 的工具调用功能,带有 tool_choice='required' 以确保一致的结构化响应。schema 被转换为工具定义,工具调用响应被解析回请求的格式。
import asyncioasync def main(): messages = [ ("system", "You are a helpful assistant."), ("human", "What is the capital of France?"), ] response = await model.ainvoke(messages) print(response.content)asyncio.run(main())
Amazon Nova 模型与 LangChain 的 LCEL (LangChain Expression Language) 无缝协作以构建链:
from langchain_core.output_parsers import StrOutputParserfrom langchain_core.prompts import ChatPromptTemplateprompt = ChatPromptTemplate.from_messages([ ("system", "You are a helpful assistant that translates {input_language} to {output_language}."), ("human", "{text}"),])chain = prompt | model | StrOutputParser()result = chain.invoke({ "input_language": "English", "output_language": "Spanish", "text": "Hello, how are you?"})print(result)