将树莓派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表示情绪稳定。

相关推荐
chlk12313 小时前
Linux文件权限完全图解:读懂 ls -l 和 chmod 755 背后的秘密
linux·操作系统
舒一笑14 小时前
Ubuntu系统安装CodeX出现问题
linux·后端
改一下配置文件14 小时前
Ubuntu24.04安装NVIDIA驱动完整指南(含Secure Boot解决方案)
linux
深紫色的三北六号1 天前
Linux 服务器磁盘扩容与目录迁移:rsync + bind mount 实现服务无感迁移(无需修改配置)
linux·扩容·服务迁移
SudosuBash1 天前
[CS:APP 3e] 关于对 第 12 章 读/写者的一点思考和题解 (作业 12.19,12.20,12.21)
linux·并发·操作系统(os)
曲幽1 天前
FastAPI实战:打造本地文生图接口,ollama+diffusers让AI绘画更听话
python·fastapi·web·cors·diffusers·lcm·ollama·dreamshaper8·txt2img
哈基咪怎么可能是AI2 天前
为什么我就想要「线性历史 + Signed Commits」GitHub 却把我当猴耍 🤬🎙️
linux·github
十日十行2 天前
Linux和window共享文件夹
linux
曲幽2 天前
我用FastAPI接ollama大模型,差点被asyncio整崩溃(附对话窗口实战)
python·fastapi·web·async·httpx·asyncio·ollama
木心月转码ing3 天前
WSL+Cpp开发环境配置
linux