> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-zh.cn/llms.txt
> Use this file to discover all available pages before exploring further.

# Amazon API Gateway 集成

> 使用 LangChain Python 与 Amazon API Gateway LLM 集成。

> [Amazon API Gateway](https://aws.amazon.com/api-gateway/) 是一项完全托管的服务，使开发人员能够轻松创建、发布、维护、监控和保护 API 在任意 >规模。API 充当应用程序访问后端服务中的数据、业务逻辑或功能的“入口”。使用 `API Gateway`，您可以创建 RESTful API 和 >WebSocket API 以启用支持实时双向通信的应用程序。API Gateway 支持容器化和无服务器工作负载，以及 Web 应用程序。

> `API Gateway` 处理接受和处理多达数十万并发 API 调用的所有任务，包括流量管理、CORS 支持、授权和访问控制、限流、监控和 API 版本管理。`API Gateway` 没有最低费用或启动成本。您只需为接收到的 API 调用量和传输出的数据量付费，并且通过 `API Gateway` 的分层定价模型，随着您的 API 使用量扩展，您可以降低成本。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
##Installing the langchain packages needed to use the integration
pip install -qU langchain-community
```

## LLM

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_community.llms import AmazonAPIGateway
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
api_url = "https://<api_gateway_id>.execute-api.<region>.amazonaws.com/LATEST/HF"
llm = AmazonAPIGateway(api_url=api_url)
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
# These are sample parameters for Falcon 40B Instruct Deployed from Amazon SageMaker JumpStart
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)
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
'what day comes after Friday?\nSaturday'
```

## Agent

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
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

# Next, let's load some tools to use. Note that the `llm-math` tool uses an LLM, so we need to pass that in.
tools = load_tools(["python_repl", "llm-math"], llm=llm)

# Finally, let's initialize an agent with the tools, the language model, and the type of agent we want to use.
agent = create_agent(
    model=llm,
    tools=tools,
)

# Now let's test it out!
agent.invoke(
    """
Write a Python script that prints "Hello, world!"
"""
)
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
> 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.
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
'Hello, world!'
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
result = agent.invoke(
    """
What is 2.3 ^ 4.5?
"""
)

result.split("\n")[0]
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
> 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.
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
'42.43998894277659'
```

***

<div className="source-links">
  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/i18n\zh-CN\oss\python\integrations\llms\amazon_api_gateway.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>

  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>
</div>
