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

相关推荐
leonshi6 小时前
使用embedchain快速建立rag知识库,本地大模型
ai·rag·ollama
orion571 天前
Missing Semester Class1:course overview and introduction of shell
linux
用户120487221611 天前
Linux驱动编译与加载
linux·嵌入式
用户805533698032 天前
Input 子系统架构:Core、Handler、Driver 三层是怎么协作的
linux·嵌入式
用户805533698032 天前
RK-Forge外设系列开篇 - 把板子从「能启动」变成「能用」:Ethernet/SPI/MMC 三个纯接线外设
linux·github·嵌入式
七歌杜金房2 天前
我终于又有了自己的 Linux 电脑
linux·debian·mac
tntxia3 天前
linux curl命令详解_curl详解
linux
扛枪的书生3 天前
Linux 网络管理器用法速查
linux
匹诺曹i3673 天前
memory_search 对了、7B 总结错了:OpenClaw D4 来源引用与 8 个坑
ollama
顺风尿一寸3 天前
Java Socket 内核之旅:从 SocketChannel.read() 到 tcp_recvmsg 与 epoll 的完整调用链路
linux