Skip to content

Ollama

Ollama是一个本地化AI模型解决方案,允许您在本地运行开源大型语言模型(LLM)。通过PPAgent,您可以轻松地将Ollama模型集成到您的应用中,实现智能对话和自动化任务。

特点

  • 支持多种开源大语言模型
  • 支持本地部署模型,不依赖外部API
  • 支持基础认证和Token认证
  • 支持自定义系统提示词
  • 支持传输图像(适用于多模态模型)
  • 支持自定义Ollama参数(如温度、Top P等)

使用代码配置

您可以通过以下代码配置来将Ollama集成到PPAgent应用中:

typescript
const chatAgent = new PPAgent({
    name: "default",
    agentServiceOptions: {
        models: {
            bots: [
                {
                    name: "ollama-bot",
                    options: {
                        instanceName: "ollama-llm", // 实例名称,需要全局唯一
                        apiBase: "http://localhost:11434", // Ollama API服务地址
                        model: "llama3", // 模型名称
                        authType: "none", // 认证类型:none, basic 或 token
                        // 基础认证配置(当authType为basic时需要)
                        basicOptions: {
                            name: "username", // 基础认证用户名
                            password: "password" // 基础认证密码
                        },
                        // Token认证配置(当authType为token时需要)
                        tokenOptions: {
                            in: "header", // token位置:header 或 query
                            key: "Authorization", // token键名
                            token: "your_token_here" // token值
                        },
                        // 系统提示词
                        systemPrompt: "你是一个有用的AI助手。",
                        // 是否为纯文本模型
                        onlyText: true,
                        // Ollama的可选参数,如温度、Top P等
                        options: {
                            temperature: 0.7,
                            top_p: 0.9
                        },
                        // 模型在GPU中驻留时间(毫秒)
                        keep_alive: 60000
                    }
                }
            ],
            // ... source和agent等配置 ...
        }
    }
});

服务器运行请参考 install_code.md 文件。

配置注意事项

  1. Ollama服务地址:确保您已正确安装和启动Ollama服务,默认地址为http://localhost:11434

  2. 模型名称:必须使用已在Ollama中拉取的模型名称,您可以通过ollama list命令查看可用模型。

  3. 认证设置:根据您的Ollama服务设置选择正确的认证方式。对于没有认证的本地服务,使用authType: "none"

  4. 多模态支持:如果使用支持图像处理的多模态模型(如llava),请将onlyText设置为false

  5. 系统提示词:使用systemPrompt可以定义模型的默认行为和角色,这对于特定应用场景非常有用。

  6. 性能优化keep_alive参数可以控制模型在GPU中的驻留时间,对于频繁使用的场景可以提高响应速度。