How to Convert openai functions to PromptTemplate in langchain when using local llms?

题意:如何在使用本地 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)

  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

营赢盈英

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值