Added openai library

This commit is contained in:
Oleg Lavrovsky 2024-07-12 18:02:51 +02:00
parent 860da43611
commit d12f9c4441
No known key found for this signature in database
GPG key ID: 31E523030632FF4B
7 changed files with 307 additions and 37 deletions

View file

@ -3,51 +3,55 @@
import logging
import requests
import openai
from flask import current_app
from .user.models import Project
# In seconds, how long to wait for API response
REQUEST_TIMEOUT = 90
REQUEST_TIMEOUT = 10
ai_client = None
def gen_project_pitch(project: Project):
return gen_challenge_openai(project.name, project.summary)
topic = ""
if project.category:
topic = project.category.name
return gen_challenge_openai(project.name, topic, project.summary)
def gen_challenge_discolm(title: str, summary: str):
"""Request data from a DiscoLM API."""
API_URL = "https://api-inference.huggingface.co/models/DiscoResearch/DiscoLM-120b"
headers = {"Authorization": "Bearer %s" % current_app.config['LLM_API_KEY']}
payload = {
"inputs": "Write a challenge statement for a tech sprint or hackathon project about " +\
'%s. Do not include, but make it relevant to the title "%s"' % (summary, title)
}
response = requests.post(API_URL, headers=headers, json=payload, timeout=REQUEST_TIMEOUT)
return response.json()
def gen_challenge_openai(title: str, summary: str):
def gen_challenge_openai(title: str, topic: str, summary: str):
"""Request data from an OpenAI API."""
if not current_app.config['LLM_API_KEY']:
logging.error('Missing ChatGPT configuration (LLM_API_KEY)')
return None
API_URL = "https://api.openai.com/v1/chat/completions"
headers = {"Authorization": "Bearer %s" % current_app.config['LLM_API_KEY'],
"Content-Type": "application/json" }
prompt = "Write a challenge statement for a tech sprint or hackathon project about " +\
'"%s". In about 100 words, include a series of working steps for a prototype. ' % summary +\
' Make it relevant to the title: "%s"' % title
# See https://platform.openai.com/docs/api-reference/making-requests
payload = {
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": prompt}],
"temperature": 0.7
}
response = requests.post(API_URL, headers=headers, json=payload, timeout=REQUEST_TIMEOUT)
if ai_client is None:
if current_app.config['LLM_BASE_URL']:
ai_client = openai.OpenAI(
api_key=current_app.config['LLM_API_KEY'],
base_url=current_app.config['LLM_BASE_URL']
)
else:
logging.info("Using default OpenAI provider")
ai_client = openai.OpenAI(
api_key=current_app.config['LLM_API_KEY'],
)
prompt = "Write a challenge statement for a hackathon project about the following: " +\
'\n\n%s\n%s\n%s' % (title, topic, summary)
response = client.chat.completions.create(
model=current_app.config['LLM_MODEL'],
timeout=REQUEST_TIMEOUT,
messages = [
{
"role": "user",
"content": prompt
}
])
jsondata = response.json()
print (jsondata)
if 'choices' in jsondata and len(jsondata['choices']) > 0:
return jsondata['choices'][0]['message']['content']
else:
logging.error('No ChatGPT data')
print (jsondata)
logging.error('No LLM data in response')
print(jsondata)
return None

View file

@ -76,8 +76,10 @@ class Config(object):
TIME_ZONE = os_env.get('TIME_ZONE', 'UTC')
MAX_CONTENT_LENGTH = int(os_env.get('MAX_CONTENT_LENGTH', 1 * 1024 * 1024))
# Configure other external APIs
# Configure an external LLM API
LLM_MODEL = os_env.get('LLM_MODEL', 'gpt-3.5-turbo')
LLM_API_KEY = os_env.get('LLM_API_KEY', '')
LLM_BASE_URL = os_env.get('LLM_BASE_URL', '')
# Configure web analytics providers
ANALYTICS_HREF = os_env.get('ANALYTICS_HREF', None)

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 66 KiB

151
poetry.lock generated
View file

@ -49,6 +49,28 @@ files = [
[package.dependencies]
typing-extensions = {version = ">=4.0.0", markers = "python_version < \"3.9\""}
[[package]]
name = "anyio"
version = "4.4.0"
description = "High level compatibility layer for multiple asynchronous event loop implementations"
optional = false
python-versions = ">=3.8"
files = [
{file = "anyio-4.4.0-py3-none-any.whl", hash = "sha256:c1b2d8f46a8a812513012e1107cb0e68c17159a7a594208005a57dc776e1bdc7"},
{file = "anyio-4.4.0.tar.gz", hash = "sha256:5aadc6a1bbb7cdb0bede386cac5e2940f5e2ff3aa20277e991cf028e0585ce94"},
]
[package.dependencies]
exceptiongroup = {version = ">=1.0.2", markers = "python_version < \"3.11\""}
idna = ">=2.8"
sniffio = ">=1.1"
typing-extensions = {version = ">=4.1", markers = "python_version < \"3.11\""}
[package.extras]
doc = ["Sphinx (>=7)", "packaging", "sphinx-autodoc-typehints (>=1.2.0)", "sphinx-rtd-theme"]
test = ["anyio[trio]", "coverage[toml] (>=7)", "exceptiongroup (>=1.2.0)", "hypothesis (>=4.0)", "psutil (>=5.9)", "pytest (>=7.0)", "pytest-mock (>=3.6.1)", "trustme", "uvloop (>=0.17)"]
trio = ["trio (>=0.23)"]
[[package]]
name = "async-timeout"
version = "4.0.3"
@ -203,8 +225,8 @@ files = [
jmespath = ">=0.7.1,<2.0.0"
python-dateutil = ">=2.1,<3.0.0"
urllib3 = [
{version = ">=1.25.4,<1.27", markers = "python_version < \"3.10\""},
{version = ">=1.25.4,<2.2.0 || >2.2.0,<3", markers = "python_version >= \"3.10\""},
{version = ">=1.25.4,<1.27", markers = "python_version < \"3.10\""},
]
[package.extras]
@ -570,6 +592,17 @@ files = [
{file = "cssselect-1.2.0.tar.gz", hash = "sha256:666b19839cfaddb9ce9d36bfe4c969132c647b92fc9088c4e23f786b30f1b3dc"},
]
[[package]]
name = "distro"
version = "1.9.0"
description = "Distro - an OS platform information API"
optional = false
python-versions = ">=3.6"
files = [
{file = "distro-1.9.0-py3-none-any.whl", hash = "sha256:7bffd925d65168f85027d8da9af6bddab658135b840670a223589bc0c8ef02b2"},
{file = "distro-1.9.0.tar.gz", hash = "sha256:2fa77c6fd8940f116ee1d6b94a2f90b13b5ea8d019b98bc8bafdcabcdd9bdbed"},
]
[[package]]
name = "dnspython"
version = "2.6.1"
@ -1122,8 +1155,8 @@ files = [
[package.dependencies]
cffi = {version = ">=1.12.2", markers = "platform_python_implementation == \"CPython\" and sys_platform == \"win32\""}
greenlet = [
{version = ">=2.0.0", markers = "platform_python_implementation == \"CPython\" and python_version < \"3.11\""},
{version = ">=3.0rc3", markers = "platform_python_implementation == \"CPython\" and python_version >= \"3.11\""},
{version = ">=2.0.0", markers = "platform_python_implementation == \"CPython\" and python_version < \"3.11\""},
]
"zope.event" = "*"
"zope.interface" = "*"
@ -1273,6 +1306,17 @@ setproctitle = ["setproctitle"]
testing = ["coverage", "eventlet", "gevent", "pytest", "pytest-cov"]
tornado = ["tornado (>=0.2)"]
[[package]]
name = "h11"
version = "0.14.0"
description = "A pure-Python, bring-your-own-I/O implementation of HTTP/1.1"
optional = false
python-versions = ">=3.7"
files = [
{file = "h11-0.14.0-py3-none-any.whl", hash = "sha256:e3fe4ac4b851c468cc8363d500db52c2ead036020723024a109d37346efaa761"},
{file = "h11-0.14.0.tar.gz", hash = "sha256:8f19fbbe99e72420ff35c00b27a34cb9937e902a8b810e2c88300c6f0a3b699d"},
]
[[package]]
name = "hiredis"
version = "2.3.2"
@ -1391,6 +1435,51 @@ files = [
{file = "hiredis-2.3.2.tar.gz", hash = "sha256:733e2456b68f3f126ddaf2cd500a33b25146c3676b97ea843665717bda0c5d43"},
]
[[package]]
name = "httpcore"
version = "1.0.5"
description = "A minimal low-level HTTP client."
optional = false
python-versions = ">=3.8"
files = [
{file = "httpcore-1.0.5-py3-none-any.whl", hash = "sha256:421f18bac248b25d310f3cacd198d55b8e6125c107797b609ff9b7a6ba7991b5"},
{file = "httpcore-1.0.5.tar.gz", hash = "sha256:34a38e2f9291467ee3b44e89dd52615370e152954ba21721378a87b2960f7a61"},
]
[package.dependencies]
certifi = "*"
h11 = ">=0.13,<0.15"
[package.extras]
asyncio = ["anyio (>=4.0,<5.0)"]
http2 = ["h2 (>=3,<5)"]
socks = ["socksio (==1.*)"]
trio = ["trio (>=0.22.0,<0.26.0)"]
[[package]]
name = "httpx"
version = "0.27.0"
description = "The next generation HTTP client."
optional = false
python-versions = ">=3.8"
files = [
{file = "httpx-0.27.0-py3-none-any.whl", hash = "sha256:71d5465162c13681bff01ad59b2cc68dd838ea1f10e51574bac27103f00c91a5"},
{file = "httpx-0.27.0.tar.gz", hash = "sha256:a0cb88a46f32dc874e04ee956e4c2764aba2aa228f650b06788ba6bda2962ab5"},
]
[package.dependencies]
anyio = "*"
certifi = "*"
httpcore = "==1.*"
idna = "*"
sniffio = "*"
[package.extras]
brotli = ["brotli", "brotlicffi"]
cli = ["click (==8.*)", "pygments (==2.*)", "rich (>=10,<14)"]
http2 = ["h2 (>=3,<5)"]
socks = ["socksio (==1.*)"]
[[package]]
name = "idna"
version = "3.7"
@ -1923,6 +2012,29 @@ rsa = ["cryptography (>=3.0.0)"]
signals = ["blinker (>=1.4.0)"]
signedtoken = ["cryptography (>=3.0.0)", "pyjwt (>=2.0.0,<3)"]
[[package]]
name = "openai"
version = "1.35.13"
description = "The official Python library for the openai API"
optional = false
python-versions = ">=3.7.1"
files = [
{file = "openai-1.35.13-py3-none-any.whl", hash = "sha256:36ec3e93e0d1f243f69be85c89b9221a471c3e450dfd9df16c9829e3cdf63e60"},
{file = "openai-1.35.13.tar.gz", hash = "sha256:c684f3945608baf7d2dcc0ef3ee6f3e27e4c66f21076df0b47be45d57e6ae6e4"},
]
[package.dependencies]
anyio = ">=3.5.0,<5"
distro = ">=1.7.0,<2"
httpx = ">=0.23.0,<1"
pydantic = ">=1.9.0,<3"
sniffio = "*"
tqdm = ">4"
typing-extensions = ">=4.7,<5"
[package.extras]
datalib = ["numpy (>=1)", "pandas (>=1.2.3)", "pandas-stubs (>=1.1.0.11)"]
[[package]]
name = "packaging"
version = "24.1"
@ -2118,8 +2230,8 @@ files = [
annotated-types = ">=0.4.0"
pydantic-core = "2.20.1"
typing-extensions = [
{version = ">=4.6.1", markers = "python_version < \"3.13\""},
{version = ">=4.12.2", markers = "python_version >= \"3.13\""},
{version = ">=4.6.1", markers = "python_version < \"3.13\""},
]
[package.extras]
@ -2738,6 +2850,17 @@ files = [
{file = "six-1.16.0.tar.gz", hash = "sha256:1e61c37477a1626458e36f7b1d82aa5c9b094fa4802892072e49de9c60c4c926"},
]
[[package]]
name = "sniffio"
version = "1.3.1"
description = "Sniff out which async library your code is running under"
optional = false
python-versions = ">=3.7"
files = [
{file = "sniffio-1.3.1-py3-none-any.whl", hash = "sha256:2f6da418d1f1e0fddd844478f41680e794e6051915791a034ff65e5f100525a2"},
{file = "sniffio-1.3.1.tar.gz", hash = "sha256:f4324edc670a0f49750a81b895f35c3adb843cca46f0530f79fc1babb23789dc"},
]
[[package]]
name = "snowballstemmer"
version = "2.2.0"
@ -2935,6 +3058,26 @@ files = [
{file = "tomli-2.0.1.tar.gz", hash = "sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f"},
]
[[package]]
name = "tqdm"
version = "4.66.4"
description = "Fast, Extensible Progress Meter"
optional = false
python-versions = ">=3.7"
files = [
{file = "tqdm-4.66.4-py3-none-any.whl", hash = "sha256:b75ca56b413b030bc3f00af51fd2c1a1a5eac6a0c1cca83cbb37a5c52abce644"},
{file = "tqdm-4.66.4.tar.gz", hash = "sha256:e4d936c9de8727928f3be6079590e97d9abfe8d39a590be678eb5919ffc186bb"},
]
[package.dependencies]
colorama = {version = "*", markers = "platform_system == \"Windows\""}
[package.extras]
dev = ["pytest (>=6)", "pytest-cov", "pytest-timeout", "pytest-xdist"]
notebook = ["ipywidgets (>=6)"]
slack = ["slack-sdk"]
telegram = ["requests"]
[[package]]
name = "typer"
version = "0.11.1"
@ -3216,4 +3359,4 @@ testing = ["coverage (>=5.0.3)", "zope.event", "zope.testing"]
[metadata]
lock-version = "2.0"
python-versions = ">=3.8,<4"
content-hash = "302f355689c1a43340ad1b9efde1797e0ea4811549db17555077d71f78591782"
content-hash = "6e1d4f934f0d8800c2a4f2e594e6f9633233dd834c480a55c2570aa7c0de323e"

View file

@ -51,6 +51,7 @@ urllib3 = "<2"
werkzeug = "^3.0"
whitenoise = "^5.3"
wtforms = "^3.0"
openai = "^1.35.13"
[tool.poetry.dev-dependencies]
factory-boy = "*"

View file

@ -1,5 +1,7 @@
alembic==1.13.2 ; python_version >= "3.8" and python_version < "4"
aniso8601==9.0.1 ; python_version >= "3.8" and python_version < "4"
annotated-types==0.7.0 ; python_version >= "3.8" and python_version < "4"
anyio==4.4.0 ; python_version >= "3.8" and python_version < "4"
async-timeout==4.0.3 ; python_version >= "3.8" and python_full_version <= "3.11.2"
attrs==23.2.0 ; python_version >= "3.8" and python_version < "4"
bcrypt==4.1.3 ; python_version >= "3.8" and python_version < "4"
@ -16,8 +18,10 @@ click==8.1.7 ; python_version >= "3.8" and python_version < "4"
colorama==0.4.6 ; python_version >= "3.8" and python_version < "4"
cssmin==0.2.0 ; python_version >= "3.8" and python_version < "4"
cssselect==1.2.0 ; python_version >= "3.8" and python_version < "4"
distro==1.9.0 ; python_version >= "3.8" and python_version < "4"
dnspython==2.6.1 ; python_version >= "3.8" and python_version < "4"
email-validator==1.3.1 ; python_version >= "3.8" and python_version < "4"
exceptiongroup==1.2.1 ; python_version >= "3.8" and python_version < "3.11"
flask-assets==2.1.0 ; python_version >= "3.8" and python_version < "4"
flask-bcrypt==1.0.1 ; python_version >= "3.8" and python_version < "4"
flask-caching==2.3.0 ; python_version >= "3.8" and python_version < "4"
@ -38,9 +42,12 @@ gevent==24.2.1 ; python_version >= "3.8" and python_version < "4"
graphene==3.3 ; python_version >= "3.8" and python_version < "4"
graphql-core==3.2.3 ; python_version >= "3.8" and python_version < "4"
graphql-relay==3.2.0 ; python_version >= "3.8" and python_version < "4"
greenlet==3.0.3 ; python_version >= "3.8" and (platform_machine == "aarch64" or platform_machine == "ppc64le" or platform_machine == "x86_64" or platform_machine == "amd64" or platform_machine == "AMD64" or platform_machine == "win32" or platform_machine == "WIN32" or platform_python_implementation == "CPython") and python_version < "4"
greenlet==3.0.3 ; (platform_machine == "aarch64" or platform_machine == "ppc64le" or platform_machine == "x86_64" or platform_machine == "amd64" or platform_machine == "AMD64" or platform_machine == "win32" or platform_machine == "WIN32" or platform_python_implementation == "CPython") and python_version >= "3.8" and python_version < "4"
gunicorn[gevent]==22.0.0 ; python_version >= "3.8" and python_version < "4"
h11==0.14.0 ; python_version >= "3.8" and python_version < "4"
hiredis==2.3.2 ; python_version >= "3.8" and python_version < "4"
httpcore==1.0.5 ; python_version >= "3.8" and python_version < "4"
httpx==0.27.0 ; python_version >= "3.8" and python_version < "4"
idna==3.7 ; python_version >= "3.8" and python_version < "4"
importlib-metadata==8.0.0 ; python_version >= "3.8" and python_version < "3.10"
importlib-resources==6.4.0 ; python_version >= "3.8" and python_version < "3.9"
@ -61,11 +68,14 @@ micawber==0.5.5 ; python_version >= "3.8" and python_version < "4"
misaka==2.1.1 ; python_version >= "3.8" and python_version < "4"
mkdocs-material-extensions==1.3.1 ; python_version >= "3.8" and python_version < "4"
oauthlib==3.2.2 ; python_version >= "3.8" and python_version < "4"
openai==1.35.13 ; python_version >= "3.8" and python_version < "4"
packaging==24.1 ; python_version >= "3.8" and python_version < "4"
petl==1.7.15 ; python_version >= "3.8" and python_version < "4"
pkgutil-resolve-name==1.3.10 ; python_version >= "3.8" and python_version < "3.9"
psycopg2-binary==2.9.9 ; python_version >= "3.8" and python_version < "4"
pycparser==2.22 ; python_version >= "3.8" and python_version < "4"
pydantic-core==2.20.1 ; python_version >= "3.8" and python_version < "4"
pydantic==2.8.2 ; python_version >= "3.8" and python_version < "4"
pygments==2.18.0 ; python_version >= "3.8" and python_version < "4"
pyquery==2.0.0 ; python_version >= "3.8" and python_version < "4"
pystache==0.6.5 ; python_version >= "3.8" and python_version < "4"
@ -86,12 +96,14 @@ setuptools==70.3.0 ; python_version >= "3.8" and python_version < "4"
shellingham==1.5.4 ; python_version >= "3.8" and python_version < "4"
simpleeval==0.9.13 ; python_version >= "3.8" and python_version < "4"
six==1.16.0 ; python_version >= "3.8" and python_version < "4"
sniffio==1.3.1 ; python_version >= "3.8" and python_version < "4"
sqlalchemy-continuum==1.4.2 ; python_version >= "3.8" and python_version < "4"
sqlalchemy-utils==0.41.2 ; python_version >= "3.8" and python_version < "4"
sqlalchemy==1.4.52 ; python_version >= "3.8" and python_version < "4"
stringcase==1.2.0 ; python_version >= "3.8" and python_version < "4"
tabulate==0.9.0 ; python_version >= "3.8" and python_version < "4"
text-unidecode==1.3 ; python_version >= "3.8" and python_version < "4"
tqdm==4.66.4 ; python_version >= "3.8" and python_version < "4"
typer==0.11.1 ; python_version >= "3.8" and python_version < "4"
typer[all]==0.11.1 ; python_version >= "3.8" and python_version < "4"
typing-extensions==4.12.2 ; python_version >= "3.8" and python_version < "4"

View file

@ -5,6 +5,7 @@ import pytest
import json
from dribdat.aggregation import ProjectActivity
from dribdat.apigenerate import gen_project_pitch
from dribdat.user.models import Event
from dribdat.public.api import *
@ -97,3 +98,9 @@ class TestApi:
# Test Project search
ppj = json.loads(projects_top_json().get_data())
assert len(ppj['projects']) == 1
def test_get_autochallenge(self):
project = ProjectFactory()
project.longtext = "Smeeagain"
project.save()
assert "Smee" in gen_project_pitch(project)