服务器大本营

 找回密码
 我要入营

QQ登录

只需一步,快速开始

查看: 15|回复: 0

Ollama搭建本地AI大模型并应用调用

[复制链接]

7万

敬重

812

主题

207

精华

管理员

积分
8930

启航之章进阶之印里程之碑突破之证飞跃之星蜕变之勋卓越之路龙年行大运

QQ
发表于 昨天 15:06 | 显示全部楼层 |阅读模式

我要入营,结交更多好友,开启更多功能,轻松玩转服务器大本营!

您需要 登录 才可以下载或查看,没有账号?我要入营

x
1、下载Ollama
1)打开
Ollama官网,点击download下载
2)下载后直接安装即可。

2、启动配置模型
默认是启动cmd窗口直接输入
  1. ollama run llama3
复制代码

启动llama3大模型 或者启动千问大模型
  1. ollama run qwen2
复制代码
启动输入你需要输入的问题即可

3、配置UI界面
安装docker
并部署web操作界面
  1. docker run -d -p 3000:8080 --add-host=host.docker.internal:host-gateway -v open-webui --restart always ghcr.io/open-webui/open-webui:main
复制代码
安装完毕后,安装包较大,需等待一段时间。
localhost:3000即可打开网址

4、搭建本地知识库
AnythingLLM

5、配置文件
开放11434端口,便于外部访问接口,如果跨域访问的话配置OLLAMA_ORIGINS=*

Windows版
只需要在系统环境变量中直接配置
OLLAMA_HOST为变量名,"0.0.0.0:11434"为变量值
  1. OLLAMA_HOST= "0.0.0.0:11434"
复制代码

MAC版
配置OLLAMA_HOST
  1. sudo sh -c 'echo "export OLLAMA_HOST=0.0.0.0:11434">>/etc/profile'launchctl setenv OLLAMA_HOST "0.0.0.0:11434"
复制代码

Linux版
配置OLLAMA_HOST
  1. Environment="OLLAMA\_HOST=0.0.0.0"
复制代码

6、程序调用接口
golang实现例子:流式响应速度更快,用户体验更佳。
golang例子:非流式响应

  1. package main
  2. import (        "bufio"        "bytes"        "encoding/json"        "fmt"        "io/ioutil"        "net/http"        "os"        "strings"        "time")
  3. const (          obaseURL  = "http://localhost:11434/api"          omodelID  = "qwen2:0.5b"                 // 选择合适的模型        oendpoint = "/chat"                      //"/chat/completions")
  4. // ChatCompletionRequest 定义了请求体的结构type olChatCompletionRequest struct {        Model    string `json:"model"`        Messages []struct {                Role    string `json:"role"`                Content string `json:"content"`        } `json:"messages"`        Stream bool `json:"stream"`        //Temperature float32 `json:"temperature"`}
  5. // ChatCompletionResponse 定义了响应体的结构type olChatCompletionResponse struct {        //Choices []struct {        Message struct {                Role    string `json:"role"`                Content string `json:"content"`        } `json:"message"`        //} `json:"choices"`}
  6. // sendRequestWithRetry 发送请求并处理可能的429错误func olsendRequestWithRetry(client *http.Client, requestBody []byte) (*http.Response, error) {        req, err := http.NewRequest("POST", obaseURL+oendpoint, bytes.NewBuffer(requestBody))        if err != nil {                return nil, err        }
  7.         req.Header.Set("Content-Type", "application/json")        //req.Header.Set("Authorization", "Bearer "+apiKey)
  8.         resp, err := client.Do(req)        if err != nil {                return nil, err        }
  9.         if resp.StatusCode == http.StatusTooManyRequests {                retryAfter := resp.Header.Get("Retry-After")                if retryAfter != "" {                        duration, _ := time.ParseDuration(retryAfter)                        time.Sleep(duration)                } else {                        time.Sleep(5 * time.Second) // 默认等待5秒                }                return olsendRequestWithRetry(client, requestBody) // 递归重试        }
  10.         return resp, nil}
  11. func main() {        client := &http.Client{} // 创建一个全局的 HTTP 客户端实例
  12.         // 初始化对话历史记录        history := []struct {                Role    string `json:"role"`                Content string `json:"content"`        }{                {"system", "你是一位唐代诗人,特别擅长模仿李白的风格。"},        }
  13.         // 创建标准输入的扫描器        scanner := bufio.NewScanner(os.Stdin)
  14.         for {                fmt.Print("请输入您的问题(或者输入 'exit' 退出): ")                scanner.Scan()                userInput := strings.TrimSpace(scanner.Text())
  15.                 // 退出条件                if userInput == "exit" {                        fmt.Println("感谢使用,再见!")                        break                }
  16.                 // 添加用户输入到历史记录                history = append(history, struct {                        Role    string `json:"role"`                        Content string `json:"content"`                }{                        "user",                        userInput,                })
  17.                 // 创建请求体                requestBody := olChatCompletionRequest{                        Model:    omodelID,                        Messages: history,                        Stream:   false,                        //Temperature: 0.7,                }
  18.                 // 构建完整的请求体,包含历史消息                requestBody.Messages = append([]struct {                        Role    string `json:"role"`                        Content string `json:"content"`                }{                        {                                Role:    "system",                                Content: "你是一位唐代诗人,特别擅长模仿李白的风格。",                        },                }, history...)
  19.                 // 将请求体序列化为 JSON                requestBodyJSON, err := json.Marshal(requestBody)                if err != nil {                        fmt.Println("Error marshalling request body:", err)                        continue                }                fmt.Println("wocao:" + string(requestBodyJSON))                // 发送请求并处理重试                resp, err := olsendRequestWithRetry(client, requestBodyJSON)                if err != nil {                        fmt.Println("Error sending request after retries:", err)                        continue                }                defer resp.Body.Close()
  20.                 // 检查响应状态码                if resp.StatusCode != http.StatusOK {                        fmt.Printf("Received non-200 response status code: %d\n", resp.StatusCode)                        continue                }
  21.                 // 读取响应体                responseBody, err := ioutil.ReadAll(resp.Body)                if err != nil {                        fmt.Println("Error reading response body:", err)                        continue                }                //fmt.Println("0000" + string(responseBody))                // 解析响应体                var completionResponse olChatCompletionResponse                err = json.Unmarshal(responseBody, &completionResponse)                if err != nil {                        fmt.Println("Error unmarshalling response body:", err)                        continue                }                fmt.Printf("AI 回复: %s\n", completionResponse.Message.Content) // choice.Message.Content                // 将用户的消息添加到历史记录中                history = append(history, struct {                        Role    string `json:"role"`                        Content string `json:"content"`                }{                        Role:    completionResponse.Message.Role,                        Content: completionResponse.Message.Content, // 假设用户的消息是第一个                })
  22.         }}
复制代码

感谢您的阅读,服务器大本营-技术文章内容集合站,助您成为更专业的服务器管理员!
一入IDC深似海,从此你我是良人!
您需要登录后才可以回帖 登录 | 我要入营

本版积分规则

点击直接加入[服务器大本营QQ频道]
滴!摸鱼时间到~
Loading...

QQ|Archiver|手机版|网站地图|服务器大本营 ( 赣ICP备2021009089号 )

GMT+8, 2025-7-11 11:09 , Processed in 0.060563 second(s), 26 queries , Gzip On.

Powered by 服务器大本营

© 2021-2023 联系飞飞

快速回复 返回顶部 返回列表