-
-
Save sekmet/b27e2c4889b29ff7e9feddc8a82771db to your computer and use it in GitHub Desktop.
Python LLM Starter Module
This file contains 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
""" | |
Purpose: | |
Interact with the OpenAI API. | |
Provide supporting prompt engineering functions. | |
""" | |
import sys | |
from dotenv import load_dotenv | |
import os | |
from typing import Any, Dict | |
import openai | |
# load .env file | |
load_dotenv() | |
assert os.environ.get("OPENAI_API_KEY") | |
# get openai api key | |
openai.api_key = os.environ.get("OPENAI_API_KEY") | |
# ------------------ helpers ------------------ | |
def safe_get(data, dot_chained_keys): | |
""" | |
{'a': {'b': [{'c': 1}]}} | |
safe_get(data, 'a.b.0.c') -> 1 | |
""" | |
keys = dot_chained_keys.split(".") | |
for key in keys: | |
try: | |
if isinstance(data, list): | |
data = data[int(key)] | |
else: | |
data = data[key] | |
except (KeyError, TypeError, IndexError): | |
return None | |
return data | |
def response_parser(response: Dict[str, Any]): | |
return safe_get(response, "choices.0.message.content") | |
# ------------------ content generators ------------------ | |
def prompt(prompt: str, model: str = "gpt-4") -> str: | |
# validate the openai api key - if it's not valid, raise an error | |
if not openai.api_key: | |
sys.exit( | |
""" | |
ERORR: OpenAI API key not found. Please export your key to OPENAI_API_KEY | |
Example bash command: | |
export OPENAI_API_KEY=<your openai apikey> | |
""" | |
) | |
response = openai.ChatCompletion.create( | |
model=model, | |
messages=[ | |
{ | |
"role": "user", | |
"content": prompt, | |
} | |
], | |
) | |
return response_parser(response) | |
def add_cap_ref( | |
prompt: str, prompt_suffix: str, cap_ref: str, cap_ref_content: str | |
) -> str: | |
""" | |
Attaches a capitalized reference to the prompt. | |
Example | |
prompt = 'Refactor this code.' | |
prompt_suffix = 'Make it more readable using this EXAMPLE.' | |
cap_ref = 'EXAMPLE' | |
cap_ref_content = 'def foo():\n return True' | |
returns 'Refactor this code. Make it more readable using this EXAMPLE.\n\nEXAMPLE\n\ndef foo():\n return True' | |
""" | |
new_prompt = f"""{prompt} {prompt_suffix}\n\n{cap_ref}\n\n{cap_ref_content}""" | |
return new_prompt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment