顯示具有 tools 標籤的文章。 顯示所有文章
顯示具有 tools 標籤的文章。 顯示所有文章

2024年12月14日 星期六

Ollama的代理功能(Agent)

範例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from phi.agent import Agent
from phi.model.ollama import Ollama
from phi.tools.duckduckgo import DuckDuckGo

web_agent = Agent(
    name="Web Agent",
    model=Ollama(id="llama3.2"),
    tools=[DuckDuckGo()],
    instructions=["Always include sources"],
    show_tool_calls=True,
    markdown=True,
)
web_agent.print_response("告訴我有關於Ollama?", stream=True)

第1次執行結果:
┌─ Message ───────────────────────────────────────────────────────────────────┐
│                                                                             │
│ 告訴我有關於Ollama?                                                         │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘
┌─ Response (13.9s) ──────────────────────────────────────────────────────────┐
│                                                                             │
│                                                                             │
│  • Running: duckduckgo_news(max_results=5, query=ollama)                    │
│                                                                             │
│ Ollama 是一個由 Meta 開發的開源 Language                                    │
│ Model(LLM),其目的是提供更合理的對話和語言處理能力。與其他 OPEN Llama     │
│ 模型相比,Ollama                                                            │
│ 具有更強大的對話能力、更好的理解能力以及能夠更好地处理多種语言風格和風俗。  │
│                                                                             │
│ 2024 年 10 月 23 日,Ollama 的開發者宣布了它們的新功能,使得 Ollama         │
│ 可以在電腦上運行,不需要 internet 連線。這是 Ollama                         │
│ 達到新的里程碑,讓它更容易被使用和分享給更多人。                            │
│                                                                             │
│ 目前,Ollama 仍然是在開發中,但是它已经展示出了非常出色的表現。             │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘
第2次執行結果:
┌─ Message ───────────────────────────────────────────────────────────────────┐
│                                                                             │
│ 告訴我有關於Ollama?                                                         │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘
┌─ Response (17.4s) ──────────────────────────────────────────────────────────┐
│                                                                             │
│                                                                             │
│  • Running: duckduckgo_search(query=Ollama)                                 │
│                                                                             │
│ OLLAMA(Ollama)是一個開源的框架,用于建構和執行大型自然語言模型(Large     │
│ Language                                                                    │
│ Models,LLMs)。它提供了一種簡單的API,可以用來創造、執行和管理模型,也包含 │
│ 了一些預先啟動的模型,可用於多種應用程式。                                  │
│                                                                             │
│ OLLAMA可以用於以下功能:                                                    │
│                                                                             │
│  1 建構模型:使用Ollama的API可以建構新的模型。                              │
│  2 執行模型:使用Ollama的CLI工具可以執行已經建立好的模型。                  │
│  3 管理模型:Ollama也可以用來管理和 track 模型。                            │
│                                                                             │
│ OLLAMA還包括了一些預先啟動的模型,可用於多種應用程式,如:                   │
│                                                                             │
│  1 錯誤反饋:使用錯誤反饋模型來改善模型的表現。                             │
│  2 文本展開:使用文本展開模型來生成有用的內容。                             │
│  3 記事提取:使用記事提取模型來提取特定的信息。                             │
│                                                                             │
│ 對於研究人員、資料科學家和技術使用者,OLLAMA是一種很好的工具,可以讓他們更  │
│ flexibly 和有效地建構和執行大型自然語言模型。                               │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘

Ollama的工具(Tools)使用

Ollama Python函式庫:https://github.com/ollama/ollama-python

範例:加法和減法

 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from ollama import chat
from ollama import ChatResponse


def add_two_numbers(a: int, b: int) -> int:
  """
  Add two numbers

  Args:
    a (int): The first number
    b (int): The second number

  Returns:
    int: The sum of the two numbers
  """
  return int(a) + int(b)


def subtract_two_numbers(a: int, b: int) -> int:
  """
  Subtract two numbers
  """
  return int(a) - int(b)


# Tools can still be manually defined and passed into chat
subtract_two_numbers_tool = {
  'type': 'function',
  'function': {
    'name': 'subtract_two_numbers',
    'description': 'Subtract two numbers',
    'parameters': {
      'type': 'object',
      'required': ['a', 'b'],
      'properties': {
        'a': {'type': 'integer', 'description': 'The first number'},
        'b': {'type': 'integer', 'description': 'The second number'},
      },
    },
  },
}

messages = [{'role': 'user', 'content': '請問4扣3為?'}]
print('Prompt:', messages[0]['content'])

available_functions = {
  'add_two_numbers': add_two_numbers,
  'subtract_two_numbers': subtract_two_numbers,
}

response: ChatResponse = chat(
  'llama3.2',
  messages=messages,
  tools=[add_two_numbers, subtract_two_numbers_tool],
)

if response.message.tool_calls:
  # There may be multiple tool calls in the response
  for tool in response.message.tool_calls:
    # Ensure the function is available, and then call it
    if function_to_call := available_functions.get(tool.function.name):
      print('Calling function:', tool.function.name)
      print('Arguments:', tool.function.arguments)
      output = function_to_call(**tool.function.arguments)
      print('Function output:', output)
    else:
      print('Function', tool.function.name, 'not found')

# Only needed to chat with the model using the tool call results
if response.message.tool_calls:
  # Add the function response to messages for the model to use
  messages.append(response.message)
  messages.append({'role': 'tool', 'content': str(output), 'name': tool.function.name})

  # Get final response from model with function outputs
  final_response = chat('llama3.2', messages=messages)
  print('Final response:', final_response.message.content)

else:
  print('No tool calls returned from model')

第1次執行結果:
Prompt: 請問4扣3為?
Calling function: subtract_two_numbers
Arguments: {'a': '4', 'b': '3'}
Function output: 1
Final response: 答案是 1

第2次執行結果:
Prompt: 請問4扣3為?
Calling function: subtract_two_numbers
Arguments: {'a': '4', 'b': '3'}
Function output: 1
Final response: 4 - 3 = 1

2019年12月2日 星期一

工具簡介--highlight

#簡介
軟體:highlight3.54.1

操作說明 功能:將程式碼轉換成單純的html標籤

轉換模式:1. 檔案模式 2. 剪貼簿模式

#操作影片
#說明
 
1. Files:檔案處理模式
2. Preview預覽視窗
3. choose input files

輸入檔案,多個檔案同時轉換。
點選某一檔案,立即預覽。

4. 選定輸出資料夾
5. Convert files
開始轉換。
轉換結果,要到輸出資料夾去看。

6. copy file to clipboard
點選某一檔案,轉換結果,存到剪貼簿。

7. Add line number:

加行號,左邊是開始行號,右邊是幾位數。

8. Pad with zeroes

行號前空位補0

9. Omit header and footer:

只轉換必要標籤,頭尾如<html>或<body>等忽略不要。

  Clipboard:剪貼簿模式
1. 從剪貼簿將要轉換的內容貼進來,預覽視窗可見。

2. 將結果貼回剪貼簿

3. 以上兩者的合併成一個動作,也就是,不用預覽了。

4. 請看下一欄說明
 1. 在預覽視窗,先選取要轉換的內容

2. 才能打勾,再按Copy preview to clipboard

p.s.這部份影片中,漏了說明。
 勾選這個項目,網頁上才能照1. 2. 3. 排列整齊。

而且在複製的時候,才不會連行號都複製到。

如果不想要多產生一個css檔

1. 會在轉換結果的檔案前面,定義所有的css,然後再使用。

2. 這裡也打勾的話,css就直接寫在標籤裡面。