Skip to content

Instantly share code, notes, and snippets.

@belkarx
Created July 1, 2024 21:12
Show Gist options
  • Select an option

  • Save belkarx/544956b9dc500adc104ec5c8b4c1bec7 to your computer and use it in GitHub Desktop.

Select an option

Save belkarx/544956b9dc500adc104ec5c8b4c1bec7 to your computer and use it in GitHub Desktop.
anthropic web interface too slow? pop up a little window for quick queries, as a google replacement
import tkinter as tk
#import pyperclip
import requests
import os
import json
def submit_text(event=None):
text = text_var.get()
response = requests.post(
"https://api.anthropic.com/v1/messages",
headers={
"Content-Type": "application/json",
"anthropic-version": "2023-06-01",
"x-api-key": os.environ["ANTHROPIC_API_KEY"]
},
json={
"model": "claude-3-5-sonnet-20240620",
"max_tokens": 247,
"temperature": 0.2,
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": f"be very concise (give only code snippets and not extra text when appropriate, answer the user's query (assume what they're asking, act like a search engine) but nothing else, stay *short*.\nquery: {text}"
}
]
}
]
}
)
print(response.json())
result = response.json()['content'][0]['text']
result_text.delete(1.0, tk.END)
result_text.insert(tk.END, result)
result_text.focus()
def copy_and_close(event=None):
text = result_text.get(1.0, tk.END).strip()
#pyperclip.copy(text)
root.destroy()
root = tk.Tk()
root.attributes('-type', 'dialog')
root.eval('tk::PlaceWindow . center')
text_var = tk.StringVar()
entry = tk.Entry(root, textvariable=text_var)
entry.focus()
entry.bind("<Return>", submit_text)
entry.pack()
result_text = tk.Text(root, height=10, width=50)
result_text.pack()
result_text.bind("<Return>", copy_and_close)
root.mainloop()
@belkarx
Copy link
Author

belkarx commented Jul 1, 2024

#.xbinkeysrc
"/usr/bin/zsh ~/open_llm.sh"
    alt+a
#open_llm.sh
source /home/uk000/.python_venv/bin/activate &&
/home/uk000/.python_venv/bin/python3 /home/uk000/open_llm.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment