搭建本地日中翻译服务
下载SakuraLLM模型
鉴于显存为6G,下载20241012-Qwen2.5-1.5B-v1.0模型,去https://hf-mirror.com/SakuraLLM/Sakura-1.5B-Qwen2.5-v1.0-GGUF/tree/main下载gguf文件。set CUDA_VISIBLE_DEVICES=-1(windows的环境变量设置方式),在CPU上也能运行
编译llama.cpp
下载llama.cpp代码包
cmake -B build -DGGML_CUDA=ON
cmake --build build --config Release
将build/bin/llama-server拷贝到下一节下载好后的Sakura_Launcher_GUI/llama目录下(不必要)
运行Sakura启动器(不必要)
下载PiDanShouRouZhouXD/Sakura_Launcher_GUI代码包,python main.py,程序运行起来之后,设置SakuraLLM模型的位置,启动翻译服务,即可访问http://localhost:8080,问一句,它翻译一句
根据Sakura启动器的输出命令运行服务
实际上启动器构建了llama-server.exe --model "sakura-1.5b-qwen2.5-v1.0-fp16.gguf" -c 2048 -ngl 200 -a sakura-1.5b-qwen2.5-v1.0-fp16.gguf" --host 127.0.0.1 --port 8080 -np 1 --metrics --slots -fa --no-mmap
命令来运行服务,所以,只要准备好llama-server二进制文件和sakura-1.5b-qwen2.5-v1.0-fp16.gguf即可。
批量化翻译
import openai
taf='target.txt'
pici=1
client=openai.OpenAI(base_url='http://127.0.0.1:8080/v1',api_key='1')
messages=[{'role':'system','content':'You are a helpful assistant.'}]
with open('test.txt',encoding='utf8') as f:
contents=f.readlines()
print(contents)
nc=[]
for i in range(len(contents)):
print(contents[i].strip())
if i%pici==0:
messages=[{'role':'system','content':'You are a helpful assistant.'}]
messages.append({'role':'user','content':contents[i].strip()})
else:
messages.append({'role':'user','content':contents[i].strip()})
completion=client.chat.completions.create(model='sakura-1.5b-qwen2.5-v1.0-fp16',messages=messages,frequency_penalty=0.2)# 服务启动时会打印模型名称,那个即为model的值
print(i,'/',len(contents),completion.choices[0].message.content)
messages.append({'role':'assistant','content':completion.choices[0].message.content})
if completion.choices[0].message.content[:-1]=='。':
nc.append(completion.choices[0].message.content[:-1]+'\n')
else:
nc.append(completion.choices[0].message.content+'\n')
print()
with open(taf,'w',encoding='utf8') as f:
f.write(''.join(nc))
以十句为一组作为上文进行翻译,上文过长的话尤其翻译过300句的段落时不对上文进行限制的话,大模型容易陷入连续重复回答一句话的地步(好像死循环一样)
参考链接:
SakuraLLM/SakuraLLM
PiDanShouRouZhouXD/Sakura_Launcher_GUI
创建于2501152208,修改于2501152208