+
95
-

OllamaFunctions如何使用?

OllamaFunctions如何使用?


网友回复

+
16
-

OllamaFunctions 是一个实验性的 Python 库,它将 Ollama 模型封装为与 OpenAI Functions 相同的 API。

这使得开发者可以利用 Ollama 模型来调用特定的函数,并以结构化的方式获取输出。这个库的设计目的是为了让 Ollama 模型能够执行类似于 OpenAI 的函数调用功能,从而支持更复杂的交互和数据处理任务。通过 OllamaFunctions,可以定义函数的参数和描述,并通过模型的函数调用能力来执行这些函数,返回符合预设模式的结构化数据。

安装依赖

pip install langchain_experimental

运行代码

import requests
from langchain_experimental.llms.ollama_functions import OllamaFunctions
from langchain_core.messages import HumanMessage, ToolMessage


def exec_result(result):
    for tool_call in result.tool_calls:
        selected_tool = {"get_current_weather": get_current_weather}[tool_call["name"].lower()]
        tool_output = selected_tool(tool_call["args"])
        
def get_current_weather(args):
    
    city=args['location']
    
    # 对城市名称进行URL编码
    city_encoded = requests.utils.quote(city)
    # API URL,包含城市名称参数
    api_url = f...

点击查看剩余70%

+
16
-

除了OllamaFunctions可以实现大模型调用函数插件功能外,大模型内置的提示词也可以实现

提示词

You have access to the following tools:
{function_to_json(get_weather)}
{function_to_json(calculate_mortgage_payment)}
{function_to_json(get_directions)}
{function_to_json(get_article_details)}

You must follow these instructions:
Always select one or more of the above tools based on the user ...

点击查看剩余70%

+
15
-

ollama客户端已经支持函数调用

pip install ollama

import json
import ollama
import asyncio


# Simulates an API call to get flight times
# In a real application, this would fetch data from a live database or API
def get_flight_times(departure: str, arrival: str) -> str:
  flights = {
    'NYC-LAX': {'departure': '08:00 AM', 'arrival': '11:30 AM', 'duration': '5h 30m'},
    'LAX-NYC': {'departure': '02:00 PM', 'arrival': '10:30 PM', 'duration': '5h 30m'},
    'LHR-JFK': {'departure': '10:00 AM', 'arrival': '01:00 PM', 'duration': '8h 00m'},
    'JFK-LHR': {'departure': '09:00 PM', 'arrival': '09:00 AM', 'duration': '7h 00m'},
    'CDG-DXB': {'departure': '11:00 AM', 'arrival': '08:00 PM', 'duration': '6h 00m'},
    'DXB-CDG': {'departure': '03:00 AM', 'arrival': '07:30 AM', 'duration': '7h 30m'},
  }

  key = f'{departure}-{arrival}'.upper()
  return json.dumps(flights.get(key, {'error': 'Flight not found'}))


async def run(model: str):
  client = ollama.AsyncClient()
  # Initialize conversation with a user query
  messag...

点击查看剩余70%

+
15
-

现在用ollama客户端可以实现function call了

import json
import ollama
import asyncio


# Simulates an API call to get flight times
# In a real application, this would fetch data from a live database or API
def get_flight_times(departure: str, arrival: str) -> str:
  flights = {
    'NYC-LAX': {'departure': '08:00 AM', 'arrival': '11:30 AM', 'duration': '5h 30m'},
    'LAX-NYC': {'departure': '02:00 PM', 'arrival': '10:30 PM', 'duration': '5h 30m'},
    'LHR-JFK': {'departure': '10:00 AM', 'arrival': '01:00 PM', 'duration': '8h 00m'},
    'JFK-LHR': {'departure': '09:00 PM', 'arrival': '09:00 AM', 'duration': '7h 00m'},
    'CDG-DXB': {'departure': '11:00 AM', 'arrival': '08:00 PM', 'duration': '6h 00m'},
    'DXB-CDG': {'departure': '03:00 AM', 'arrival': '07:30 AM', 'duration': '7h 30m'},
  }

  key = f'{departure}-{arrival}'.upper()
  return json.dumps(flights.get(key, {'error': 'Flight not found'}))


async def run(model: str):
  client = ollama.AsyncClient()
  # Initialize conversation with a user query
  messages =...

点击查看剩余70%

我知道答案,我要回答