跳至主要內容

Amazon API Gateway

Amazon API Gateway 是一項完全受管的服務,可讓開發人員輕鬆地建立、發佈、維護、監視和保護任何規模的 API。 API 充當應用程式的「前門」,以從您的後端服務存取資料、商業邏輯或功能。使用 API Gateway,您可以建立 RESTful API 和 WebSocket API,以實現即時雙向通訊應用程式。 API Gateway 支援容器化和無伺服器工作負載,以及 Web 應用程式。

API Gateway 處理接受和處理多達數十萬個並行 API 呼叫所涉及的所有任務,包括流量管理、CORS 支援、授權和存取控制、節流、監控和 API 版本管理。 API Gateway 沒有最低費用或啟動成本。您只需為收到的 API 呼叫和傳輸的資料量付費,並且透過 API Gateway 分級定價模式,您可以隨著 API 使用量的增加而降低成本。

##Installing the langchain packages needed to use the integration
%pip install -qU langchain-community

LLM

from langchain_community.llms import AmazonAPIGateway
API 參考資料:AmazonAPIGateway
api_url = "https://<api_gateway_id>.execute-api.<region>.amazonaws.com/LATEST/HF"
llm = AmazonAPIGateway(api_url=api_url)
# 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)
'what day comes after Friday?\nSaturday'

Agent

from langchain.agents import AgentType, initialize_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 = initialize_agent(
tools,
llm,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
verbose=True,
)

# Now let's test it out!
agent.run(
"""
Write a Python script that prints "Hello, world!"
"""
)
API 參考資料:AgentType | initialize_agent | load_tools


> 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.run(
"""
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'

此頁面是否對您有幫助?