Skip to content

Commit

Permalink
make question.py available as a the name of the file
Browse files Browse the repository at this point in the history
  • Loading branch information
cdemers committed Oct 19, 2023
1 parent 861b0c8 commit 797d98b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 58 deletions.
58 changes: 0 additions & 58 deletions question

This file was deleted.

1 change: 1 addition & 0 deletions question
58 changes: 58 additions & 0 deletions question.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env python3

import os
import sys
import openai
import logging
from rich.markdown import Markdown
from rich import print as rprint
from halo import Halo

# Setup logging based on environment variable
log_level = os.environ.get("LOG_LEVEL", "WARNING").upper()
logging.basicConfig(level=getattr(logging, log_level))

# Read OpenAI API key from environment variable
api_key = os.environ.get("OPENAI_API_KEY")
if not api_key:
logging.error("OPENAI_API_KEY environment variable not set.")
exit(1)

openai.api_key = api_key

def call_openai_api(question):
try:
MODEL = "gpt-4-0613"
messages = [{"role": "user", "content": question}]

# Make API call
with Halo(text='Waiting for response...', spinner='dots'):
response = openai.ChatCompletion.create(
model=MODEL,
messages=messages,
temperature=0,
)
return response['choices'][0]['message']['content']
except Exception as e:
logging.error(f"API call failed: {e}")
return None

if __name__ == "__main__":
if len(sys.argv) < 2:
logging.error("No query string provided.")
exit(1)

# Get user input from command line argument
question = sys.argv[1]
logging.info(f"Received input: {question}")

# Call OpenAI API
response = call_openai_api(question)

# Output response or failure message
if response:
markdown_response = Markdown(response)
rprint("Assistant says:", markdown_response)
else:
print("Failed to get a response.")

0 comments on commit 797d98b

Please sign in to comment.