Skip to main content

Django OpenAI Assistant

Project description

A Django / Celery scalable OpenAI Assistant Runner.

Assumption: you have Django up and running with Celery. (Tested on Redis)

pip install django_openai_assistant

in settings.py Add 'django_openai_assistant' to your INSTALLED_APPS [] array
make sure to have OPENAI_API_KEY defined in settings with your OpenAI key
create and apply migrations for django_openai_assistant

Create a simple Assistant in https://platform.openai.com/assistants To begin you probably want one with no functions. Let's say you called it 'Test Assistant'

Use the assistant in your code:

demo.py

ffrom django_openai_assistant.assistant import assistantTask
from celery import shared_task

 # Define OPENAI_API_KEY in your settings.py file
 # Add 'django_openai_assistant' to your INSTALLED_APPS in settings.py
 # run python manage.py makemigrations django_openai_assistant and then python managey.pymigrate
 # create at least one Assistant in https://platform.openai.com/assistants

def testAssistant(request=None):
    # replace <<your appname>> with the name you django app this file is in!
    # replace <<your assistant name>> with the name of the assistant you created in the OpenAI platform
    task = assistantTask(assistantName="<<your assistant name>>", tools= [], completionCall = "<<your appname>>.test:afterRunFunction")
    task.prompt = "Who was Napoleon Bonaparte?"
    task.createRun() ## this will get everything going!

@shared_task(Name="This will be called once the run is complete")
def afterRunFunction(taskID):
    # Function is called when run is completed. MUST be annoted as @shared_task!!! 
    # start by retrieving the task
    task = assistantTask(run_id = taskID)
    if task.status == 'completed': ## check to make sure it is completed not failed or something else
        print(task.markdownresponse())
    else:
        print('run failed')

See https://medium.com/@jlvalorvc/building-a-scalable-openai-assistant-processor-in-django-with-celery-a61a1af722e0

Version history:

0.4.0

  • redo readme.md
  • standard version numbering
  • task.metadata is now initiatilized as {} to prevent task.metadata.get() from failing. 0.33
  • retrieve by thread_id or run_id openaiTask(run_id='run_xxxx').
  • asmarkdown(string) available as a function outside class
  • retrievefile() to download a file by openai file ID.
  • now processing multi response with embedded image.

0.32

  • small fixes.

0.31

  • made sure that file uploads to openai receive a 'file name' when uploaded.

0.30

  • fix to properly differentiate between two functions that start with the same name like 'company'
  • and 'companyfind' and throw and exception (istead of a pass) when running in Debug mode when calling tool function.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_openai_assistant-0.4.0.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

django_openai_assistant-0.4.0-py2.py3-none-any.whl (10.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django_openai_assistant-0.4.0.tar.gz.

File metadata

File hashes

Hashes for django_openai_assistant-0.4.0.tar.gz
Algorithm Hash digest
SHA256 8d4ca9ae5e40431c35e256c98a43d54bc64056ea291093421eb8550eab35c182
MD5 33f1d86e2713f3d0b9e9d08e5524d329
BLAKE2b-256 16828f475d188d865fe93fe0aa9954af2805ac5c1078e098ce0e124ad634bff6

See more details on using hashes here.

File details

Details for the file django_openai_assistant-0.4.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_openai_assistant-0.4.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 3ac767929d501cb060b113431e08f199d768683ce9fcbe23df83b9e7eb89ca54
MD5 ea7ef5677d15bee8afe5194d4c3c73e1
BLAKE2b-256 741e733f5a8548d35fdd1ca9fbbf850f2e2ddfb468829bf1fd5f2cc4809af19a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page