将树莓派5当做Ollama服务器,C#调用generate的API的示例

其实完全没这个必要,性能用脚后跟想都会很差。但基于上一篇文章的成果,来都来了就先简单试试吧。

先来看看这个拼夕夕上五百多块钱能达到的效果:

只要对速度没要求,那感觉就还行。

Ollama默认只在本地回环(127.0.0.1)上提供服务,树莓派下可以修改/etc/systemd/system/ollama.service,在Service节中加一句:

bash 复制代码
Environment="OLLAMA_HOST=0.0.0.0:11434"

注意,这可能会导致一些潜在的网络安全风险,比如未经授权的访问等,请谨慎评估。

然后重启服务:

bash 复制代码
sudo systemctl daemon-reload
sudo systemctl restart ollama

这样别的设备也能访问Ollama的API了。

C#代码很简单,本质上就是一个异步的POST调用。

cs 复制代码
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;


namespace OllamaClient
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private async void btnPost_Click(object sender, EventArgs e)
        {
            string ollamaUrl = txtURL.Text.Trim();
            string modelName = txtModel.Text.Trim();
            string question = txtQuestion.Text.Trim();

            string response = await GenerateAnswer(ollamaUrl, question, modelName,false);
            txtResponse.Text = response;
        }

        private async Task<string> GenerateAnswer(string ollamaUrl, string question, string modelName,bool useStreamMode)
        {
            // 创建 HttpClient 实例
            using (HttpClient client = new HttpClient())
            {
                client.Timeout = TimeSpan.FromMilliseconds(Timeout.Infinite);

                // 构建请求的 URL
                string requestUrl = $"{ollamaUrl}/api/generate";

                // 构建请求的 JSON 数据
                var requestBody = new
                {
                    model = modelName,
                    prompt = question,
                    stream = useStreamMode
                };

                // 将请求数据序列化为 JSON 字符串
                string json = JsonConvert.SerializeObject(requestBody);

                // 创建 HttpContent 对象
                StringContent content = new StringContent(json, Encoding.UTF8, "application/json");

                // 发送 POST 请求
                HttpResponseMessage response = await client.PostAsync(requestUrl, content);

                // 确保请求成功
                response.EnsureSuccessStatusCode();

                // 读取返回的 JSON 数据
                string jsonResponse = await response.Content.ReadAsStringAsync();
                //return jsonResponse;

                // 解析 JSON 数据,提取回答内容
                dynamic jsonResponseObj = JsonConvert.DeserializeObject(jsonResponse);
                string answer = jsonResponseObj.response;

                // 返回回答内容
                // 换行符默认\n,Windows下不太友好
                return answer.Replace("\n","\r\n");
            }
        }
    }

}

需事先用NuGet工具装好Newtonsoft.Json。

RPI5表示情绪稳定。

相关推荐
Apipi*6 小时前
30天速通Linux 第六章信号及信号处理
linux·运维·信号处理
天启HTTP7 小时前
爬虫防封核心原理:IP轮换机制与风控规避逻辑
linux·服务器·网络·爬虫·tcp/ip
广州浮点FLOATLIC10 小时前
许可证服务器迁移后软件打不开:研发 IT 怎样定位连接问题
linux·服务器·数据库
BLUcoding10 小时前
接口服务公网超时排查记录:SecureRandom 阻塞问题分析与解决
java·linux·springboot·aes·securerandom
AIgorithmGEEK10 小时前
[Linux]从手写报头到内核套路:序列化、反序列化与自定义协议全链路
linux·运维·服务器·网络·序列化·反序列化
Nil20810 小时前
leetcode 139单词拆分
linux·运维·服务器
Starry-sky(jing)10 小时前
BUG: unable to handle kernel paging request 完整排查:dmesg 四要素与三路定罪
linux·运维·服务器·内核·排障
mounter62510 小时前
从硬件互连到操作系统变革:CXL 技术演进与 Linux 内核工程挑战
linux·运维·服务器
xh didida12 小时前
Linux -- 基础IO
linux·服务器·开发语言·c++
zly350013 小时前
centos7 mount设备挂载 /dev/sda1/硬盘挂载
linux·运维·服务器