Code Monkey home page Code Monkey logo

astra-assistants-api's Introduction

Astra Assistant API Service

Run tests Docker build and publish

A drop-in compatible service for the OpenAI beta Assistants API with support for persistent threads, files, assistants, streaming, retreival, function calling and more using AstraDB (DataStax's db as a service offering powered by Apache Cassandra and jvector).

Compatible with existing OpenAI apps via the OpenAI SDKs by changing a single line of code.

You can use our Astra Assistants service, or host the API server yourself.

Client Getting Started

To build an app that uses the Astra Asistants service install the streaming-assistants dependency with your favorite package manager:

poetry add streaming_assistants

Signup for Astra and get an Admin API token:

Set your environment variables (depending on what LLMs you want to use), see the .env.bkp file for an example:

#!/bin/bash

# astra has a generous free tier, no cc required 
# https://astra.datastax.com/ --> tokens --> administrator user --> generate
export ASTRA_DB_APPLICATION_TOKEN=
# https://platform.openai.com/api-keys --> create new secret key
export OPENAI_API_KEY=

# https://www.perplexity.ai/settings/api  --> generate
export PERPLEXITYAI_API_KEY=

# https://dashboard.cohere.com/api-keys
export COHERE_API_KEY=

#bedrock models https://docs.aws.amazon.com/bedrock/latest/userguide/setting-up.html
export AWS_REGION_NAME=
export AWS_ACCESS_KEY_ID=
export AWS_SECRET_ACCESS_KEY=

#vertexai models https://console.cloud.google.com/vertex-ai
export GOOGLE_JSON_PATH=
export GOOGLE_PROJECT_ID=

#gemini api https://makersuite.google.com/app/apikey
export GEMINI_API_KEY=

#anthropic claude models https://console.anthropic.com/settings/keys
export ANTHROPIC_API_KEY=

Then import and patch your client:

from openai import OpenAI
from streaming_assistants import patch
client = patch(OpenAI())

The system will create a db on your behalf and name it assistant_api_db using your token. Note, this means that the first request will hang until your db is ready (could be a couple of minutes). This will only happen once.

Now you're ready to create an assistant

assistant = client.beta.assistants.create(
  instructions="You are a personal math tutor. When asked a math question, write and run code to answer the question.",
  model="gpt-4-1106-preview",
  tools=[{"type": "retrieval"}]
)

By default, the service uses AstraDB as the database/vector store and OpenAI for embeddings and chat completion.

Third party LLM Support

We now support many third party models for both embeddings and completion thanks to litellm. Pass the api key of your service using api-key and embedding-model headers.

You can pass different models, just make sure you have the right corresponding api key in your environment.

model="gpt-4-1106-preview"
#model="gpt-3.5-turbo"
#model="cohere_chat/command-r"
#model="perplexity/mixtral-8x7b-instruct"
#model="perplexity/llama-3-sonar-large-32k-online"
#model="anthropic.claude-v2"
#model="gemini/gemini-pro"
#model = "meta.llama2-13b-chat-v1"

assistant = client.beta.assistants.create(
    name="Math Tutor",
    instructions="You are a personal math tutor. Answer questions briefly, in a sentence or less.",
    model=model,
)

for third party embedding models we support embedding_model in client.files.create:

file = client.files.create(
    file=open(
        "./test/language_models_are_unsupervised_multitask_learners.pdf",
        "rb",
    ),
    purpose="assistants",
    embedding_model="text-embedding-3-large",
)

To run the examples using poetry create a .env file in this directory with your secrets and run:

poetry install

Create your .env file and add your keys to it:

cp .env.bkp .env

and

poetry run python examples/python/chat_completion/basic.py

poetry run python examples/python/retrieval/basic.py

poetry run python examples/python/streaming_retrieval/basic.py

poetry run python examples/python/function_calling/basic.py

Running yourself

with docker:

docker run datastax/astra-assistants

or locally with poetry:

poetry install

poetry run python run.py

Contributing

Check out our contributing guide

Coverage

See our coverage report here

Roadmap:

  • Support for other embedding models and LLMs
  • function calling
  • Streaming support
  • Configurable RAG

astra-assistants-api's People

Contributors

alexleventer avatar dependabot[bot] avatar jgillenwater avatar phact avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

astra-assistants-api's Issues

[Question] Functions with other models, setting the model in run.create, more

Does function calling work with other models such as gemini?

Is it possible to change the model of the assistant at the run?

run = await self.beta.threads.runs.create(
            thread_id=thread.id,
            assistant_id=assistant_id
            model="gemini/gemini-pro"
        )

I already have assistants in the playground, do I need to create them since you are possibly storing it in your database?

Do you use my data?

What is stored on AstraDB? (edited)

API Error When Assistants Do Not Utilize Attached Tools

Description

When adding tools to an assistant, the API throws an error if the assistant does not utilize them. In OpenAI, assistants can choose whether to use tools or not. This issue originates from the threads.py create_run method.

Steps to Reproduce

  1. Create an assistant with any custom tool. (function)
  2. Ask the assistant to say "hi."
  3. Observe the error: "Could not extract function arguments from LLM response, may have not been properly formatted. Consider retrying or use a different model."

Expected Behavior

In OpenAI, tools can be attached to an assistant, allowing it to decide whether or not to use them. This autonomy in tool utilization is crucial for any agentic system.

Actual Behavior

The operation fails with the error:
ValueError("Could not extract function arguments from LLM response, may have not been properly formatted. Consider retrying or use a different model.")

Additional Information

  • OpenAI version: 1.20.0
  • Python version: 3.10.13
  • Streaming assistants version: 0.16.0
  • Operating System: macOS 14.1.1

Permission Denied Error when using with_options in OpenAI client

Description

When attempting to create a file using the OpenAI Client with custom options set via with_options, a PermissionDeniedError is thrown indicating a missing authentication token in the header.

Steps to Reproduce

  1. Initialize the OpenAI Client and patch it.
  2. Attempt to create a file using the following code:
    file_id = self.client.with_options(
        timeout=80 * 1000,  # Set a custom timeout
    ).files.create(file=f, purpose="assistants").id
  3. Observe the PermissionDeniedError with a 403 status code indicating a missing 'astra-api-token'.

Expected Behavior

The file creation should proceed without any permission errors, utilizing the provided options including the custom timeout.

Actual Behavior

The operation fails with the following error:

PermissionDeniedError: Error code: 403 - {'detail': 'Must pass an astradb token in the astra-api-token header'}

Additional Information

  • Removing the with_options method call and directly using files.create() without any options works correctly, suggesting an issue with how headers or tokens are managed when with_options is used.
  • OpenAI version: 1.21.2
  • Python version: 3.10.13
  • Operating System: macOS 14.1.1

Assistants V2

https://platform.openai.com/docs/assistants/whats-new

Tasks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.