11---
22Description : " "
3- date : " 2025-02-11 "
3+ date : " 2025-03-19 "
44lastmod : " "
55tags : []
66title : ChatModel - Ollama
@@ -18,30 +18,35 @@ The Ollama model is an implementation of the ChatModel interface, designed for i
1818The Ollama model is initialized through the ` NewChatModel ` function. The main configuration parameters are as follows:
1919
2020``` go
21- import " github.com/cloudwego/eino-ext/components/model/ollama"
21+ import (
22+ " github.com/cloudwego/eino-ext/components/model/ollama"
23+ " github.com/ollama/ollama/api"
24+ )
2225
2326model , err := ollama.NewChatModel (ctx, &ollama.ChatModelConfig {
2427 // Basic Configuration
25- BaseURL : " http://localhost:11434" , // Ollama service address
26- Timeout : 30 * time.Second , // Request timeout
27-
28+ BaseURL : " http://localhost:11434" , // Ollama service address
29+ Timeout : 30 * time.Second , // Request timeout
30+
2831 // Model Configuration
2932 Model : " llama2" , // Model name
30- Format : " json" , // Output format (optional)
33+ Format : json. RawMessage ( " json" ), // Output format (optional)
3134 KeepAlive : &keepAlive, // Keep-alive time
32-
35+
3336 // Model Parameters
3437 Options : &api.Options {
35- Temperature: 0.7 , // Temperature
36- TopP: 0.9 , // Top-P sampling
37- TopK: 40 , // Top-K sampling
38- Seed: 42 , // Random seed
39- NumPredict: 100 , // Maximum generation length
40- Stop: []string {}, // Stop words
41- RepeatPenalty: 1.1 , // Repeat penalty
42- NumCtx: 4096 , // Context window size
43- NumGPU: 1 , // Number of GPUs
44- NumThread: 4 , // Number of CPU threads
38+ Runner: api.Runner {
39+ NumCtx: 4096 , // Context window size
40+ NumGPU: 1 , // Number of GPUs
41+ NumThread: 4 , // Number of CPU threads
42+ },
43+ Temperature: 0.7 , // Temperature
44+ TopP: 0.9 , // Top-P sampling
45+ TopK: 40 , // Top-K sampling
46+ Seed: 42 , // Random seed
47+ NumPredict: 100 , // Maximum generation length
48+ Stop: []string {}, // Stop words
49+ RepeatPenalty: 1.1 , // Repeat penalty
4550 },
4651})
4752```
@@ -51,26 +56,24 @@ model, err := ollama.NewChatModel(ctx, &ollama.ChatModelConfig{
5156Conversation generation supports both normal mode and streaming mode:
5257
5358``` go
54- func main () {
55- // Normal mode
56- response , err := model.Generate (ctx, messages)
59+ // Normal mode
60+ response , err := model.Generate (ctx, messages)
5761
58- // Streaming mode
59- stream , err := model.Stream (ctx, messages)
60- }
62+ // Streaming mode
63+ stream , err := model.Stream (ctx, messages)
6164```
6265
6366Example of message formats:
6467
6568``` go
66- func main () {
67- messages := []*schema.Message {
68- // System message
69- schema.SystemMessage (" You are an assistant" ),
69+ import " github.com/cloudwego/eino/schema"
70+
71+ messages := []*schema.Message {
72+ // System message
73+ schema.SystemMessage (" You are an assistant" ),
7074
71- // User message
72- schema.UserMessage (" Hello" )
73- }
75+ // User message
76+ schema.UserMessage (" Hello" )
7477}
7578```
7679
@@ -81,25 +84,25 @@ Supports binding tools:
8184> Note: Only models that support function call can use this capability
8285
8386``` go
84- func main () {
85- // Define tools
86- tools := []*schema.ToolInfo {
87- {
88- Name: " search" ,
89- Desc: " Search information" ,
90- ParamsOneOf: schema.NewParamsOneOfByParams (map [string ]*schema.ParameterInfo {
91- " query" : {
92- Type: schema.String ,
93- Desc: " Search keyword" ,
94- Required: true ,
95- },
96- }),
97- },
98- }
99-
100- // Bind tools
101- err := model.BindTools (tools)
87+ import " github.com/cloudwego/eino/schema"
88+
89+ // Define tools
90+ tools := []*schema.ToolInfo {
91+ {
92+ Name: " search" ,
93+ Desc: " Search information" ,
94+ ParamsOneOf: schema.NewParamsOneOfByParams (map [string ]*schema.ParameterInfo {
95+ " query" : {
96+ Type: schema.String ,
97+ Desc: " Search keyword" ,
98+ Required: true ,
99+ },
100+ }),
101+ },
102102}
103+
104+ // Bind tools
105+ err := model.BindTools (tools)
103106```
104107
105108### ** Complete Usage Example**
@@ -112,48 +115,43 @@ package main
112115import (
113116 " context"
114117 " time"
115-
118+
116119 " github.com/cloudwego/eino-ext/components/model/ollama"
117120 " github.com/cloudwego/eino/schema"
121+ " github.com/ollama/ollama/api"
118122)
119123
120124func main () {
121125 ctx := context.Background ()
122-
126+
123127 // Initialize model
124128 model , err := ollama.NewChatModel (ctx, &ollama.ChatModelConfig {
125- BaseURL: " http://localhost:11434" ,
126- Timeout: 30 * time.Second ,
127- Model: " llama2" ,
128- Options: &api.Options {
129- Temperature: 0.7 ,
130- NumPredict: 100 ,
131- },
129+ BaseURL: " http://localhost:11434" ,
130+ Timeout: 30 * time.Second ,
131+ Model: " llama2" ,
132+ Options: &api.Options {
133+ Temperature: 0.7 ,
134+ NumPredict: 100 ,
135+ },
132136 })
133137 if err != nil {
134- panic (err)
138+ panic (err)
135139 }
136-
140+
137141 // Prepare messages
138142 messages := []*schema.Message {
139- schema.SystemMessage (" You are an assistant" ),
140- schema.UserMessage (" Introduce Ollama" ),
143+ schema.SystemMessage (" You are an assistant" ),
144+ schema.UserMessage (" Introduce Ollama" ),
141145 }
142-
146+
143147 // Generate response
144148 response , err := model.Generate (ctx, messages)
145149 if err != nil {
146- panic (err)
150+ panic (err)
147151 }
148-
152+
149153 // Handle response
150154 println (response.Content )
151-
152- // Get performance metrics
153- if metrics , ok := response.ResponseMeta .Extra [" ollama_metrics" ].(api.Metrics ); ok {
154- println (" Evaluation time:" , metrics.EvalDuration )
155- println (" Total time:" , metrics.TotalDuration )
156- }
157155}
158156```
159157
0 commit comments