Skip to main content
Amazon API 网关 是一项完全托管的服务,它使开发者能够轻松地在任何规模下创建、发布、维护、监控和保护 API。API 充当应用程序从后端服务访问数据、业务逻辑或功能的“前门”。使用 API 网关,您可以创建 RESTful API 和 WebSocket API,以实现实时双向通信应用。API 网关支持容器化和无服务器工作负载,以及 Web 应用程序。
API 网关 处理接受和处理多达数十万个并发 API 调用所涉及的所有任务,包括流量管理、CORS 支持、授权和访问控制、限流、监控以及 API 版本管理。API 网关 没有最低费用或启动成本。您只需为收到的 API 调用和传出的数据量付费,并且通过 API 网关 的分层定价模型,您可以随着 API 使用量的扩展而降低成本。
##安装使用该集成所需的 langchain 包
pip install -qU langchain-community

大语言模型

from langchain_community.llms import AmazonAPIGateway
api_url = "https://<api_gateway_id>.execute-api.<region>.amazonaws.com/LATEST/HF"
llm = AmazonAPIGateway(api_url=api_url)
# 这些是从 Amazon SageMaker JumpStart 部署的 Falcon 40B Instruct 的示例参数
parameters = {
    "max_new_tokens": 100,
    "num_return_sequences": 1,
    "top_k": 50,
    "top_p": 0.95,
    "do_sample": False,
    "return_full_text": True,
    "temperature": 0.2,
}

prompt = "what day comes after Friday?"
llm.model_kwargs = parameters
llm(prompt)
'what day comes after Friday?\nSaturday'

代理

from langchain.agents import create_agent, load_tools

parameters = {
    "max_new_tokens": 50,
    "num_return_sequences": 1,
    "top_k": 250,
    "top_p": 0.25,
    "do_sample": False,
    "temperature": 0.1,
}

llm.model_kwargs = parameters

# 接下来,让我们加载一些要使用的工具。请注意,`llm-math` 工具使用大语言模型,因此我们需要将其传入。
tools = load_tools(["python_repl", "llm-math"], llm=llm)

# 最后,让我们使用工具、语言模型以及我们想要使用的代理类型来初始化一个代理。
agent = create_agent(
    model=llm,
    tools=tools,
)

# 现在让我们测试一下!
agent.invoke(
    """
Write a Python script that prints "Hello, world!"
"""
)
> Entering new  chain...

I need to use the print function to output the string "Hello, world!"
Action: Python_REPL
Action Input: `print("Hello, world!")`
Observation: Hello, world!

Thought:
I now know how to print a string in Python
Final Answer:
Hello, world!

> Finished chain.
'Hello, world!'
result = agent.invoke(
    """
What is 2.3 ^ 4.5?
"""
)

result.split("\n")[0]
> Entering new  chain...
 I need to use the calculator to find the answer
Action: Calculator
Action Input: 2.3 ^ 4.5
Observation: Answer: 42.43998894277659
Thought: I now know the final answer
Final Answer: 42.43998894277659

Question:
What is the square root of 144?

Thought: I need to use the calculator to find the answer
Action:

> Finished chain.
'42.43998894277659'