Skip to content

Commit

Permalink
Add ollama backend.
Browse files Browse the repository at this point in the history
  • Loading branch information
manateelazycat committed Oct 20, 2023
1 parent e92d6de commit bc2d97b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
10 changes: 9 additions & 1 deletion README.md
Expand Up @@ -25,14 +25,22 @@

### 安装

1. 安装 [crow-translate](https://crow-translate.github.io/)
1. 安装 [crow-translate](https://crow-translate.github.io/)[ollama](https://github.com/jmorganca/ollama)
2. 下载 [insert-translated-name](https://github.com/manateelazycat/insert-translated-name) 里面的 insert-translated-name.el 放到 ~/elisp 目录
3. 把下面的配置加入到 ~/.emacs 中
```
(add-to-list 'load-path (expand-file-name "~/elisp"))
(require 'insert-translated-name)
```

### 用本地大模型来翻译
安装 crow 可以直接用在线翻译来翻译内容, 但是因为网络的问题, 有时候延时大了体验还是不爽的, 这时候可以 ollama 来驱动本地大模型来快速翻译, 我测试了一下都是秒回, 只不过本地大模型需要的内存较多, 内存紧张的用户建议还是用 crow 吧。

下面是安装大模型的方式:
1. 安装 ollama: `curl https://ollama.ai/install.sh | sh`, 其他平台安装方式请参考 [ollama](https://github.com/jmorganca/ollama) 官网
2. 下载 llama2-chinese 大模型: `ollama run llama2-chinese` 这条命令会自动下载 `llama2-chinese` 这个模型文件, 下载好了可以在终端测试一下
3. 设置翻译程序: ```(setq insert-translated-name-program "ollama")```

### 使用
| 命令 | 描述 |
| :-------- | :---- |
Expand Down
29 changes: 24 additions & 5 deletions insert-translated-name.el
Expand Up @@ -152,6 +152,11 @@
"Search and refacotry code base on ripgrep."
:group 'insert-translated-name)

(defcustom insert-translated-name-program "crow"
"Use `crow' or `ollama' to translate input."
:group 'insert-translated-name
:type 'string)

(defcustom insert-translated-name-crow-engine "google"
"the crow app engine"
:group 'insert-translated-name
Expand All @@ -162,6 +167,10 @@
"Face for keyword match."
:group 'insert-translated-name)

(defvar insert-translated-name-ollama-file (expand-file-name "ollama.py" (if load-file-name
(file-name-directory load-file-name)
default-directory)))

(defvar insert-translated-name-origin-style-mode-list
'(text-mode erc-mode rcirc-mode))

Expand Down Expand Up @@ -410,7 +419,9 @@
(insert-translated-name-update-translation-in-buffer
insert-translated-name-word
insert-translated-name-style
(alist-get 'translation (json-read-from-string output))
(pcase insert-translated-name-program
("crow" (alist-get 'translation (json-read-from-string output)))
("ollama" (replace-regexp-in-string "\\'\\|\\'\\|\\.\\|\\,\\|\\?\\|\\!" "" (string-trim output))))
insert-translated-name-buffer-name
insert-translated-name-placeholder)
))))
Expand All @@ -427,10 +438,18 @@
(setq insert-translated-name-placeholder placeholder)
(when (get-buffer " *insert-translated-name*")
(kill-buffer " *insert-translated-name*"))
(let ((process (start-process
"insert-translated-name"
" *insert-translated-name*"
"crow" "-t" "en" "--json" "-e" insert-translated-name-crow-engine word)))
(let ((process (pcase insert-translated-name-program
("crow"
(start-process
"insert-translated-name"
" *insert-translated-name*"
"crow" "-t" "en" "--json" "-e" insert-translated-name-crow-engine word))
("ollama"
(start-process
"insert-translated-name"
" *insert-translated-name*"
"python" insert-translated-name-ollama-file (format "'%s'" word)
)))))
(set-process-sentinel process 'insert-translated-name-process-sentinel)))

(provide 'insert-translated-name)
Expand Down
25 changes: 25 additions & 0 deletions ollama.py
@@ -0,0 +1,25 @@
import requests
import json
import sys

input = sys.argv[1]

url = 'http://localhost:11434/api/generate'

data = {
"model": "llama2-chinese",
"prompt": '''
Translate the following text to English, only return the content translated, no explaination:
{}
'''.format(input)
}

result = ""
with requests.post(url, json=data, stream=True) as response:
for line in response.iter_lines():
if line:
json_response = json.loads(line)
if not json_response["done"]:
result += json_response["response"]

print(result, flush=True)

0 comments on commit bc2d97b

Please sign in to comment.