Ollama 的最新更新:工具使用

Ollama 的最新更新:工具使用

Barry Lv6

Ollama的最新更新工具使用

关于Ollama中函数调用的所有信息

就在几天前,Ollama宣布其工具调用功能已直接在Ollama中可用,加入了大多数LLM开发者的行列。

尽管Ollama并不是一个LLM开发者,更像是LLM的赋能者,但这对其粉丝来说是个好消息。

在本文中,我将向您展示如何使用Ollama的API和Python来使用工具。

如果您之前从未听说过Ollama,我建议您阅读下面的文章,我在其中介绍了他们及其所做的工作。

设置开发环境

在开始编码之前,最佳实践是设置一个新的开发环境,以保持我们的项目独立。我使用conda,但您可以使用任何您熟悉的工具。

1
2
3
4
PS C:\Users\thoma> conda create -n ollama_tools python=3.11 -y
PS C:\Users\thoma> conda activate ollama_tools
(ollama_tools) PS C:\Users\thoma> pip install jupyter yfinance ollama

安装Ollama

访问ollama.com 并下载适合您系统的最新版本的Ollama,然后进行安装。

检查您的版本。您需要至少0.3.0才能使用工具调用。

1
2
(ollama_tools) PS C:\Users\thoma> ollama --version
ollama version is 0.3.0

选择您的LLM模型并通过Ollama下载(拉取)它

有关Ollama中可用的兼容模型的列表,这些模型允许使用工具,请查看他们网站的这一部分。

一旦您决定使用哪个模型,请使用Ollama将其拉取(下载)到您的本地系统。例如,我选择使用Llama 3.1 8B模型。

1
(ollama_tools) PS C:\Users\thoma> ollama pull llama3.1

第一次执行此操作可能需要一两分钟。您可以通过以下命令检查您本地系统上拥有的模型:

1
(ollama_tools) PS C:\Users\thoma> ollama list

要运行模型(在与其交互之前需要这样做),请使用run命令,例如:

1
(ollama_tools) PS C:\Users\thoma> ollam run llama3.1:latest --keepalive 120m

一旦您到达**>>>**提示符,您可以直接退出,模型将在后台继续运行,在这种情况下,持续120分钟,然后自动卸载。

为什么我们需要使用工具?

我们需要使用工具的原因是,LLMs 通常在其知识中有一个截止日期。这意味着它们不知道当前的事件、情况等……除非它们可以直接访问互联网,而大多数是无法做到的。

因此,如果你问一个 LLM,爱丁堡当前的温度是多少,它将无法告诉你。对于某些事情,例如某个特定股票的当前价格,也是如此。

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
>>> what is the current price of MSFT (Microsoft)

As I'm a large language model, I don't have direct access to real-time market data. However, I can suggest some
ways for you to find out the current stock price of Microsoft (MSFT):

1. **Financial websites**: Visit reputable financial websites like:
* Yahoo Finance (finance.yahoo.com)
* Google Finance (finance.google.com)
* Bloomberg (bloomberg.com)
* CNBC (cnbc.com)
2. **Stock market apps**: Use a mobile app like:
* Robinhood
* Fidelity Investments
* eTrade
* TradingView
3. **Direct from the source**: Visit Microsoft's investor relations page (ir.microsoft.com) for real-time stock
data.

As of my knowledge cutoff, the current price of MSFT was around $245-$250 per share. However, please note that
this information may be outdated, and I recommend checking a reliable financial website or app for the most recent
and accurate price.

If you want to get an estimate, here are some historical price ranges:

* 52-week high: around $280
* 52-week low: around $210

Keep in mind that stock prices can fluctuate rapidly due to various market factors. Always consult multiple
sources or a financial advisor for more precise and up-to-date information.

示例代码 — 查找股票的当前价格

让我们来解决这个问题。

因此,为了回答我们的问题,我们首先需要一个函数来返回股票的当前价格。我们可以使用 Yahoo Finance 来实现这一点。

1
2
3
4
5
6
7
8
9
10
11
import yfinance as yf

def get_current_stock_price(ticker_symbol):

# 获取股票数据
stock = yf.Ticker(ticker_symbol)

# 获取当前价格
current_price = stock.history(period='1d')['Close'].iloc[0]

return current_price

现在,向 Ollama 指定工具(函数)。

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
31
import ollama

# 想要获取微软的当前价格 - 股票代码 MSFT

response = ollama.chat(
model='llama3.1',
messages=[{'role': 'user','content':
'What is the current price of MSFT'}],

# 提供一个工具来获取股票的当前价格
tools=[{
'type': 'function',
'function': {
'name': 'get_current_stock_price',
'description': '获取股票的当前价格',
'parameters': {
'type': 'object',
'properties': {
'ticker_symbol': {
'type': 'string',
'description': '股票的代码',
},
},
'required': ['ticker_symbol'],
},
},
},
],
)

print(response['message']['tool_calls'])

动态调用工具。

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
# 创建一个字典,将函数名称映射到函数
function_map = {
'get_current_stock_price': get_current_stock_price,
# 根据需要在此处添加更多函数
}

def call_function_safely(response, function_map):
# 从响应中提取函数名称和参数
tool_call = response['message']['tool_calls'][0]
function_name = tool_call['function']['name']
arguments = tool_call['function']['arguments']

# 在函数映射中查找函数
function_to_call = function_map.get(function_name)

if function_to_call:
try:
# 使用参数调用函数
result = function_to_call(**arguments)
print(f"{arguments['ticker_symbol']} 的当前价格是 : {result}")
except TypeError as e:
print(f"参数错误: {e}")
else:
print(f"{function_name} 不是一个被识别的函数")

call_function_safely(response, function_map)

这产生了以下输出,

1
MSFT 的当前价格是 : 427.4599914550781

对我来说看起来差不多,

好的,我现在就到此为止。希望你觉得这篇文章有用。如果有,请查看我在 这个链接 上的个人资料页面。在那里,你可以看到我其他的已发布故事,并订阅以便在我发布新内容时收到通知。

如果你喜欢这个内容,我认为你也会对这些文章感兴趣。

  • 标题: Ollama 的最新更新:工具使用
  • 作者: Barry
  • 创建于 : 2024-07-31 19:23:10
  • 更新于 : 2024-08-31 06:59:45
  • 链接: https://wx.role.fun/2024/07/31/884a225c58184358879132dffed78726/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。