Integrating with OpenAI

What I’m trying to do:
I am trying to create a OpenAI application that takes two inputs and generates a text. I keep getting this error.

Is there some sort of anvil tutorial i can follow?

`NameError: name 'client' is not defined`

* `at ServerModule1, line 14`
* `called from Home, line 18`

What I’ve tried and what’s not working:

import openai

api_key = "API KEY"

@anvil.server.callable
def generate_dream(input):
    messages = [
      {"role":"system",
      "content": """You are a dream interpertor. Write a 2 paragraph response in a personal tone. You will use dream title and dream description to interprete the dreams of the user. """
    }
  ]
    messages.append({"role": "user", "content": f"{input}"})
    completion = client.chat.completions.create(
    model="gpt-3.5-turbo",
    messages=messages
  )

    reply = completion.choices[0].message.content
    return reply

I followed this guy on Youtube

Clone link:
share a copy of your app

The code in the tutorial video does not have this line written this way:
image
Below the video he left a link in the description to his website containing the code:

Python says “name error” because you are calling something that does not exist.

I remember what I did. I tried his code to and it failed

APIRemovedInV1: You tried to access openai.ChatCompletion, but this is no longer supported in openai>=1.0.0 - see the README at https://github.com/openai/openai-python for the API. You can run `openai migrate` to automatically upgrade your codebase to use the 1.0.0 interface. Alternatively, you can pin your installation to the old version, e.g. `pip install openai==0.28` A detailed migration guide is available here: https://github.com/openai/openai-python/discussions/742
at /home/anvil/.env/lib/python3.10/site-packages/openai/lib/_old_api.py:39
called from ServerModule1, line 14
called from Home, line 18

I went on documentation and tried to mess around with it and couldnt get it to work. I figured if someone here knew how to integrate it.

@ianb I rewrote my code again in the way OpenAI API documentation advised.

Link: https://platform.openai.com/docs/guides/text-generation/completions-api
just incase I did something wrong

What I am stuck on is the API_Key.

How do I create a new python file and reference it in anvil?

client.api_key = config.OPENAI_API_KEY

Where do i save the API key so can i use it here. Its not recommended to hardcode it like i did before. Do I save it in another form and add it? Can you please share some sort of tutorial or something?

from openai import OpenAI


client = OpenAI()
client.api_key = config.OPENAI_API_KEY

@anvil.server.callable
def generate_dream(input):
    messages = [
      {"role":"system",
      "content": """You are a dream interpertor. Write a 2 paragraph response in a personal tone. You will use dream title and dream description to interprete the dreams of the user. """
    }
  ]
    messages.append({"role": "user", "content": f"{input}"})
    completion = client.completions.create(
    model="gpt-3.5-turbo",
    prompt=messages
  )

    reply = completion.choices[0].message.content
    return reply

Probably best to just make it an anvil secret:

2 Likes

Ok so I tried to use Secrets for API and am getting this error?

anvil.secrets.SecretError: This app's secrets have been cleared. Please reset their values in the App Secrets config. (GFNEKWTJLRL6R4KE)
<running on the server>
called from /downlink-sources/downlink-2024-01-26-10-46-10/anvil/secrets.py:5
called from ServerModule1, line 6

This code is on the server module

import anvil.secrets

OPENAI_API_KEY = anvil.secrets.get_secret("API_Key")
client = OpenAI(OPENAI_API_KEY)


Instructions for doing this should be in the Documentation that @duncan_richards12 referenced above.

2 Likes

Thank you. I finally got it to work :smile: