
好吧,系好安全带,因为我们正在深入研究一个快速而肮脏的解决方案,用于运行本地 llm(语言模型)并发出 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 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 的 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 一样。来吧,测试它、打破它、让它变得押韵——世界(或者至少是你的模型)是你的牡蛎。
以上就是运行本地 LLM 和发出 API 请求的快速指南的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号