ollama

package
v0.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 26, 2026 License: None detected not legal advice Imports: 0 Imported by: 0

Documentation

Overview

Package ollama 实现 Ollama 本地模型服务的 ModelProvider.

Ollama 在本地运行开源 LLM(Llama,DeepSeek,Qwen,Gemma 等), 提供 OpenAI 兼容的 Chat Completions API.

特点:

  • 无需 API Key(本地服务无鉴权)
  • 通过 /api/tags 端点实时获取已安装的本地模型列表
  • 不支持 Thinking(本地模型通常不支持结构化推理输出)
  • 不支持 Prompt Caching 和 Batch API

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// BaseURL 是 Ollama 服务地址(默认 http://localhost:11434).
	// 远程 Ollama 实例时填写对应地址,如 "http://192.168.1.100:11434".
	BaseURL string

	// HTTPClient 注入自定义 HTTP 客户端(超时配置等).
	// 本地模型推理可能较慢,建议设置较长的超时时间.
	// nil = 使用默认 http.Client (带 Timeout 字段配置的 ResponseHeaderTimeout).
	// 非 nil 时 consumer 完全接管超时责任, 下面的 Timeout 字段被忽略.
	HTTPClient *http.Client

	// Timeout 限制"从请求发出到收到响应首字节"的时间.
	//
	// 通过 http.Transport.ResponseHeaderTimeout 实现, **不影响** SSE 流式 body 读取.
	// 精妙之处(CLEVER): 不要误用 http.Client.Timeout - 会砍死 SSE 流.
	//
	// 0 = 使用 defaultTimeout (300s, 适合本地推理冷启动).
	// Ollama 默认比云端长 5 倍: 本地模型 (尤其是第一次调用某个模型) 需要从磁盘加载
	// 权重 + warm-up, 可能耗时 30-60s 才开始响应.云端 60s 默认值在 Ollama 会频繁超时.
	Timeout time.Duration
}

Config 是 Ollama provider 的配置.

func (Config) GoString

func (c Config) GoString() string

GoString 实现 fmt.GoStringer,防止 %#v 打印时意外暴露配置详情. 升华改进(ELEVATED): Ollama 本地部署无 APIKey,BaseURL 暴露内网拓扑也应避免. 替代方案:<完整打印 BaseURL> - 本地部署 URL 通常不算敏感,但统一遮蔽更安全.

type Provider

type Provider struct {
	// contains filtered or unexported fields
}

Provider 是 Ollama ModelProvider 实现.

func New

func New(cfg Config) *Provider

New 创建 Ollama ModelProvider.

func (*Provider) Models

func (p *Provider) Models(ctx context.Context) ([]flyto.ModelInfo, error)

Models 从 Ollama /api/tags 端点获取本地已安装的模型列表.

Ollama 的模型列表完全由用户本地已拉取(ollama pull)的模型决定, 因此必须实时获取,静态表没有意义.

func (*Provider) Name

func (p *Provider) Name() string

func (*Provider) Stream

func (p *Provider) Stream(ctx context.Context, req *flyto.Request) (<-chan flyto.Event, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL