运行本地 LLM 和发出 API 请求的快速指南

ID:21399 / 打印

运行本地 llm 和发出 api 请求的快速指南

好吧,系好安全带,因为我们正在深入研究一个快速而肮脏的解决方案,用于运行本地 llm(语言模型)并发出 api 请求 - 就像花哨的商业解决方案所做的那样。为什么?嗯,为什么不呢?只需大约三分钟,您就可以在本地运行一个完美的系统来进行大多数测试。如果您觉得需要再次扩展到云,切换回来几乎毫不费力。

这是我们将要遵循的文档,主要是为了让您可以声称您已阅读它:

  • openai api 文档

特别是,我们将重点关注提出这样的请求:

curl https://api.openai.com/v1/chat/completions    -h "content-type: application/json"    -h "authorization: bearer $openai_api_key"    -d '{      "model": "gpt-4o-mini",      "messages": [{"role": "user", "content": "say this is a test!"}],      "temperature": 0.7    }' 

到目前为止,一切都很好,对吧?没有什么开创性的。但这就是有趣的地方......

进入lm工作室

有一个名为 lm studio 的宝石工具,它使本地法学硕士更容易处理。安装并运行模型后,您会注意到一个带有名为“开发人员”的控制台图标的选项卡。我知道,一开始听起来不太令人兴奋,但坚持下去,因为它会变得更好。此选项卡附带一个方便的 curl 示例,向您展示如何使用本地模型。而且,你难道不知道吗,它看起来很眼熟!

curl http://localhost:1234/v1/chat/completions    -h "content-type: application/json"    -d '{     "model": "llama-3.1-8b-lexi-uncensored-v2",     "messages": [       { "role": "system", "content": "always answer in rhymes. today is thursday" },       { "role": "user", "content": "what day is it today?" }     ],     "temperature": 0.7,     "max_tokens": -1,     "stream": false }' 

看起来很眼熟对吧?这是我们刚刚看到的本地版本。您将获得与 openai api 请求相同的设置,只不过它在本地计算机上运行。另外,它还有一点小技巧——比如“总是用押韵回答”系统提示。诗歌,有人吗?

python 怎么样?我们找到你了。

如果您更喜欢使用 python(说实话,谁不喜欢呢?),以下是使用 python 的 requests 模块发送相同请求的方法:

import requests import json  url = "http://localhost:1234/v1/chat/completions"  headers = {     "Content-Type": "application/json" }  data = {     "model": "llama-3.1-8b-lexi-uncensored-v2",     "messages": [         {"role": "system", "content": "You are a helpful assistant."},         {"role": "user", "content": "What is the capital of France?"}     ],     "temperature": 0.7,     "max_tokens": -1,     "stream": False }  response = requests.post(url, headers=headers, data=json.dumps(data))  if response.status_code == 200:     result = response.json()     print(result["choices"][0]["message"]["content"]) else:     print(f"Error: {response.status_code}") 

瞧!您现在已准备好向当地的 llm 发送请求,就像使用商业 api 一样。来吧,测试它、打破它、让它变得押韵——世界(或者至少是你的模型)是你的牡蛎。

享受!

上一篇: 科大讯飞Websockets API持续连接断开?如何正确发送音频数据包?
下一篇: BeautifulSoup中find_all提取元素包含回车符如何处理?

作者:admin @ 24资源网   2025-01-14

本站所有软件、源码、文章均有网友提供,如有侵权联系308410122@qq.com

与本文相关文章

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。