datastax / astra-assistants-api

Drop in replacement for the OpenAI Assistants API
Apache License 2.0
149 stars 17 forks source link

Chat completion example for Python not working #74

Open cfdude opened 1 day ago

cfdude commented 1 day ago

https://github.com/datastax/astra-assistants-api/blob/main/examples/python/chat_completion/basic.py

This example looks like it was crafted for OpenAI but the model was changed to Anthropic which has different requirements and would not work as coded.

phact commented 18 hours ago

Hi, thanks for filing this issue!

The point of Astra Assistants is to make an openai compatible service that works with any model. Did you try it?

The tests definitely work ~but I haven't run the examples in a while~ and I just tested the completion example.

cfdude commented 13 hours ago

When patch(OpenAI) is called in the code it will go through the process of creating the openai compatible service, but then you get errors in the console as the rest of the code won't function as coded. From a learning perspective, if this is supposed to demonstrate a chat completion it does not do that.

phact commented 13 hours ago

What's your exact error? You need to make sure you pass the anthropic API key by setting it as an environment variable.

phact commented 13 hours ago

Here's my output:

$ poetry run python examples/python/chat_completion/basic.py Patching OpenAI client, it will now communicate to Astra Assistants API: https://open-assistant-ai.astra.datastax.com/v1/ Learn more about Astra at: https://docs.datastax.com/en/astra-db-serverless/tutorials/astra-assistants-api.html prompt> Draw an ASCII art kitten eating icecream artist-anthropic/claude-3-sonnet-20240229> Here's an ASCII art of a kitten eating ice cream:

             ,---.
            /(    )\
           ( \`  / )
            `-/=%\=`
              )== (
             /      \
            /.`\\/'/.\
           //' /\'/ \'\
          / /'       \'\
         / /'        / \
         \_\'       /   \
          \__)     (    (
           \__)     )    \
            \__)   /      \
             \__) /\       \
              \  / \       /
               \/   \     /
                    \ \ /
                     \///
                     //\\
                    //  \\
                    \\  //
                     \\//
                      \/

... ...

Note, you can pass the anthropic API key as an environment variable:

export ANTHROPIC_API_KEY=''

before your python comand

or use a .env file as directed in the readme

cfdude commented 13 hours ago

i'm following the instructions on the readme using poetry for local install, this is my output just following the directions. Yes, I do have the keys in the renamed .env file for OpenAI, Astra as well as Antropic.

(venv) ➜ astra-assistants-api git:(main) ✗ poetry run python examples/python/chat_completion/basic.py Traceback (most recent call last): File "/Users/robsherman/Documents/Repos/astra-assistants-api/examples/python/chat_completion/basic.py", line 47, in client = patch(OpenAI()) ^^^^^^^^ File "/Users/robsherman/Documents/Repos/astra-assistants-api/venv/lib/python3.11/site-packages/openai/_client.py", line 105, in init raise OpenAIError( openai.OpenAIError: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable (venv) ➜ astra-assistants-api git:(main) ✗

phact commented 13 hours ago

That's the OpenAI SDK complaining that your OPENAI_API_KEY env var is not set. Note, even when using anthropic the openai_api_key needs to be set to something, even if it's an invalid key.

To make sure your .env file is getting picked up try the following in your code and see if the keys are set at runtime:

import os
print(os.environ['OPENAI_API_KEY'])

if you want, try setting your keys directly in your code as follows:

os.environ["OPENAI_API_KEY"] = "dummy"