Skip to content

Instantly share code, notes, and snippets.

@sonique6784
Created February 15, 2024 09:37
Show Gist options
  • Save sonique6784/ec7e03c4a199c1a277cd8f097c070f26 to your computer and use it in GitHub Desktop.
Save sonique6784/ec7e03c4a199c1a277cd8f097c070f26 to your computer and use it in GitHub Desktop.
A minimalist Gemini implementation for Terminal
#
# A minimalist Gemini implementation for Terminal
# Ask Gemini from the command line.
#
import sys
import os
import pathlib
import textwrap
import google.generativeai as genai
from IPython.display import display
from IPython.display import Markdown
print("Loading Gemini...")
def readAPIKey():
apikeypath = 'apikey.txt'
if os.path.isfile(apikeypath):
file = open(apikeypath, 'r')
content = file.readline()
file.close()
return content.strip()
else:
print("add your APIKey in apikey.txt")
exit(1)
genai.configure(api_key=readAPIKey())
model = genai.GenerativeModel('gemini-pro')
chat = model.start_chat()
def to_markdown(text):
text = text.replace('•', ' *')
return Markdown(textwrap.indent(text, '> ', predicate=lambda _: True))
def list_all_models():
for m in genai.list_models():
if 'generateContent' in m.supported_generation_methods:
print(m.name)
def askGemini(prompt):
print("🤖 ", end='', flush=True)
response = chat.send_message(prompt)
print(response.text)
print("❓ ", end='', flush=True)
print("Ready, ask anything:")
print("❓ ", end='', flush=True)
for line in sys.stdin:
if 'exit' == line.rstrip():
break
else:
askGemini(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment