> ## 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)">
    <Steps>
      <Step title="Install the Python SDK">
        Run `pip install athina-logger`
      </Step>

      <Step title="Configure API key">
        ```python
          from athina_logger.api_key import AthinaApiKey
          from athina_logger.openai_wrapper import openai

          AthinaApiKey.set_api_key(os.getenv('ATHINA_API_KEY'))
          openai.api_key = os.getenv('OPENAI_API_KEY')
        ```
      </Step>

      <Step title="Replace your OpenAI import">
        ```python
          from athina_logger.openai_wrapper import openai
        ```
      </Step>

      <Step title="Add metadata fields">
        Use OpenAI as your normally would, but optionally add `AthinaMeta` fields for better segmentation on the platform.

        ```python
        from athina_logger.athina_meta import AthinaMeta
        messages = [ { "role": "user", "content": "How much funding does Y Combinator provide?" } ]

        # Use openai.ChatCompletion just as you would normally
        # Add fields to AthinaMeta for better segmentation of your data
        openai.ChatCompletion.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": 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
          ),
        )
        ```
      </Step>
    </Steps>

    <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>
