forked from appenz/macLLM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
macllm.py
84 lines (69 loc) · 2.13 KB
/
macllm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#
# Ultra-simple LLM tool for the macOS clipboard
# (c) in 2023 Guido Appenzeller
#
# OpenAI API Key is taken fromthe environment variable OPENAI_API_KEY
# or imported from the file apikey.py
#
import os
import subprocess
import openai
import time
from shortcuts import ShortCut
class Clipboard:
def get(self):
p = subprocess.Popen(['pbpaste'], stdout=subprocess.PIPE)
r = p.wait()
data = p.stdout.read()
return data
def set(self,data):
p = subprocess.Popen(['pbcopy'], stdin=subprocess.PIPE)
p.stdin.write(data)
p.stdin.close()
r = p.wait()
class LLM:
def __init__(self, model="gpt-3.5-turbo", temperature=0.0):
self.model = model
self.temperature = temperature
def generate(self, text):
c = openai.ChatCompletion.create(
model=self.model,
messages = [
{"role": "user", "content": str(text)},
],
)
return c.choices[0].message.content
# Get API Key from environment variable or from file
# e.g. export OPENAI_API_KEY="copy pasted from https://platform.openai.com/account/api-keys"
openai.api_key = os.getenv("OPENAI_API_KEY")
if not openai.api_key:
import apikey
openai.api_key = apikey.apikey
# Create a clipboard object
clipboard = Clipboard()
llm = LLM()
req = 0
# Watch the clipboard for the trigger string "@@" and if you find it run through GPT
# and write the result back to the clipboard
print()
print('To get an answer from the ChatGPT API, copy text that starts with "@@" (no quotes!) to the clipboard, wait a bit, then paste the answer.')
print()
while True:
txt = clipboard.get().decode()
if txt.startswith("@@"):
req = req+1
if ShortCut.checkShortcuts(txt):
txt = ShortCut.checkShortcuts(txt).generate(txt)
else:
txt = txt[2:].strip()
out = llm.generate(txt).strip()
print()
print(f'--- Request: {req} ----------------------------')
print(txt)
print('-->')
print(out)
print()
clipboard.set(out.encode())
# wait 1 second
time.sleep(1)
# @@Capital of Paris?