espanso Ollama 연결

--

기본 연결 방법

  • Ollama
  • 최근 윈도우 프리뷰 릴리즈
  • 커맨드라인으로 질문하고 답을 얻을 수 있음
  • shell 의 결과물을 출력하는 형태로 Espanso 적용 가능
  • espanso의 form으로 입력하는 것도 좋네.
  • 이 방식 사용하면 cmd 방식으로 출력해주니까 꽤 여러모로 쓸 수 있을 듯. ㅇㅅㅇ!
  • 출처: Nat Taylor
  • 이 블로그도 꽤 재미있다. 완전 텍스트 기반
- trigger: ":llm"
replace: "{{completion}}"
vars:
- name: prompt
type: form
params:
layout: |
Prompt [[prompt]]
fields:
prompt:
multiline: true
- name: completion
type: shell
params:
cmd: "ollama run llama2-uncensored '{{prompt.prompt}}'"

개선 — python을 이용한 자동번역

  • 영어로 입력하고, 출력되는 게 번거로웠다.
  • 파이썬을 이용. 먼저 한글 → 영문 번역한 뒤 → ollama 넘기고 → 다시 한글로 받아오는 형태로 개선. 잘된다.
  • 출력할 때 한글이 계속 깨져서 스트레스 받았었는데 해결함 출처
# translate_from_llm.py
import sys
# import clipboard
from googletrans import Translator
import subprocess
import os
import io
sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding = 'utf-8')
sys.stderr = io.TextIOWrapper(sys.stderr.detach(), encoding = 'utf-8')
def run_command(command):
"""Shell에서 명령어를 실행하고 출력을 반환합니다."""
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
result, error = process.communicate()
if process.returncode != 0:
raise Exception("Command failed: " + str(error))
return result.decode('utf-8')
def translate_text(text, src, dest):
translator = Translator()
translated = translator.translate(text, src=src, dest=dest)
return translated.text
if __name__ == "__main__":
content = translate_text(sys.argv[1], "ko", "en")
llmAnswer = run_command("ollama run llama2-uncensored '" + content + "'")
if not content:
print("not content")
sys.exit(1)
translated_content = translate_text(llmAnswer, "en", "ko")
print(translated_content)

아래는 보완된 espanso 설정

- trigger: ":lko"
replace: "{{completion}}"
vars:
- name: prompt
type: form
params:
layout: |
Prompt [[prompt]]
fields:
prompt:
multiline: true
- name: completion
type: shell
params:
cmd: "py D:\\GoogleDrive\\REFERENCEs\\PROGRAMs\\espanso-portable\\.espanso\\tools\\trans\\translate_from_llm.py '{{prompt.prompt}}'"

실 사용예시

아래처럼 키워드를 넣으면 Ollama에 영어로 번역해서 넘긴 다음, 영어로 온 답변을 다시 한글로 변역해 출력한다.
가볍게 쓰기 좋은 듯.

--

--

Elf Painter | Creative Technology Visionary

I'm a writer pursuing creative innovation through the fusion of AI and art.