Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tech challenge/python sdk #1

Merged
merged 10 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions sdk-python/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.venv_*
.env
**/*.pyc
31 changes: 31 additions & 0 deletions sdk-python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
### Getting started
1. Enter env variables in example.env and rename to .env
2. Create and start venv (see instructions below)
3. From venv, run `python3 main.py`

### Virtual Environment for Local Dev
Make sure virtual environment is created
1. Create virtual environment in root directory: `python3 -m venv .venv_llm-ops`
2. Activate virtual environment:

// On Windows Command Shell, run:
`.venv_llm-ops\Scripts\activate.bat`

// On Windows Power Shell, run:
`.venv_llm-ops\Scripts\activate.ps1`

// On Unix or MacOS, run:
`source .venv_llm-ops/bin/activate`

3. Update pip to install packages: `pip install --upgrade pip`
4. Install required packages: `pip install -r requirements.txt`
5. After dev, you should update requirements.txt:
- `pip freeze > requirements.txt`
- WARNING: this prevents any additional packages from being installed in venv, only run this command after finishing dev
- To start installing packages in venv again, delete .venv folder and restart process

### Running .py files
1. While in venv, run `export PYTHONPATH='.'`
- Helps to fix: `ModuleNotFoundError: No module named 'lib'`
- If not, check that scripts and folders do not have the same name
2. Run `python3 path/to/file`
25 changes: 25 additions & 0 deletions sdk-python/database/connect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import os
import psycopg2
from dotenv import load_dotenv
load_dotenv()

DATABASE_URL = os.getenv('DATABASE_URL')

if not DATABASE_URL:
raise EnvironmentError(f"DATABASE_URL not found: {DATABASE_URL}\nMake sure that you have a DATABASE_URL value in the .env file.")

def connect_to_db():
conn = psycopg2.connect(DATABASE_URL)
cur = conn.cursor()
cur.execute('SELECT NOW();')
time = cur.fetchone()[0]

cur.execute('SELECT version();')
version = cur.fetchone()[0]

if not time or not version:
raise ConnectionError(f"Could not connect to database: {DATABASE_URL}")

print('Current time:', time)
print('PostgreSQL version:', version)
return conn, cur
5 changes: 5 additions & 0 deletions sdk-python/database/write_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def write_data(cur, time, prompt, response, tokens):
cur.execute("""
INSERT INTO llm_logs (datetime_utc, input_string, output_string, total_tokens)
VALUES (%s, %s, %s, %s);
""", (time, prompt, response, tokens))
2 changes: 2 additions & 0 deletions sdk-python/example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
OPENAI_API_KEY=
DATABASE_URL=
18 changes: 18 additions & 0 deletions sdk-python/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from dotenv import load_dotenv
load_dotenv()
from database.connect import connect_to_db
from database.write_data import write_data
from models.get_llm_response import get_llm_response
from datetime import datetime

prompt = "What is a proompt?"
response = get_llm_response(prompt)

connection, cursor = connect_to_db()

write_data(cursor, datetime.utcfromtimestamp(response.created), prompt, response.choices[0].text, response.usage.total_tokens)

connection.commit()

cursor.close()
connection.close()
15 changes: 15 additions & 0 deletions sdk-python/models/get_llm_response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import os
import openai

OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")

if not OPENAI_API_KEY:
raise EnvironmentError(f"OPENAI_API_KEY not found: {OPENAI_API_KEY}\nMake sure that you have an OPENAI_API_KEY value in the .env file.")

openai.api_key = OPENAI_API_KEY
def get_llm_response(prompt: str):
return openai.Completion.create(
model="text-davinci-003",
prompt=prompt,
temperature=0.6,
)
16 changes: 16 additions & 0 deletions sdk-python/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
aiohttp==3.8.5
aiosignal==1.3.1
async-timeout==4.0.2
attrs==23.1.0
certifi==2023.7.22
charset-normalizer==3.2.0
frozenlist==1.4.0
idna==3.4
multidict==6.0.4
openai==0.27.8
psycopg2-binary==2.9.7
python-dotenv==1.0.0
requests==2.31.0
tqdm==4.65.1
urllib3==2.0.4
yarl==1.9.2