Created
February 15, 2024 09:37
-
-
Save sonique6784/ec7e03c4a199c1a277cd8f097c070f26 to your computer and use it in GitHub Desktop.
A minimalist Gemini implementation for Terminal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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