> ## Documentation Index
> Fetch the complete documentation index at: https://docs.athina.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# OpenAI Chat Completion

> If you're using OpenAI chat completions in Python, you can get set up in just 2 minutes

<Tabs>
  <Tab title="OpenAI Wrapper (Easy)">
    #### 1. Install the Python SDK

    Run `pip install athina-logger`

    #### 2. Import Athina Logger

    Replace your `import openai` with this:

    ```python
    from athina_logger.api_key import AthinaApiKey
    from athina_logger.athina_meta import AthinaMeta
    from athina_logger.openai_wrapper import openai

    client = openai.OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
    ```

    #### 3. Set Athina API key

    ```python
    # Initialize the Athina API key somewhere in your code
    AthinaApiKey.set_api_key(os.getenv('ATHINA_API_KEY'))
    ```

    #### 4. Use OpenAI chat completions request as you do normally

    Non streaming example:

    ```python
    messages = [ { "role": "user", "content": "How much funding does Y Combinator provide?" } ]

    # Use client.chat.completions.create just as you would normally
    # Add fields to AthinaMeta for better segmentation of your data
    client.chat.completions.create(
        model="gpt-4",
        messages=messages,
        stream=False,
        athina_meta=AthinaMeta(
            prompt_slug="yc_rag_v1",
            user_query="How much funding does Y Combinator provide?", # For RAG Q&A systems, log the user's query
            context={"information": "Your docs"}, # Your retrieved documents
            session_id="session_id", # Conversation ID
            customer_id="customer_id", # Your Customer's ID
            customer_user_id="customer_user_id", # Your End User's ID
            environment="environment", # Environment (production, staging, dev, etc)
            external_reference_id="ext_ref_123456",
            custom_attributes={
                "name": "John",
                "age": 30,
                "city": "New York"
            } # Your custom-attributes
        ),
    )
    ```

    Streaming example:

    ```python
    messages = [ { "role": "user", "content": "How much funding does Y Combinator provide?" } ]

    stream = client.chat.completions.create(
        model="gpt-4",
        messages=messages,
        stream=True,
        athina_meta=AthinaMeta(
            prompt_slug="yc_rag_v1",
            user_query="How much funding does Y Combinator provide?", # For RAG Q&A systems, log the user's query
            context={"information": retrieved_documents} # Your retrieved documents
            session_id=session_id, # Conversation ID
            customer_id=customer_id, # Your Customer's ID
            customer_user_id=customer_id, # Your End User's ID
            environment=environment, # Environment (production, staging, dev, etc)
            external_reference_id="ext_ref_123456",
            custom_attributes={
                "name": "John",
                "age": 30,
                "city": "New York"
            } # Your custom-attributes
        ),
    )
    for chunk in stream:
        if chunk.choices[0].delta.content is not None:
            print(chunk.choices[0].delta.content, end="")
    ```

    <Note>
      **Note:** We support both `stream=True` and `stream=False` for OpenAI chat
      completions. OpenAI doesn’t provide usage statistics such as prompt and
      completion tokens when streaming. However, We overcomes this limitation by
      getting these with the help of the tiktoken package, which is designed to work
      with all tokenized OpenAI GPT models.
    </Note>
  </Tab>

  <Tab title="API Request">
    #### Log via API Request

    See instructions [here](/api-reference/logging/log-via-api-request).
  </Tab>

  <Tab title="Python SDK">
    #### Log via Python SDK

    See instructions [here](/api-reference/logging/log-via-python-sdk).
  </Tab>

  <Tab title="TypeScript SDK">
    #### Log via TypeScript SDK

    See instructions [here](/api-reference/logging/log-via-typescript-sdk).
  </Tab>
</Tabs>

***

## Frequently Asked Questions

<AccordionGroup>
  <Accordion title="Is this SDK going to make a proxy request to OpenAI through Athina?">
    Nope! We know how important your OpenAI inference call is, so we don't want to interfere with that or increase response times.

    Importing `openai` from athina just makes an async logging request to Athina (separate from your OpenAI request) after you get back the response from `openai`
  </Accordion>

  <Accordion title="Will this SDK increase my latency?">
    Nope. The logging call is being made in a background thread as a fire and forget request, so there is almost no additional latency (\< 5ms).
  </Accordion>

  <Accordion title="What is AthinaMeta">
    The `AthinaMeta` fields are used for segmentation of your data on the dashboard. All these fields are optional, but highly recommended.

    You can view the full list of [logging attributes](/api-reference/logging/logging-attributes) here.

    ```python
    class AthinaMeta:
        prompt_slug: Optional[str] = None
        context: Optional[dict] = None
        customer_id: Optional[dict] = None
        customer_user_id: Optional[dict] = None
        session_id: Optional[dict] = None
        user_query: Optional[dict] = None
        environment: Optional[dict] = None
        external_reference_id: Optional[dict] = None
        customer_id: Optional[str] = None
        customer_user_id: Optional[str] = None
        response_time: Optional[int] = None
        custom_attributes: Optional[dict] = None
    ```
  </Accordion>
</AccordionGroup>
