题意:如何在使用本地 LLM 时将 OpenAI 函数转换为 LangChain 中的 PromptTemplate?
问题背景:
Is is possible to Convert openai functions to PromptTemplate in langchain when using local llms and return final output similar to openai api function format
是否可以在使用本地 LLM 时将 OpenAI 函数转换为 LangChain 中的 PromptTemplate,并返回类似于 OpenAI API 函数格式的最终输出?
import langchain
functions = [
{
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["location"],
},
}
]
llm = llm()
#Pass functions into prompt template in langchain
#Format prompt template similar to openai manner
问题解决:
I used openweather to get the temperature information. My tool is a function called get_current_weather which reaches a city, state and a unit. The unit is the metric or imperial results.
我使用 OpenWeather 获取温度信息。我的工具是一个名为 `get_current_weather` 的函数,它接收城市、州和单位作为参数。单位可以是公制或英制结果。
def get_current_weather(location, unit='celsius'):
units = 'metric' if unit == 'celsius' else 'imperial'
base_url = "http://api.openweathermap.org/data/2.5/weather"
params = {
"q": location,
"units": units,
"appid": weather_api_key # Replace with your actual API key
}
query_string = urllib.parse.urlencode(params)
url = f"{base_url}?{query_string}"
print(url)
response = requests.get(url)
if response.status_code == 200:
data = response.json()
print(data)
weather = {
'location': data['name'],
'temperature': data['main']['temp'],
'description': data['weather'][0]['description'],
'humidity': data['main']['humidity'],
'pressure': data['main']['pressure']
}
return weather
else:
return {'error': 'City not found or API limit exceeded'}
tools = [
Tool(
name="Get Weather",
func=get_current_weather,
description="Fetches the weather by location for a specific unit"
),
]
llm=ChatOpenAI(model="gpt-4o-mini",temperature=0, openai_api_key=key)
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)
response = agent.run({"input": "slc, ut", "unit": "celsius"})
print(response)