奥本 AI 将所有的最新 AI 聚合到了一起,为您开启多模型 AI 绘画新时代!

扫码即刻关注公众号

Ollama 最新更新:工具使用


6 个月前

AI生成的图片(Dalle-3)

Ollama的最新更新:工具使用

在Ollama的最新更新中,我们引入了工具使用功能,这将极大地扩展Ollama的能力,使其能够处理更复杂的任务和挑战。以下是更新的主要内容:

1. 工具使用概述

工具使用是指Ollama能够识别、选择和使用各种工具来完成特定任务。这包括但不限于:

  • 选择正确的工具来解决问题
  • 根据任务需求调整工具的使用方式
  • 学习如何更有效地使用工具

2. 工具识别

Ollama现在能够识别各种工具,包括:

  • 手动工具(如锤子、螺丝刀等)
  • 电动工具(如电钻、砂轮机等)
  • 数字工具(如软件、应用程序等)

3. 工具选择

根据任务的具体需求,Ollama能够选择最合适的工具来完成任务。这包括:

  • 评估任务的复杂性和所需的技能水平
  • 考虑工具的可用性和成本效益
  • 选择最适合任务的工具

4. 工具使用技巧

Ollama已经学习了如何更有效地使用工具,包括:

- 学习正确的工具握持和操作方法

你需要了解的关于Ollama中函数调用的所有事情

在Ollama编程语言中,函数调用是执行程序中一个重要部分。以下是一些关键点,帮助你理解Ollama中的函数调用:

1. 函数定义

首先,你需要定义一个函数。在Ollama中,你可以使用fn关键字来定义一个函数。例如:

ollama fn add(a, b) { return a + b; }

2. 函数调用

一旦你定义了一个函数,你可以在程序的任何地方调用它。调用函数时,你需要使用函数名后跟一对圆括号,括号内包含参数列表。例如:

ollama result = add(3, 4);

3. 参数和返回值

函数可以有零个或多个参数,并且可以返回一个值。在上面的例子中,add函数有两个参数ab,并且返回它们的和。

4. 作用域

函数有自己的局部作用域。这意味着在函数内部定义的变量在函数外部是不可见的。

5. 递

就在几天前,Ollama加入了大多数大型语言模型(LLM)开发者的行列,宣布Ollama内部直接提供了工具调用的使用。 尽管Ollama并不是一个LLM(大型语言模型)的开发者,更像是LLM的使能者,但这个消息对于它的粉丝来说仍然是个好消息。 在这篇文章中,我将向您展示如何使用Ollama的API和Python来使用工具。 如果你之前没有听说过Ollama,我建议你阅读下面的文章,在那里我介绍了他们以及他们的工作。 [Ollama 介绍 — 第一部分]

在本地运行LLM的Ollama使用指南

Ollama是一个轻量级的、高性能的LLM(Large Language Models,大型语言模型)运行环境,它允许用户在本地计算机上运行大型语言模型,而无需依赖于远程服务器或云服务。以下是使用Ollama在本地运行LLM的步骤:

  1. 安装Ollama: 首先,您需要在您的计算机上安装Ollama。您可以从Ollama的官方网站下载安装包,并按照安装指南进行安装。

  2. 选择LLM模型: 选择一个适合您需求的大型语言模型。Ollama支持多种模型,包括但不限于GPT、BERT等。

  3. 下载模型文件: 从模型提供者的网站下载所需的LLM模型文件。这些文件通常很大,可能需要一些时间来下载。

  4. 配置Ollama: 在Ollama中配置模型文件的路径,以及其他必要的设置,如内存分配、处理器使用等。

  5. 运行LLM模型: 使用Ollama提供的命令行界面或图形用户界面启动LLM模型。您可以输入文本或指令,模型将根据输入生成响应。

6. gitconnected.com

https://levelup.gitconnected.com/introduction-to-ollama-part-1-1156f9563b8d)搭建开发环境 在开始编码之前,最佳实践是设置一个新的开发环境以保持我们的项目隔离。我使用conda来实现这一点,但您可以使用您熟悉的任何工具。 ``` PS C:Users homa> conda create -n ollama_tools python=3.11 -y PS C:Users homa> conda activate ollama_tools (ollama_tools) PS C:Users homa> pip install jupyter yfinance ollama

```

安装Ollama 导航到

ollama.com

下载适用于您系统的Ollama最新版本,然后进行安装。 检查您的版本。您至少需要0.3.0才能使用工具调用。 (ollama_tools) PS C:Users homa> ollama --version ollama version is 0.3.0

选择您的LLM模型并通过Ollama下载(拉取)它 对于Ollama中允许使用工具的兼容模型列表,请查看他们网站的这个部分, [Ollama]

快速开始使用大型语言模型。

ollama.com

https://ollama.com/search?c=tools)一旦你决定使用哪种模型,使用Ollama将其(下载)到你的本地系统。例如,我选择使用Llama 3.1 8B模型。 (ollama_tools) PS C:Users homa> ollama pull llama3.1

第一次这样做可能需要一两分钟。你可以通过以下方式检查你的本地系统上有哪些模型: (ollama_tools) PS C:Users homa> ollama list

要运行一个模型,这是在与之交互之前需要做的,使用例如 run 命令。 (ollama_tools) PS C:Users homa> ollam run llama3.1:latest --keepalive 120m

一旦你到达 >>> 提示符,你可以直接退出,模型将在后台继续运行,在这种情况下,将持续运行120分钟,然后自动卸载。 为什么我们需要使用工具?

我们需要使用工具的原因是,大型语言模型(LLMs)通常在其知识库中有一个截止日期。这意味着它们不了解当前事件、情况等……除非它们能够直接访问互联网,但大多数模型都不能。 所以,如果你问一个大型语言模型(LLM)当前爱丁堡的温度,它无法告诉你。同样的情况也适用于询问特定股票的当前价格等事情。 ```

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. ```

```markdown

Example Code: Find the Current Price of a Stock

To find the current price of a stock, you can use various financial APIs or libraries. Here's an example using the yfinance library in Python to fetch the current price of a stock.

Prerequisites

  • Install yfinance library if you haven't already: bash pip install yfinance

Python Code

```python import yfinance as yf

def get_current_stock_price(stock_symbol): """ This function fetches the current price of a given stock symbol.

:param stock_symbol: str - The stock symbol to fetch the price for.
:return: float - The current price of the stock.
"""
# Fetch the stock data
stock_data = yf.Ticker(stock_symbol)

# Get the current price
current_price = stock_data.info['regularMarketPrice']

return current_price

Example usage:

if name == "main": stock_symbol = "AAPL" # Apple Inc. price = get_current_stock_price(stock_symbol) print(f"The current price of {stock_symbol} is: ${price}") ``` This code snippet

让我们来解决这个问题。 那么,为了回答我们的问题,我们首先需要一个函数来返回股票的当前价格。我们可以使用雅虎财经来实现这一点。 ``` import yfinance as yf

def get_current_stock_price(ticker_symbol):

# Get the stock data stock = yf.Ticker(ticker_symbol)

# Get the current price current_price = stock.history(period='1d')['Close'].iloc[0]

return current_price ```

现在,请指定工具(功能)给Ollama。 ``` import ollama

Want the current price of Microsoft - ticker symbol MSFT

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

# provide a tool to get the current price of a stock tools=[{ 'type': 'function', 'function': { 'name': 'get_current_stock_price', 'description': 'Get the current price for a stock', 'parameters': { 'type': 'object', 'properties': { 'ticker_symbol': { 'type': 'string', 'description': 'The ticker symbol of the stock', }, }, 'required': ['ticker_symbol'], }, }, }, ], )

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

动态调用工具。 ```

Create a dictionary mapping function names to functions

function_map = { 'get_current_stock_price': get_current_stock_price, # Add more functions here as needed }

def call_function_safely(response, function_map): # Extract the function name and arguments from the response tool_call = response['message']['tool_calls'][0] function_name = tool_call['function']['name'] arguments = tool_call['function']['arguments']

# Look up the function in the function map
function_to_call = function_map.get(function_name)

if function_to_call:
    try:
        # Call the function with the arguments
        result = function_to_call(**arguments)
        print(f"The current price of {arguments['ticker_symbol']} is : {result}")
    except TypeError as e:
        print(f"Argument error: {e}")
else:
    print(f"{function_name} is not a recognized function")

call_function_safely(response, function_map) ```

这产生了以下输出, The current price of MSFT is : 427.4599914550781

看起来对我来说已经足够接近了,

图片来自必应

*好的,这就是我现在所有的内容。希望你觉得这篇文章有用。如果你觉得有用,请查看我的个人资料页面在

this link

从那里,你可以看到我其他发表的故事,并订阅以获取我发布新内容时的通知。* 如果您喜欢这篇文章,我认为您也会对以下文章感兴趣。 [Python的并行范式转变]

探索无GIL Python的性能潜力

在Python编程语言中,全局解释器锁(GIL)是一个长期存在的问题,它限制了多线程程序的性能,因为GIL确保了同一时刻只有一个线程可以执行Python字节码。然而,Python社区一直在探索GIL-free(无GIL)的实现,以期提高并行计算的性能。

1. GIL的背景和问题

GIL是Python解释器的一个关键组件,它允许CPython(Python的标准实现)在多线程环境中安全地执行。但是,GIL也带来了一些性能瓶颈,尤其是在CPU密集型任务中。当多个线程尝试同时执行时,GIL会导致线程争用和上下文切换,从而降低程序的整体性能。

2. 无GIL的Python实现

为了克服GIL的限制,一些Python实现,如Jython和IronPython,已经采用了无GIL的设计。此外,CPython的一些分支,例如PyPy,也在尝试实现无GIL的版本。这些实现通过不同的内存管理和线程调度策略来提高并行性。

3. 性能提升的潜力

无GIL的Python实现可以显著提高多线程程序的性能。在某些 Towards Data Science

https://towardsdatascience.com/pythons-parallel-paradigm-shift-2bb40d3c2dd0)[PySpark 详解:用户定义函数]

它们是什么,以及如何使用它们?

Towards Data Science

https://towardsdatascience.com/pyspark-explained-user-defined-functions-40a71f924b1a)

上海赋迪网络科技

电话:18116340052