langchain-APIChain

使用LangChain的APIChain调用天气接口的API查询天气的示例

思路:
使用自己封装的LLM接口,确保最大长度不要太长,设定API的提示模板格式,防止太长时对prompt截断的时候只保留关键信息。
一共调用2次LLM模型,第一次根据问题,生成API的请求,然后调用API获取结果,第二次根据API的结果和问题,生成最终的答案。
代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from langchain.chains import APIChain
from langchain.prompts.prompt import PromptTemplate
from chat.paper_chat import PoeSageAPI
from langchain.chains.api import open_meteo_docs

# 使用API查询调用天气查询的接口示例
llm = PoeSageAPI(temperature=0,max_token=700)
API_RESPONSE_PROMPT_TEMPLATE = (
"""
You are given the below API Documentation:
{api_docs}
Using this documentation, generate the full API url to call for answering the user question.
You should build the API url in order to get a response that is as short as possible, while still getting the necessary information to answer the question. Pay attention to deliberately exclude any unnecessary pieces of data in the API call.
API url:{api_url}
Here is the response from the API:
{api_response}
Question:{question}
Summarize this response to answer the original question.
Summary:"""
)

MY_API_RESPONSE_PROMPT = PromptTemplate(
input_variables=["api_docs", "question", "api_url", "api_response"],
template=API_RESPONSE_PROMPT_TEMPLATE,
)

chain_new = APIChain.from_llm_and_api_docs(llm, api_docs=open_meteo_docs.OPEN_METEO_DOCS,api_response_prompt=MY_API_RESPONSE_PROMPT,verbose=True)

response = chain_new.run('What is the weather like right now in Munich, Germany in degrees Fahrenheit?')
print(response)

结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
> Entering new APIChain chain...
[
{
"POE": "poe",
"input_prompt": "You are given the below API Documentation:\nBASE URL: https://api.open-meteo.com/\n\nAPI Documentation\nThe API endpoint /v1/forecast accepts a geographical coordinate, a list of weather variables and responds with a JSON hourly weather forecast for 7 days. Time always starts at 0:00 today and contains 168 hours. All URL parameters are listed below:\n\nParameter\tFormat\tRequired\tDefault\tDescription\nlatitude, longitude\tFloating point\tYes\t\tGeographical WGS84 coordinate of the location\nhourly\tString array\tNo\t\tA list of weather variables which should be returned. Values can be comma separated, or multiple &hourly= parameter in the URL can be used.\ndaily\tString array\tNo\t\tA list of daily weather variable aggregations which should be returned. Values can be comma separated, or multiple &daily= parameter in the URL can be used. If daily weather variables are specified, parameter timezone is required.\ncurrent_weather\tBool\tNo\tfalse\tInclude current weather conditions in the JSON output.\ntemperature_unit\tString\tNo\tcelsius\tIf fahrenheit is set, all temperature values are converted to Fahrenheit.\nwindspeed_unit\tString\tNo\tkmh\tOther wind speed speed units: ms, mph and kn\nprecipitation_unit\tString\tNo\tmm\tOther precipitation amount units: inch\ntimeformat\tString\tNo\tiso8601\tIf format unixtime is selected, all time values are returned in UNIX epoch time in seconds. Please note that all timestamp are in GMT+0! For daily values with unix timestamps, please apply utc_offset_seconds again to get the correct date.\ntimezone\tString\tNo\tGMT\tIf timezone is set, all timestamps are returned as local-time and data is returned starting at 00:00 local-time. Any time zone name from the time zone database is supported. If auto is set as a time zone, the coordinates will be automatically resolved to the local time zone.\npast_days\tInteger (0-2)\tNo\t0\tIf past_days is set, yesterday or the day before yesterday data are also returned.\nstart_date\nend_date\tString (yyyy-mm-dd)\tNo\t\tThe time interval to get weather data. A day must be specified as an ISO8601 date (e.g. 2022-06-30).\nmodels\tString array\tNo\tauto\tManually select one or more weather models. Per default, the best suitable weather models will be combined.\n\nHourly Parameter Definition\nThe parameter &hourly= accepts the following values. Most weather variables are given as an instantaneous value for the indicated hour. Some variables like precipitation are calculated from the preceding hour as an average or sum.\n\nVariable\tValid time\tUnit\tDescription\ntemperature_2m\tInstant\t°C (°F)\tAir temperature at 2 meters above ground\nsnowfall\tPreceding hour sum\tcm (inch)\tSnowfall amount of the preceding hour in centimeters. For the water equivalent in millimeter, divide by 7. E.g. 7 cm snow = 10 mm precipitation water equivalent\nrain\tPreceding hour sum\tmm (inch)\tRain from large scale weather systems of the preceding hour in millimeter\nshowers\tPreceding hour sum\tmm (inch)\tShowers from convective precipitation in millimeters from the preceding hour\nweathercode\tInstant\tWMO code\tWeather condition as a numeric code. Follow WMO weather interpretation codes. See table below for details.\nsnow_depth\tInstant\tmeters\tSnow depth on the ground\nfreezinglevel_height\tInstant\tmeters\tAltitude above sea level of the 0°C level\nvisibility\tInstant\tmeters\tViewing distance in meters. Influenced by low clouds, humidity and aerosols. Maximum visibility is approximately 24 km.\nUsing this documentation, generate the full API url to call for answering the user question.\nYou should build the API url in order to get a response that is as short as possible, while still getting the necessary information to answer the question. Pay attention to deliberately exclude any unnecessary pieces of data in the API call.\n\nQuestion:What is the weather like right now in Munich, Germany in degrees Fahrenheit?\nAPI url:",
"input_text": "",
"response": "https://api.open-meteo.com/v1/forecast?latitude=48.137154&longitude=11.576124&hourly=temperature_2m&current_weather=true&temperature_unit=fahrenheit"
}
]
https://api.open-meteo.com/v1/forecast?latitude=48.137154&longitude=11.576124&hourly=temperature_2m&current_weather=true&temperature_unit=fahrenheit
{"latitude":48.14,"longitude":11.58,"generationtime_ms":0.2110004425048828,"utc_offset_seconds":0,"timezone":"GMT","timezone_abbreviation":"GMT","elevation":526.0,"current_weather":{"temperature":54.3,"windspeed":12.3,"winddirection":75.0,"weathercode":0,"is_day":1,"time":"2023-06-13T06:00"},"hourly_units":{"time":"iso8601","temperature_2m":"°F"},"hourly":{"time":["2023-06-13T00:00","2023-06-13T01:00","2023-06-13T02:00","2023-06-13T03:00","2023-06-13T04:00","2023-06-13T05:00","2023-06-13T06:00","2023-06-13T07:00","2023-06-13T08:00","2023-06-13T09:00","2023-06-13T10:00","2023-06-13T11:00","2023-06-13T12:00","2023-06-13T13:00","2023-06-13T14:00","2023-06-13T15:00","2023-06-13T16:00","2023-06-13T17:00","2023-06-13T18:00","2023-06-13T19:00","2023-06-13T20:00","2023-06-13T21:00","2023-06-13T22:00","2023-06-13T23:00","2023-06-14T00:00","2023-06-14T01:00","2023-06-14T02:00","2023-06-14T03:00","2023-06-14T04:00","2023-06-14T05:00","2023-06-14T06:00","2023-06-14T07:00","2023-06-14T08:00","2023-06-14T09:00","2023-06-14T10:00","2023-06-14T11:00","2023-06-14T12:00","2023-06-14T13:00","2023-06-14T14:00","2023-06-14T15:00","2023-06-14T16:00","2023-06-14T17:00","2023-06-14T18:00","2023-06-14T19:00","2023-06-14T20:00","2023-06-14T21:00","2023-06-14T22:00","2023-06-14T23:00","2023-06-15T00:00","2023-06-15T01:00","2023-06-15T02:00","2023-06-15T03:00","2023-06-15T04:00","2023-06-15T05:00","2023-06-15T06:00","2023-06-15T07:00","2023-06-15T08:00","2023-06-15T09:00","2023-06-15T10:00","2023-06-15T11:00","2023-06-15T12:00","2023-06-15T13:00","2023-06-15T14:00","2023-06-15T15:00","2023-06-15T16:00","2023-06-15T17:00","2023-06-15T18:00","2023-06-15T19:00","2023-06-15T20:00","2023-06-15T21:00","2023-06-15T22:00","2023-06-15T23:00","2023-06-16T00:00","2023-06-16T01:00","2023-06-16T02:00","2023-06-16T03:00","2023-06-16T04:00","2023-06-16T05:00","2023-06-16T06:00","2023-06-16T07:00","2023-06-16T08:00","2023-06-16T09:00","2023-06-16T10:00","2023-06-16T11:00","2023-06-16T12:00","2023-06-16T13:00","2023-06-16T14:00","2023-06-16T15:00","2023-06-16T16:00","2023-06-16T17:00","2023-06-16T18:00","2023-06-16T19:00","2023-06-16T20:00","2023-06-16T21:00","2023-06-16T22:00","2023-06-16T23:00","2023-06-17T00:00","2023-06-17T01:00","2023-06-17T02:00","2023-06-17T03:00","2023-06-17T04:00","2023-06-17T05:00","2023-06-17T06:00","2023-06-17T07:00","2023-06-17T08:00","2023-06-17T09:00","2023-06-17T10:00","2023-06-17T11:00","2023-06-17T12:00","2023-06-17T13:00","2023-06-17T14:00","2023-06-17T15:00","2023-06-17T16:00","2023-06-17T17:00","2023-06-17T18:00","2023-06-17T19:00","2023-06-17T20:00","2023-06-17T21:00","2023-06-17T22:00","2023-06-17T23:00","2023-06-18T00:00","2023-06-18T01:00","2023-06-18T02:00","2023-06-18T03:00","2023-06-18T04:00","2023-06-18T05:00","2023-06-18T06:00","2023-06-18T07:00","2023-06-18T08:00","2023-06-18T09:00","2023-06-18T10:00","2023-06-18T11:00","2023-06-18T12:00","2023-06-18T13:00","2023-06-18T14:00","2023-06-18T15:00","2023-06-18T16:00","2023-06-18T17:00","2023-06-18T18:00","2023-06-18T19:00","2023-06-18T20:00","2023-06-18T21:00","2023-06-18T22:00","2023-06-18T23:00","2023-06-19T00:00","2023-06-19T01:00","2023-06-19T02:00","2023-06-19T03:00","2023-06-19T04:00","2023-06-19T05:00","2023-06-19T06:00","2023-06-19T07:00","2023-06-19T08:00","2023-06-19T09:00","2023-06-19T10:00","2023-06-19T11:00","2023-06-19T12:00","2023-06-19T13:00","2023-06-19T14:00","2023-06-19T15:00","2023-06-19T16:00","2023-06-19T17:00","2023-06-19T18:00","2023-06-19T19:00","2023-06-19T20:00","2023-06-19T21:00","2023-06-19T22:00","2023-06-19T23:00"],"temperature_2m":[57.8,56.2,54.1,52.4,51.0,51.5,54.3,58.3,62.1,65.2,67.7,69.8,71.3,72.2,72.4,72.3,71.8,70.1,68.3,65.7,62.6,60.0,57.8,56.3,55.1,53.9,52.9,52.2,51.2,52.1,55.5,59.5,63.0,67.0,68.7,69.9,71.3,71.8,72.2,72.0,71.8,70.1,68.2,66.1,63.7,61.8,60.2,58.2,57.2,55.5,54.7,53.7,52.4,54.6,58.0,61.6,64.8,67.9,68.6,69.2,71.0,71.8,72.9,72.8,71.9,71.1,69.7,66.5,63.5,61.8,60.6,59.3,58.1,56.8,56.0,55.4,54.6,56.4,60.2,63.0,66.2,68.7,70.2,71.1,71.6,71.8,71.5,71.0,70.2,69.3,67.9,65.6,62.9,60.7,59.8,59.5,59.1,57.9,56.5,55.8,56.4,57.6,59.4,62.0,64.9,67.5,69.3,70.6,71.6,72.2,72.5,72.3,71.8,70.8,69.2,66.7,63.8,61.2,59.6,58.6,57.9,55.1,53.8,53.6,55.2,58.0,61.1,65.0,69.3,72.8,74.9,76.1,77.0,77.9,78.4,78.4,77.9,77.0,75.2,72.4,68.8,65.8,64.0,62.8,61.7,60.4,59.2,59.0,60.1,62.2,64.8,68.4,72.5,76.1,78.8,80.8,82.4,83.5,84.2,84.2,83.3,81.8,79.7,76.2,72.2,68.9,67.2,66.2]}}
[
{
"POE": "poe",
"input_prompt": "15:00\",\"2023-06-17T16:00\",\"2023-06-17T17:00\",\"2023-06-17T18:00\",\"2023-06-17T19:00\",\"2023-06-17T20:00\",\"2023-06-17T21:00\",\"2023-06-17T22:00\",\"2023-06-17T23:00\",\"2023-06-18T00:00\",\"2023-06-18T01:00\",\"2023-06-18T02:00\",\"2023-06-18T03:00\",\"2023-06-18T04:00\",\"2023-06-18T05:00\",\"2023-06-18T06:00\",\"2023-06-18T07:00\",\"2023-06-18T08:00\",\"2023-06-18T09:00\",\"2023-06-18T10:00\",\"2023-06-18T11:00\",\"2023-06-18T12:00\",\"2023-06-18T13:00\",\"2023-06-18T14:00\",\"2023-06-18T15:00\",\"2023-06-18T16:00\",\"2023-06-18T17:00\",\"2023-06-18T18:00\",\"2023-06-18T19:00\",\"2023-06-18T20:00\",\"2023-06-18T21:00\",\"2023-06-18T22:00\",\"2023-06-18T23:00\",\"2023-06-19T00:00\",\"2023-06-19T01:00\",\"2023-06-19T02:00\",\"2023-06-19T03:00\",\"2023-06-19T04:00\",\"2023-06-19T05:00\",\"2023-06-19T06:00\",\"2023-06-19T07:00\",\"2023-06-19T08:00\",\"2023-06-19T09:00\",\"2023-06-19T10:00\",\"2023-06-19T11:00\",\"2023-06-19T12:00\",\"2023-06-19T13:00\",\"2023-06-19T14:00\",\"2023-06-19T15:00\",\"2023-06-19T16:00\",\"2023-06-19T17:00\",\"2023-06-19T18:00\",\"2023-06-19T19:00\",\"2023-06-19T20:00\",\"2023-06-19T21:00\",\"2023-06-19T22:00\",\"2023-06-19T23:00\"],\"temperature_2m\":[57.8,56.2,54.1,52.4,51.0,51.5,54.3,58.3,62.1,65.2,67.7,69.8,71.3,72.2,72.4,72.3,71.8,70.1,68.3,65.7,62.6,60.0,57.8,56.3,55.1,53.9,52.9,52.2,51.2,52.1,55.5,59.5,63.0,67.0,68.7,69.9,71.3,71.8,72.2,72.0,71.8,70.1,68.2,66.1,63.7,61.8,60.2,58.2,57.2,55.5,54.7,53.7,52.4,54.6,58.0,61.6,64.8,67.9,68.6,69.2,71.0,71.8,72.9,72.8,71.9,71.1,69.7,66.5,63.5,61.8,60.6,59.3,58.1,56.8,56.0,55.4,54.6,56.4,60.2,63.0,66.2,68.7,70.2,71.1,71.6,71.8,71.5,71.0,70.2,69.3,67.9,65.6,62.9,60.7,59.8,59.5,59.1,57.9,56.5,55.8,56.4,57.6,59.4,62.0,64.9,67.5,69.3,70.6,71.6,72.2,72.5,72.3,71.8,70.8,69.2,66.7,63.8,61.2,59.6,58.6,57.9,55.1,53.8,53.6,55.2,58.0,61.1,65.0,69.3,72.8,74.9,76.1,77.0,77.9,78.4,78.4,77.9,77.0,75.2,72.4,68.8,65.8,64.0,62.8,61.7,60.4,59.2,59.0,60.1,62.2,64.8,68.4,72.5,76.1,78.8,80.8,82.4,83.5,84.2,84.2,83.3,81.8,79.7,76.2,72.2,68.9,67.2,66.2]}}\nQuestion:What is the weather like right now in Munich, Germany in degrees Fahrenheit?\nSummarize this response to answer the original question.\nSummary:",
"input_text": "",
"response": "The provided data includes a list of dates and times along with corresponding temperature readings in degrees Celsius for Munich, Germany. However, the data does not provide the current temperature or convert the Celsius readings to Fahrenheit. Therefore, it is not possible to determine the current weather in Munich, Germany in degrees Fahrenheit based on this data alone."
}
]

> Finished chain.
The provided data includes a list of dates and times along with corresponding temperature readings in degrees Celsius for Munich, Germany. However, the data does not provide the current temperature or convert the Celsius readings to Fahrenheit. Therefore, it is not possible to determine the current weather in Munich, Germany in degrees Fahrenheit based on this data alone.

langchain-APIChain
https://johnson7788.github.io/2023/06/13/langchain-APIChain/
作者
Johnson
发布于
2023年6月13日
许可协议