# embedding_metadata

key: _node_content
string_value:
{"id_": "755272ac-ae4d-430a-8dec-7619bbb914ea", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/functions.ipynb", "title": "Azure functions example", "tokens": 1653, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "6ab98de4-bf90-406c-858b-6a08d9ada54e", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/functions.ipynb", "title": "Azure functions example", "tokens": 1653, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "d0de622efed18c3134dc1bb74fd204f24f1823f6c027a442308bf7a6d2b7b14b", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "50e73423-5a5e-46b4-a4d2-a6450dea57d3", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/functions.ipynb", "title": "Azure functions example", "tokens": 1653, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "7118d8c34a95c44be8919d00f9ec978312c9da69e01e776ae3f6550bead40c98", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 4418, "end_char_idx": 7562, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
functions = [
    {
        "name": "get_current_weather",
        "description": "Get the current weather",
        "parameters": {
            "type": "object",
            "properties": {
                "location": {
                    "type": "string",
                    "description": "The city and state, e.g. San Francisco, CA",
                },
                "format": {
                    "type": "string",
                    "enum": ["celsius", "fahrenheit"],
                    "description": "The temperature unit to use. Infer this from the users location.",
                },
            },
            "required": ["location"],
        },
    }
]
```

#### 2. Pass function definition(s) into chat completions API

Now we can pass the function into the chat completions API. If the model determines it should call the function, a `finish_reason` of "tool_calls" will be populated on the choice and the details of which function to call and its arguments will be present in the `message`. Optionally, you can set the `tool_choice` keyword argument to force the model to call a particular function (e.g. `{"type": "function", "function": {"name": get_current_weather}}`). By default, this is set to `auto`, allowing the model to choose whether to call the function or not. 


```python
messages = [
    {"role": "system", "content": "Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous."},
    {"role": "user", "content": "What's the weather like today in Seattle?"}
]

chat_completion = client.chat.completions.create(
    model=deployment,
    messages=messages,
    tools=functions,
)
print(chat_completion)
```

#### 3. Call function with arguments from the response

The name of the function call will be one that was provided initially and the arguments will include JSON matching the schema included in the function definition.


```python
import json

def get_current_weather(request):
    """
    This function is for illustrative purposes.
    The location and unit should be used to determine weather
    instead of returning a hardcoded response.
    """
    location = request.get("location")
    unit = request.get("unit")
    return {"temperature": "22", "unit": "celsius", "description": "Sunny"}

function_call = chat_completion.choices[0].message.tool_calls[0].function
print(function_call.name)
print(function_call.arguments)

if function_call.name == "get_current_weather":
    response = get_current_weather(json.loads(function_call.arguments))
```

#### 4. Feed function response back into chat completions API

The response from the function should be serialized into a new message with the role set to "function". Now the model will use the response data to formulate its answer.


```python
messages.append(
    {
        "role": "function",
        "name": "get_current_weather",
        "content": json.dumps(response)
    }
)

function_completion = client.chat.completions.create(
    model=deployment,
    messages=messages,
    tools=functions,
)

print(function_completion.choices[0].message.content.strip())
```


key: _node_content
string_value:
{"id_": "6d701d10-1e79-4e3f-bfe2-6c96e8e2183b", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 2205, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "772f6b12-dd18-4220-a289-39a4569d4087", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 2205, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "4ec30e7d81fd63c5649ef3e786352185e9512594ae626d36f61e5d4fed8d7782", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "1a451c06-a258-4337-93c5-ca02e8a52489", "node_type": "1", "metadata": {}, "hash": "c4fa17786a418259288dfdebf63475528419c324e41bdab2fea2918b1f23da29", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 0, "end_char_idx": 3192, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
# Azure chat completion models with your own data (preview)
This example shows how to use Azure OpenAI service models with your own data. The feature is currently in preview. 

Azure OpenAI on your data enables you to run supported chat models such as GPT-3.5-Turbo and GPT-4 on your data without needing to train or fine-tune models. Running models on your data enables you to chat on top of, and analyze your data with greater accuracy and speed. One of the key benefits of Azure OpenAI on your data is its ability to tailor the content of conversational AI. Because the model has access to, and can reference specific sources to support its responses, answers are not only based on its pretrained knowledge but also on the latest information available in the designated data source. This grounding data also helps the model avoid generating responses based on outdated or incorrect information.

Azure OpenAI on your own data with Azure AI Search (f.k.a. Azure Cognitive Search) provides a customizable, pre-built solution for knowledge retrieval, from which a conversational AI application can be built. To see alternative methods for knowledge retrieval and semantic search, check out the cookbook examples for [vector databases](https://github.com/openai/openai-cookbook/tree/main/examples/vector_databases).

## How it works

[Azure OpenAI on your own data](https://learn.microsoft.com/azure/ai-services/openai/concepts/use-your-data) connects the model with your data, giving it the ability to retrieve and utilize data in a way that enhances the model's output. Together with Azure AI Search, data is retrieved from designated data sources based on the user input and provided conversation history. The data is then augmented and resubmitted as a prompt to the model, giving the model contextual information it can use to generate a response.

See the [Data, privacy, and security for Azure OpenAI Service](https://learn.microsoft.com/legal/cognitive-services/openai/data-privacy?context=%2Fazure%2Fai-services%2Fopenai%2Fcontext%2Fcontext) for more information.

## Prerequisites
To get started, we'll cover a few prequisites. 

To properly access the Azure OpenAI Service, we need to create the proper resources at the [Azure Portal](https://portal.azure.com) (you can check a detailed guide on how to do this in the [Microsoft Docs](https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource?pivots=web-portal))

To use your own data with Azure OpenAI models, you will need:

1. Azure OpenAI access and a resource with a chat model deployed (for example, GPT-3 or GPT-4)
2. Azure AI Search (f.k.a. Azure Cognitive Search) resource
3. Azure Blob Storage resource
4. Your documents to be used as data (See [data source options](https://learn.microsoft.com/azure/ai-services/openai/concepts/use-your-data#data-source-options))


For a full walk-through on how to upload your documents to blob storage and create an index using the Azure AI Studio, see this [Quickstart](https://learn.microsoft.com/azure/ai-services/openai/use-your-data-quickstart?pivots=programming-language-studio&tabs=command-line).

## Setup

First, we install the necessary dependencies.


key: _node_content
string_value:
{"id_": "1a451c06-a258-4337-93c5-ca02e8a52489", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 2205, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "772f6b12-dd18-4220-a289-39a4569d4087", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 2205, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "4ec30e7d81fd63c5649ef3e786352185e9512594ae626d36f61e5d4fed8d7782", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "6d701d10-1e79-4e3f-bfe2-6c96e8e2183b", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 2205, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "7aef50c3d7ef051a664739e113f6a0c8ff77fb050e20279adfaf9e02043309d0", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "e103ab6c-8f65-41b1-9c56-fddc0f7a41ca", "node_type": "1", "metadata": {}, "hash": "a3e8d8cd5fac279152ab544faffcebfaffcebe56c69d828e9bf7a069e894ec28", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 2862, "end_char_idx": 6104, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
For a full walk-through on how to upload your documents to blob storage and create an index using the Azure AI Studio, see this [Quickstart](https://learn.microsoft.com/azure/ai-services/openai/use-your-data-quickstart?pivots=programming-language-studio&tabs=command-line).

## Setup

First, we install the necessary dependencies.


```python
! pip install "openai>=1.0.0,<2.0.0"
! pip install python-dotenv
```

In this example, we'll use `dotenv` to load our environment variables. To connect with Azure OpenAI and the Search index, the following variables should be added to a `.env` file in `KEY=VALUE` format:

* `AZURE_OPENAI_ENDPOINT` - the Azure OpenAI endpoint. This can be found under "Keys and Endpoints" for your Azure OpenAI resource in the Azure Portal.
* `AZURE_OPENAI_API_KEY` - the Azure OpenAI API key. This can be found under "Keys and Endpoints" for your Azure OpenAI resource in the Azure Portal. Omit if using Azure Active Directory authentication (see below `Authentication using Microsoft Active Directory`)
* `SEARCH_ENDPOINT` - the AI Search endpoint. This URL be found on the "Overview" of your Search resource on the Azure Portal.
* `SEARCH_KEY` - the AI Search API key. Found under "Keys" for your Search resource in the Azure Portal.
* `SEARCH_INDEX_NAME` - the name of the index you created with your own data.


```python
import os
import openai
import dotenv

dotenv.load_dotenv()
```

### Authentication

The Azure OpenAI service supports multiple authentication mechanisms that include API keys and Azure Active Directory token credentials.


```python
use_azure_active_directory = False  # Set this flag to True if you are using Azure Active Directory
```

#### Authentication using API key

To set up the OpenAI SDK to use an *Azure API Key*, we need to set `api_key` to a key associated with your endpoint (you can find this key in *"Keys and Endpoints"* under *"Resource Management"* in the [Azure Portal](https://portal.azure.com)). You'll also find the endpoint for your resource here.


```python
if not use_azure_active_directory:
    endpoint = os.environ["AZURE_OPENAI_ENDPOINT"]
    api_key = os.environ["AZURE_OPENAI_API_KEY"]
    # set the deployment name for the model we want to use
    deployment = "<deployment-id-of-the-model-to-use>"

    client = openai.AzureOpenAI(
        base_url=f"{endpoint}/openai/deployments/{deployment}/extensions",
        api_key=api_key,
        api_version="2023-09-01-preview"
    )
```

#### Authentication using Azure Active Directory
Let's now see how we can autheticate via Azure Active Directory. We'll start by installing the `azure-identity` library. This library will provide the token credentials we need to authenticate and help us build a token credential provider through the `get_bearer_token_provider` helper function. It's recommended to use `get_bearer_token_provider` over providing a static token to `AzureOpenAI` because this API will automatically cache and refresh tokens for you. 

For more information on how to set up Azure Active Directory authentication with Azure OpenAI, see the [documentation](https://learn.microsoft.com/azure/ai-services/openai/how-to/managed-identity).


```python
! pip install "azure-identity>=1.15.0"
```


key: _node_content
string_value:
{"id_": "e103ab6c-8f65-41b1-9c56-fddc0f7a41ca", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 2205, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "772f6b12-dd18-4220-a289-39a4569d4087", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 2205, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "4ec30e7d81fd63c5649ef3e786352185e9512594ae626d36f61e5d4fed8d7782", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "1a451c06-a258-4337-93c5-ca02e8a52489", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 2205, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "55dbd35d26b7cb6c91bd433bdc23e42ff51c94a192f946cba281ff85130fe50a", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "e0c43395-a920-4451-b584-d0f9a36b6ea6", "node_type": "1", "metadata": {}, "hash": "9b068f476c1fd86e5fa7007723a6eb46a6c68fcaf6f1b32cdbebc34ab04c6465", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 4440, "end_char_idx": 7040, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
use_azure_active_directory = False  # Set this flag to True if you are using Azure Active Directory
```

#### Authentication using API key

To set up the OpenAI SDK to use an *Azure API Key*, we need to set `api_key` to a key associated with your endpoint (you can find this key in *"Keys and Endpoints"* under *"Resource Management"* in the [Azure Portal](https://portal.azure.com)). You'll also find the endpoint for your resource here.


```python
if not use_azure_active_directory:
    endpoint = os.environ["AZURE_OPENAI_ENDPOINT"]
    api_key = os.environ["AZURE_OPENAI_API_KEY"]
    # set the deployment name for the model we want to use
    deployment = "<deployment-id-of-the-model-to-use>"

    client = openai.AzureOpenAI(
        base_url=f"{endpoint}/openai/deployments/{deployment}/extensions",
        api_key=api_key,
        api_version="2023-09-01-preview"
    )
```

#### Authentication using Azure Active Directory
Let's now see how we can autheticate via Azure Active Directory. We'll start by installing the `azure-identity` library. This library will provide the token credentials we need to authenticate and help us build a token credential provider through the `get_bearer_token_provider` helper function. It's recommended to use `get_bearer_token_provider` over providing a static token to `AzureOpenAI` because this API will automatically cache and refresh tokens for you. 

For more information on how to set up Azure Active Directory authentication with Azure OpenAI, see the [documentation](https://learn.microsoft.com/azure/ai-services/openai/how-to/managed-identity).


```python
! pip install "azure-identity>=1.15.0"
```


```python
from azure.identity import DefaultAzureCredential, get_bearer_token_provider

if use_azure_active_directory:
    endpoint = os.environ["AZURE_OPENAI_ENDPOINT"]
    api_key = os.environ["AZURE_OPENAI_API_KEY"]
    # set the deployment name for the model we want to use
    deployment = "<deployment-id-of-the-model-to-use>"

    client = openai.AzureOpenAI(
        base_url=f"{endpoint}/openai/deployments/{deployment}/extensions",
        azure_ad_token_provider=get_bearer_token_provider(DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"),
        api_version="2023-09-01-preview"
    )
```

> Note: the AzureOpenAI infers the following arguments from their corresponding environment variables if they are not provided:

- `api_key` from `AZURE_OPENAI_API_KEY`
- `azure_ad_token` from `AZURE_OPENAI_AD_TOKEN`
- `api_version` from `OPENAI_API_VERSION`
- `azure_endpoint` from `AZURE_OPENAI_ENDPOINT`


key: _node_content
string_value:
{"id_": "e0c43395-a920-4451-b584-d0f9a36b6ea6", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 2205, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "772f6b12-dd18-4220-a289-39a4569d4087", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 2205, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "4ec30e7d81fd63c5649ef3e786352185e9512594ae626d36f61e5d4fed8d7782", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "e103ab6c-8f65-41b1-9c56-fddc0f7a41ca", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 2205, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "a642657e6d271d32d144b9bcf0baa564d5c1f7b0e72a11f3d9b5b27a40d312ca", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "cbfdc8e3-789f-4fbb-89c5-0888fcfc67c5", "node_type": "1", "metadata": {}, "hash": "4177c9e850ecfa2998aefdee2181db22fbf4f1ba8f212d4b382b15f953bcb512", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 6052, "end_char_idx": 9069, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
! pip install "azure-identity>=1.15.0"
```


```python
from azure.identity import DefaultAzureCredential, get_bearer_token_provider

if use_azure_active_directory:
    endpoint = os.environ["AZURE_OPENAI_ENDPOINT"]
    api_key = os.environ["AZURE_OPENAI_API_KEY"]
    # set the deployment name for the model we want to use
    deployment = "<deployment-id-of-the-model-to-use>"

    client = openai.AzureOpenAI(
        base_url=f"{endpoint}/openai/deployments/{deployment}/extensions",
        azure_ad_token_provider=get_bearer_token_provider(DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"),
        api_version="2023-09-01-preview"
    )
```

> Note: the AzureOpenAI infers the following arguments from their corresponding environment variables if they are not provided:

- `api_key` from `AZURE_OPENAI_API_KEY`
- `azure_ad_token` from `AZURE_OPENAI_AD_TOKEN`
- `api_version` from `OPENAI_API_VERSION`
- `azure_endpoint` from `AZURE_OPENAI_ENDPOINT`


## Chat completion model with your own data

### Setting the context

In this example, we want our model to base its responses on Azure AI services documentation data. Following the [Quickstart](https://learn.microsoft.com/azure/ai-services/openai/use-your-data-quickstart?tabs=command-line&pivots=programming-language-studio) shared previously, we have added the [markdown](https://github.com/MicrosoftDocs/azure-docs/blob/main/articles/ai-services/cognitive-services-and-machine-learning.md) file for the [Azure AI services and machine learning](https://learn.microsoft.com/azure/ai-services/cognitive-services-and-machine-learning) documentation page to our search index. The model is now ready to answer questions about Azure AI services and machine learning.

### Code

Now we can use Azure on your own data with Chat Completions. Providing our search endpoint, key, and index name in `dataSources`, any questions posed to the model will now be grounded in our own data. An additional property, `context`, will be provided in the response to show the data the model referenced to answer the question.


```python
completion = client.chat.completions.create(
    messages=[{"role": "user", "content": "What are the differences between Azure Machine Learning and Azure AI services?"}],
    model=deployment,
    extra_body={
        "dataSources": [
            {
                "type": "AzureCognitiveSearch",
                "parameters": {
                    "endpoint": os.environ["SEARCH_ENDPOINT"],
                    "key": os.environ["SEARCH_KEY"],
                    "indexName": os.environ["SEARCH_INDEX_NAME"],
                }
            }
        ]
    }
)
print(f"{completion.choices[0].message.role}: {completion.choices[0].message.content}")

# `context` is in the model_extra for Azure
print(f"\nContext: {completion.choices[0].message.model_extra['context']['messages'][0]['content']}")
```

If you would prefer to stream the response from the model, you can pass the `stream=True` keyword argument:


key: _node_content
string_value:
{"id_": "cbfdc8e3-789f-4fbb-89c5-0888fcfc67c5", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 2205, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "772f6b12-dd18-4220-a289-39a4569d4087", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 2205, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "4ec30e7d81fd63c5649ef3e786352185e9512594ae626d36f61e5d4fed8d7782", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "e0c43395-a920-4451-b584-d0f9a36b6ea6", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 2205, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "99f42a5905b3a9599d13c0b967232d33d1db50b218fd2973be6767484ca2cf4f", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 8151, "end_char_idx": 9990, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
completion = client.chat.completions.create(
    messages=[{"role": "user", "content": "What are the differences between Azure Machine Learning and Azure AI services?"}],
    model=deployment,
    extra_body={
        "dataSources": [
            {
                "type": "AzureCognitiveSearch",
                "parameters": {
                    "endpoint": os.environ["SEARCH_ENDPOINT"],
                    "key": os.environ["SEARCH_KEY"],
                    "indexName": os.environ["SEARCH_INDEX_NAME"],
                }
            }
        ]
    }
)
print(f"{completion.choices[0].message.role}: {completion.choices[0].message.content}")

# `context` is in the model_extra for Azure
print(f"\nContext: {completion.choices[0].message.model_extra['context']['messages'][0]['content']}")
```

If you would prefer to stream the response from the model, you can pass the `stream=True` keyword argument:


```python
response = client.chat.completions.create(
    messages=[{"role": "user", "content": "What are the differences between Azure Machine Learning and Azure AI services?"}],
    model=deployment,
    extra_body={
        "dataSources": [
            {
                "type": "AzureCognitiveSearch",
                "parameters": {
                    "endpoint": os.environ["SEARCH_ENDPOINT"],
                    "key": os.environ["SEARCH_KEY"],
                    "indexName": os.environ["SEARCH_INDEX_NAME"],
                }
            }
        ]
    },
    stream=True,
)

for chunk in response:
    delta = chunk.choices[0].delta

    if delta.role:
        print("\n"+ delta.role + ": ", end="", flush=True)
    if delta.content:
        print(delta.content, end="", flush=True)
    if delta.model_extra.get("context"):
        print(f"Context: {delta.model_extra['context']}", end="", flush=True)
```


key: _node_content
string_value:
{"id_": "d357a457-1497-4ae5-aaf4-e353fc093edf", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/completions.ipynb", "title": "Azure completions example", "tokens": 903, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "433855fe-ff34-4800-ba87-d4be047e9a64", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/completions.ipynb", "title": "Azure completions example", "tokens": 903, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "63f93087a77114e71b10b5fcffac807ff702d5c89e4f3bea448e94d9f3f340cb", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "111c20fd-2cf2-4b04-9039-3fc344ed686e", "node_type": "1", "metadata": {}, "hash": "927f9a4d15c2070779d098d96c1eceb77e0c210c7b65a68b2565ad798546d011", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 0, "end_char_idx": 2276, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
# Azure completions example

> Note: There is a newer version of the openai library available. See https://github.com/openai/openai-python/discussions/742

This example will cover completions using the Azure OpenAI service.

## Setup


First, we install the necessary dependencies.


```python
! pip install "openai>=0.28.1,<1.0.0"
```

For the following sections to work properly we first have to setup some things. Let's start with the `api_base` and `api_version`. To find your `api_base` go to https://portal.azure.com, find your resource and then under "Resource Management" -> "Keys and Endpoints" look for the "Endpoint" value.


```python
import os
import openai
```


```python
openai.api_version = '2023-05-15'
openai.api_base = '' # Please add your endpoint here
```

We next have to setup the `api_type` and `api_key`. We can either get the key from the portal or we can get it through Microsoft Active Directory Authentication. Depending on this the `api_type` is either `azure` or `azure_ad`.

### Setup: Portal
Let's first look at getting the key from the portal. Go to https://portal.azure.com, find your resource and then under "Resource Management" -> "Keys and Endpoints" look for one of the "Keys" values.


```python
openai.api_type = 'azure'
openai.api_key = os.environ["OPENAI_API_KEY"]
```

> Note: In this example, we configured the library to use the Azure API by setting the variables in code. For development, consider setting the environment variables instead:

```
OPENAI_API_BASE
OPENAI_API_KEY
OPENAI_API_TYPE
OPENAI_API_VERSION
```

### (Optional) Setup: Microsoft Active Directory Authentication
Let's now see how we can get a key via Microsoft Active Directory Authentication. Uncomment the following code if you want to use Active Directory Authentication instead of keys from the portal.


```python
# from azure.identity import DefaultAzureCredential

# default_credential = DefaultAzureCredential()
# token = default_credential.get_token("https://cognitiveservices.azure.com/.default")

# openai.api_type = 'azure_ad'
# openai.api_key = token.token
```

A token is valid for a period of time, after which it will expire. To ensure a valid token is sent with every request, you can refresh an expiring token by hooking into requests.auth:


key: _node_content
string_value:
{"id_": "111c20fd-2cf2-4b04-9039-3fc344ed686e", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/completions.ipynb", "title": "Azure completions example", "tokens": 903, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "433855fe-ff34-4800-ba87-d4be047e9a64", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/completions.ipynb", "title": "Azure completions example", "tokens": 903, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "63f93087a77114e71b10b5fcffac807ff702d5c89e4f3bea448e94d9f3f340cb", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "d357a457-1497-4ae5-aaf4-e353fc093edf", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/completions.ipynb", "title": "Azure completions example", "tokens": 903, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "cd96136d07485e14e23c132dec06fdf0bc58b561413ceca82be8f2f062f88858", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 637, "end_char_idx": 3915, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
import os
import openai
```


```python
openai.api_version = '2023-05-15'
openai.api_base = '' # Please add your endpoint here
```

We next have to setup the `api_type` and `api_key`. We can either get the key from the portal or we can get it through Microsoft Active Directory Authentication. Depending on this the `api_type` is either `azure` or `azure_ad`.

### Setup: Portal
Let's first look at getting the key from the portal. Go to https://portal.azure.com, find your resource and then under "Resource Management" -> "Keys and Endpoints" look for one of the "Keys" values.


```python
openai.api_type = 'azure'
openai.api_key = os.environ["OPENAI_API_KEY"]
```

> Note: In this example, we configured the library to use the Azure API by setting the variables in code. For development, consider setting the environment variables instead:

```
OPENAI_API_BASE
OPENAI_API_KEY
OPENAI_API_TYPE
OPENAI_API_VERSION
```

### (Optional) Setup: Microsoft Active Directory Authentication
Let's now see how we can get a key via Microsoft Active Directory Authentication. Uncomment the following code if you want to use Active Directory Authentication instead of keys from the portal.


```python
# from azure.identity import DefaultAzureCredential

# default_credential = DefaultAzureCredential()
# token = default_credential.get_token("https://cognitiveservices.azure.com/.default")

# openai.api_type = 'azure_ad'
# openai.api_key = token.token
```

A token is valid for a period of time, after which it will expire. To ensure a valid token is sent with every request, you can refresh an expiring token by hooking into requests.auth:


```python
import typing
import time
import requests
if typing.TYPE_CHECKING:
    from azure.core.credentials import TokenCredential

class TokenRefresh(requests.auth.AuthBase):

    def __init__(self, credential: "TokenCredential", scopes: typing.List[str]) -> None:
        self.credential = credential
        self.scopes = scopes
        self.cached_token: typing.Optional[str] = None

    def __call__(self, req):
        if not self.cached_token or self.cached_token.expires_on - time.time() < 300:
            self.cached_token = self.credential.get_token(*self.scopes)
        req.headers["Authorization"] = f"Bearer {self.cached_token.token}"
        return req

session = requests.Session()
session.auth = TokenRefresh(default_credential, ["https://cognitiveservices.azure.com/.default"])

openai.requestssession = session
```

## Deployments
In this section we are going to create a deployment using the `gpt-3.5-turbo-instruct` model that we can then use to create completions.

### Deployments: Create manually
Create a new deployment by going to your Resource in your portal under "Resource Management" -> "Model deployments". Select `gpt-3.5-turbo-instruct` as the model.


```python
deployment_id = '' # Fill in the deployment id from the portal here
```

### Completions
Now let's send a sample completion to the deployment.


```python
prompt = "The food was delicious and the waiter"
completion = openai.Completion.create(deployment_id=deployment_id,
                                     prompt=prompt, stop=".", temperature=0)
                                
print(f"{prompt}{completion['choices'][0]['text']}.")
```


key: _node_content
string_value:
{"id_": "2486f717-63d1-45de-ac7a-79ac03f88250", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/whisper.ipynb", "title": "Azure audio whisper (preview) example", "tokens": 1088, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "0eb8a4a1-c605-40db-b988-eee229a1d854", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/whisper.ipynb", "title": "Azure audio whisper (preview) example", "tokens": 1088, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "078cb8f441b8c6d46823a1448b8a2fdac5e7dfc658867a96a545553dab72a794", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "d59f173f-ed14-4a77-85f9-ce87be3771ee", "node_type": "1", "metadata": {}, "hash": "8b2df007a1af0dd6e539313308ca210e83cc154d3fbbdde619469781d7ea51dd", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 0, "end_char_idx": 2995, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
# Azure audio whisper (preview) example

> Note: There is a newer version of the openai library available. See https://github.com/openai/openai-python/discussions/742

The example shows how to use the Azure OpenAI Whisper model to transcribe audio files.

## Setup

First, we install the necessary dependencies.


```python
! pip install "openai>=0.28.1,<1.0.0"
! pip install python-dotenv
```

Next, we'll import our libraries and configure the Python OpenAI SDK to work with the Azure OpenAI service.

> Note: In this example, we configured the library to use the Azure API by setting the variables in code. For development, consider setting the environment variables instead:

```
OPENAI_API_BASE
OPENAI_API_KEY
OPENAI_API_TYPE
OPENAI_API_VERSION
```


```python
import os
import dotenv
import openai


dotenv.load_dotenv()
```




    True




To properly access the Azure OpenAI Service, we need to create the proper resources at the [Azure Portal](https://portal.azure.com) (you can check a detailed guide on how to do this in the [Microsoft Docs](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/how-to/create-resource?pivots=web-portal))

Once the resource is created, the first thing we need to use is its endpoint. You can get the endpoint by looking at the *"Keys and Endpoints"* section under the *"Resource Management"* section. Having this, we will set up the SDK using this information:


```python
openai.api_base = os.environ["OPENAI_API_BASE"]

# Min API version that supports Whisper
openai.api_version = "2023-09-01-preview"

# Enter the deployment_id to use for the Whisper model
deployment_id = "<deployment-id-for-your-whisper-model>"
```

### Authentication

The Azure OpenAI service supports multiple authentication mechanisms that include API keys and Azure credentials.


```python
# set to True if using Azure Active Directory authentication
use_azure_active_directory = False
```


#### Authentication using API key

To set up the OpenAI SDK to use an *Azure API Key*, we need to set up the `api_type` to `azure` and set `api_key` to a key associated with your endpoint (you can find this key in *"Keys and Endpoints"* under *"Resource Management"* in the [Azure Portal](https://portal.azure.com))


```python
if not use_azure_active_directory:
    openai.api_type = 'azure'
    openai.api_key = os.environ["OPENAI_API_KEY"]
```

#### Authentication using Azure Active Directory
Let's now see how we can get a key via Microsoft Active Directory Authentication.


```python
from azure.identity import DefaultAzureCredential

if use_azure_active_directory:
    default_credential = DefaultAzureCredential()
    token = default_credential.get_token("https://cognitiveservices.azure.com/.default")

    openai.api_type = 'azure_ad'
    openai.api_key = token.token
```

A token is valid for a period of time, after which it will expire. To ensure a valid token is sent with every request, you can refresh an expiring token by hooking into requests.auth:


key: _node_content
string_value:
{"id_": "d59f173f-ed14-4a77-85f9-ce87be3771ee", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/whisper.ipynb", "title": "Azure audio whisper (preview) example", "tokens": 1088, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "0eb8a4a1-c605-40db-b988-eee229a1d854", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/whisper.ipynb", "title": "Azure audio whisper (preview) example", "tokens": 1088, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "078cb8f441b8c6d46823a1448b8a2fdac5e7dfc658867a96a545553dab72a794", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "2486f717-63d1-45de-ac7a-79ac03f88250", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/whisper.ipynb", "title": "Azure audio whisper (preview) example", "tokens": 1088, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "3870cdccc5700223c1261ea70d4e16b9731f322bccfd893389515ffd7b0a707a", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 1421, "end_char_idx": 4768, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
openai.api_base = os.environ["OPENAI_API_BASE"]

# Min API version that supports Whisper
openai.api_version = "2023-09-01-preview"

# Enter the deployment_id to use for the Whisper model
deployment_id = "<deployment-id-for-your-whisper-model>"
```

### Authentication

The Azure OpenAI service supports multiple authentication mechanisms that include API keys and Azure credentials.


```python
# set to True if using Azure Active Directory authentication
use_azure_active_directory = False
```


#### Authentication using API key

To set up the OpenAI SDK to use an *Azure API Key*, we need to set up the `api_type` to `azure` and set `api_key` to a key associated with your endpoint (you can find this key in *"Keys and Endpoints"* under *"Resource Management"* in the [Azure Portal](https://portal.azure.com))


```python
if not use_azure_active_directory:
    openai.api_type = 'azure'
    openai.api_key = os.environ["OPENAI_API_KEY"]
```

#### Authentication using Azure Active Directory
Let's now see how we can get a key via Microsoft Active Directory Authentication.


```python
from azure.identity import DefaultAzureCredential

if use_azure_active_directory:
    default_credential = DefaultAzureCredential()
    token = default_credential.get_token("https://cognitiveservices.azure.com/.default")

    openai.api_type = 'azure_ad'
    openai.api_key = token.token
```

A token is valid for a period of time, after which it will expire. To ensure a valid token is sent with every request, you can refresh an expiring token by hooking into requests.auth:


```python
import typing
import time
import requests

if typing.TYPE_CHECKING:
    from azure.core.credentials import TokenCredential

class TokenRefresh(requests.auth.AuthBase):

    def __init__(self, credential: "TokenCredential", scopes: typing.List[str]) -> None:
        self.credential = credential
        self.scopes = scopes
        self.cached_token: typing.Optional[str] = None

    def __call__(self, req):
        if not self.cached_token or self.cached_token.expires_on - time.time() < 300:
            self.cached_token = self.credential.get_token(*self.scopes)
        req.headers["Authorization"] = f"Bearer {self.cached_token.token}"
        return req

if use_azure_active_directory:
    session = requests.Session()
    session.auth = TokenRefresh(default_credential, ["https://cognitiveservices.azure.com/.default"])

    openai.requestssession = session
```

## Audio transcription

Audio transcription, or speech-to-text, is the process of converting spoken words into text. Use the `openai.Audio.transcribe` method to transcribe an audio file stream to text.

You can get sample audio files from the [Azure AI Speech SDK repository at GitHub](https://github.com/Azure-Samples/cognitive-services-speech-sdk/tree/master/sampledata/audiofiles).


```python
# download sample audio file
import requests

sample_audio_url = "https://github.com/Azure-Samples/cognitive-services-speech-sdk/raw/master/sampledata/audiofiles/wikipediaOcelot.wav"
audio_file = requests.get(sample_audio_url)
with open("wikipediaOcelot.wav", "wb") as f:
    f.write(audio_file.content)
```


```python
transcription = openai.Audio.transcribe(
    file=open("wikipediaOcelot.wav", "rb"),
    model="whisper-1",
    deployment_id=deployment_id,
)
print(transcription.text)
```


key: _node_content
string_value:
{"id_": "e20f2908-0627-4942-bab5-c7b1a94c38ca", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat.ipynb", "title": "Azure chat completions example (preview)", "tokens": 1164, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "47fa04db-7655-4070-8003-156eb18b64ce", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat.ipynb", "title": "Azure chat completions example (preview)", "tokens": 1164, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "53d52c5d42ad01089f2f5de642fd0f7b756c50a816e31657bc1bda528b67c279", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "e54c210e-b9ee-4bb3-8c73-f5f4db2a157d", "node_type": "1", "metadata": {}, "hash": "f14f8583f21a13a95c52fb56c41c7e521a95329b8351eea86541c5abe5bd6488", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 0, "end_char_idx": 2297, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
# Azure chat completions example (preview)

> Note: There is a newer version of the openai library available. See https://github.com/openai/openai-python/discussions/742

This example will cover chat completions using the Azure OpenAI service.

## Setup


First, we install the necessary dependencies.


```python
! pip install "openai>=0.28.1,<1.0.0"
```

For the following sections to work properly we first have to setup some things. Let's start with the `api_base` and `api_version`. To find your `api_base` go to https://portal.azure.com, find your resource and then under "Resource Management" -> "Keys and Endpoints" look for the "Endpoint" value.


```python
import os
import openai
```


```python
openai.api_version = '2023-05-15'
openai.api_base = '' # Please add your endpoint here
```

We next have to setup the `api_type` and `api_key`. We can either get the key from the portal or we can get it through Microsoft Active Directory Authentication. Depending on this the `api_type` is either `azure` or `azure_ad`.

### Setup: Portal
Let's first look at getting the key from the portal. Go to https://portal.azure.com, find your resource and then under "Resource Management" -> "Keys and Endpoints" look for one of the "Keys" values.


```python
openai.api_type = 'azure'
openai.api_key = os.environ["OPENAI_API_KEY"]

```

> Note: In this example, we configured the library to use the Azure API by setting the variables in code. For development, consider setting the environment variables instead:

```
OPENAI_API_BASE
OPENAI_API_KEY
OPENAI_API_TYPE
OPENAI_API_VERSION
```

### (Optional) Setup: Microsoft Active Directory Authentication
Let's now see how we can get a key via Microsoft Active Directory Authentication. Uncomment the following code if you want to use Active Directory Authentication instead of keys from the portal.


```python
# from azure.identity import DefaultAzureCredential

# default_credential = DefaultAzureCredential()
# token = default_credential.get_token("https://cognitiveservices.azure.com/.default")

# openai.api_type = 'azure_ad'
# openai.api_key = token.token
```

A token is valid for a period of time, after which it will expire. To ensure a valid token is sent with every request, you can refresh an expiring token by hooking into requests.auth:


key: _node_content
string_value:
{"id_": "e54c210e-b9ee-4bb3-8c73-f5f4db2a157d", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat.ipynb", "title": "Azure chat completions example (preview)", "tokens": 1164, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "47fa04db-7655-4070-8003-156eb18b64ce", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat.ipynb", "title": "Azure chat completions example (preview)", "tokens": 1164, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "53d52c5d42ad01089f2f5de642fd0f7b756c50a816e31657bc1bda528b67c279", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "e20f2908-0627-4942-bab5-c7b1a94c38ca", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat.ipynb", "title": "Azure chat completions example (preview)", "tokens": 1164, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "9390cf8dfd9d1b71b2bb4a26586404d7518df250c6946b7f8130909608c5a10b", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "acb7831e-f569-4252-b352-6813d3c1013d", "node_type": "1", "metadata": {}, "hash": "71982df42fc00c04aa021216539f95d33c33a7f0a1de6c11cec174e66e9d8a49", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 657, "end_char_idx": 3698, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
import os
import openai
```


```python
openai.api_version = '2023-05-15'
openai.api_base = '' # Please add your endpoint here
```

We next have to setup the `api_type` and `api_key`. We can either get the key from the portal or we can get it through Microsoft Active Directory Authentication. Depending on this the `api_type` is either `azure` or `azure_ad`.

### Setup: Portal
Let's first look at getting the key from the portal. Go to https://portal.azure.com, find your resource and then under "Resource Management" -> "Keys and Endpoints" look for one of the "Keys" values.


```python
openai.api_type = 'azure'
openai.api_key = os.environ["OPENAI_API_KEY"]

```

> Note: In this example, we configured the library to use the Azure API by setting the variables in code. For development, consider setting the environment variables instead:

```
OPENAI_API_BASE
OPENAI_API_KEY
OPENAI_API_TYPE
OPENAI_API_VERSION
```

### (Optional) Setup: Microsoft Active Directory Authentication
Let's now see how we can get a key via Microsoft Active Directory Authentication. Uncomment the following code if you want to use Active Directory Authentication instead of keys from the portal.


```python
# from azure.identity import DefaultAzureCredential

# default_credential = DefaultAzureCredential()
# token = default_credential.get_token("https://cognitiveservices.azure.com/.default")

# openai.api_type = 'azure_ad'
# openai.api_key = token.token
```

A token is valid for a period of time, after which it will expire. To ensure a valid token is sent with every request, you can refresh an expiring token by hooking into requests.auth:


```python
import typing
import time
import requests
if typing.TYPE_CHECKING:
    from azure.core.credentials import TokenCredential

class TokenRefresh(requests.auth.AuthBase):

    def __init__(self, credential: "TokenCredential", scopes: typing.List[str]) -> None:
        self.credential = credential
        self.scopes = scopes
        self.cached_token: typing.Optional[str] = None

    def __call__(self, req):
        if not self.cached_token or self.cached_token.expires_on - time.time() < 300:
            self.cached_token = self.credential.get_token(*self.scopes)
        req.headers["Authorization"] = f"Bearer {self.cached_token.token}"
        return req

session = requests.Session()
session.auth = TokenRefresh(default_credential, ["https://cognitiveservices.azure.com/.default"])

openai.requestssession = session
```

## Deployments
In this section we are going to create a deployment using the `gpt-35-turbo` model that we can then use to create chat completions.

### Deployments: Create manually
Let's create a deployment using the `gpt-35-turbo` model. Go to https://portal.azure.com, find your resource and then under "Resource Management" -> "Model deployments" create a new `gpt-35-turbo` deployment. 


```python
deployment_id = '' # Fill in the deployment id from the portal here
```

### Create chat completion
Now let's send a sample chat completion to the deployment.


key: _node_content
string_value:
{"id_": "acb7831e-f569-4252-b352-6813d3c1013d", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat.ipynb", "title": "Azure chat completions example (preview)", "tokens": 1164, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "47fa04db-7655-4070-8003-156eb18b64ce", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat.ipynb", "title": "Azure chat completions example (preview)", "tokens": 1164, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "53d52c5d42ad01089f2f5de642fd0f7b756c50a816e31657bc1bda528b67c279", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "e54c210e-b9ee-4bb3-8c73-f5f4db2a157d", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat.ipynb", "title": "Azure chat completions example (preview)", "tokens": 1164, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "e731cee08237a51bb35861f2bdd32560b1fe2b04343cdc459acc4fdfb5e852ab", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 2300, "end_char_idx": 4942, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
import typing
import time
import requests
if typing.TYPE_CHECKING:
    from azure.core.credentials import TokenCredential

class TokenRefresh(requests.auth.AuthBase):

    def __init__(self, credential: "TokenCredential", scopes: typing.List[str]) -> None:
        self.credential = credential
        self.scopes = scopes
        self.cached_token: typing.Optional[str] = None

    def __call__(self, req):
        if not self.cached_token or self.cached_token.expires_on - time.time() < 300:
            self.cached_token = self.credential.get_token(*self.scopes)
        req.headers["Authorization"] = f"Bearer {self.cached_token.token}"
        return req

session = requests.Session()
session.auth = TokenRefresh(default_credential, ["https://cognitiveservices.azure.com/.default"])

openai.requestssession = session
```

## Deployments
In this section we are going to create a deployment using the `gpt-35-turbo` model that we can then use to create chat completions.

### Deployments: Create manually
Let's create a deployment using the `gpt-35-turbo` model. Go to https://portal.azure.com, find your resource and then under "Resource Management" -> "Model deployments" create a new `gpt-35-turbo` deployment. 


```python
deployment_id = '' # Fill in the deployment id from the portal here
```

### Create chat completion
Now let's send a sample chat completion to the deployment.


```python
# For all possible arguments see https://platform.openai.com/docs/api-reference/chat-completions/create
response = openai.ChatCompletion.create(
    deployment_id=deployment_id,
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Knock knock."},
        {"role": "assistant", "content": "Who's there?"},
        {"role": "user", "content": "Orange."},
    ],
    temperature=0,
)

print(f"{response.choices[0].message.role}: {response.choices[0].message.content}")
```

We can also stream the response.



```python
response = openai.ChatCompletion.create(
    deployment_id=deployment_id,
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Knock knock."},
        {"role": "assistant", "content": "Who's there?"},
        {"role": "user", "content": "Orange."},
    ],
    temperature=0,
    stream=True
)

for chunk in response:
    if len(chunk.choices) > 0:
        delta = chunk.choices[0].delta

        if "role" in delta.keys():
            print(delta.role + ": ", end="", flush=True)
        if "content" in delta.keys():
            print(delta.content, end="", flush=True)
```


key: _node_content
string_value:
{"id_": "7c550179-169a-4dc7-b5c8-c3b191a2766d", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/embeddings.ipynb", "title": "Azure embeddings example", "tokens": 1015, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "1908645a-dcbd-4593-b7fd-9f096f88fd80", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/embeddings.ipynb", "title": "Azure embeddings example", "tokens": 1015, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "b223a2145303566d1eca6e5926969569973a4d484a5cb6d353db5ef86daaf1b9", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "7631fd6b-7e82-4c08-8f01-7dc2d9f6a73b", "node_type": "1", "metadata": {}, "hash": "68f65e56aa830071d7db7ff48c1e8357be8bb55182030057d900da43cca765ce", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 0, "end_char_idx": 2258, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
# Azure embeddings example

> Note: There is a newer version of the openai library available. See https://github.com/openai/openai-python/discussions/742

This example will cover embeddings using the Azure OpenAI service.

## Setup

First, we install the necessary dependencies.

! pip install "openai>=0.28.1,<1.0.0"

For the following sections to work properly we first have to setup some things. Let's start with the `api_base` and `api_version`. To find your `api_base` go to https://portal.azure.com, find your resource and then under "Resource Management" -> "Keys and Endpoints" look for the "Endpoint" value.


```python
import os
import openai
```


```python
openai.api_version = '2023-05-15'
openai.api_base = '' # Please add your endpoint here
```

We next have to setup the `api_type` and `api_key`. We can either get the key from the portal or we can get it through Microsoft Active Directory Authentication. Depending on this the `api_type` is either `azure` or `azure_ad`.

### Setup: Portal
Let's first look at getting the key from the portal. Go to https://portal.azure.com, find your resource and then under "Resource Management" -> "Keys and Endpoints" look for one of the "Keys" values.


```python
openai.api_type = 'azure'
openai.api_key = os.environ["OPENAI_API_KEY"]
```

> Note: In this example, we configured the library to use the Azure API by setting the variables in code. For development, consider setting the environment variables instead:

```
OPENAI_API_BASE
OPENAI_API_KEY
OPENAI_API_TYPE
OPENAI_API_VERSION
```

### (Optional) Setup: Microsoft Active Directory Authentication
Let's now see how we can get a key via Microsoft Active Directory Authentication. Uncomment the following code if you want to use Active Directory Authentication instead of keys from the portal.


```python
# from azure.identity import DefaultAzureCredential

# default_credential = DefaultAzureCredential()
# token = default_credential.get_token("https://cognitiveservices.azure.com/.default")

# openai.api_type = 'azure_ad'
# openai.api_key = token.token
```

A token is valid for a period of time, after which it will expire. To ensure a valid token is sent with every request, you can refresh an expiring token by hooking into requests.auth:


key: _node_content
string_value:
{"id_": "7631fd6b-7e82-4c08-8f01-7dc2d9f6a73b", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/embeddings.ipynb", "title": "Azure embeddings example", "tokens": 1015, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "1908645a-dcbd-4593-b7fd-9f096f88fd80", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/embeddings.ipynb", "title": "Azure embeddings example", "tokens": 1015, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "b223a2145303566d1eca6e5926969569973a4d484a5cb6d353db5ef86daaf1b9", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "7c550179-169a-4dc7-b5c8-c3b191a2766d", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/embeddings.ipynb", "title": "Azure embeddings example", "tokens": 1015, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "892562f3ac8cee006d5aed959f18c1a7614114cc997735862e96bc9b2c316518", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "888f3f8b-48a1-475d-ba55-f62a27b15c09", "node_type": "1", "metadata": {}, "hash": "8d03c1d6529f6d46ea9697dae7a7b27905b44ee829f649897e786f8ceac5f4bd", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 619, "end_char_idx": 3673, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
import os
import openai
```


```python
openai.api_version = '2023-05-15'
openai.api_base = '' # Please add your endpoint here
```

We next have to setup the `api_type` and `api_key`. We can either get the key from the portal or we can get it through Microsoft Active Directory Authentication. Depending on this the `api_type` is either `azure` or `azure_ad`.

### Setup: Portal
Let's first look at getting the key from the portal. Go to https://portal.azure.com, find your resource and then under "Resource Management" -> "Keys and Endpoints" look for one of the "Keys" values.


```python
openai.api_type = 'azure'
openai.api_key = os.environ["OPENAI_API_KEY"]
```

> Note: In this example, we configured the library to use the Azure API by setting the variables in code. For development, consider setting the environment variables instead:

```
OPENAI_API_BASE
OPENAI_API_KEY
OPENAI_API_TYPE
OPENAI_API_VERSION
```

### (Optional) Setup: Microsoft Active Directory Authentication
Let's now see how we can get a key via Microsoft Active Directory Authentication. Uncomment the following code if you want to use Active Directory Authentication instead of keys from the portal.


```python
# from azure.identity import DefaultAzureCredential

# default_credential = DefaultAzureCredential()
# token = default_credential.get_token("https://cognitiveservices.azure.com/.default")

# openai.api_type = 'azure_ad'
# openai.api_key = token.token
```

A token is valid for a period of time, after which it will expire. To ensure a valid token is sent with every request, you can refresh an expiring token by hooking into requests.auth:


```python
import typing
import time
import requests
if typing.TYPE_CHECKING:
    from azure.core.credentials import TokenCredential

class TokenRefresh(requests.auth.AuthBase):

    def __init__(self, credential: "TokenCredential", scopes: typing.List[str]) -> None:
        self.credential = credential
        self.scopes = scopes
        self.cached_token: typing.Optional[str] = None

    def __call__(self, req):
        if not self.cached_token or self.cached_token.expires_on - time.time() < 300:
            self.cached_token = self.credential.get_token(*self.scopes)
        req.headers["Authorization"] = f"Bearer {self.cached_token.token}"
        return req

session = requests.Session()
session.auth = TokenRefresh(default_credential, ["https://cognitiveservices.azure.com/.default"])

openai.requestssession = session
```

## Deployments
In this section we are going to create a deployment that we can use to create embeddings.

### Deployments: Create manually
Let's create a deployment using the `text-similarity-curie-001` model. Create a new deployment by going to your Resource in your portal under "Resource Management" -> "Model deployments".


```python
deployment_id = '' # Fill in the deployment id from the portal here
```

### Deployments: Listing
Now because creating a new deployment takes a long time, let's look in the subscription for an already finished deployment that succeeded.


key: _node_content
string_value:
{"id_": "888f3f8b-48a1-475d-ba55-f62a27b15c09", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/embeddings.ipynb", "title": "Azure embeddings example", "tokens": 1015, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "1908645a-dcbd-4593-b7fd-9f096f88fd80", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/embeddings.ipynb", "title": "Azure embeddings example", "tokens": 1015, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "b223a2145303566d1eca6e5926969569973a4d484a5cb6d353db5ef86daaf1b9", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "7631fd6b-7e82-4c08-8f01-7dc2d9f6a73b", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/embeddings.ipynb", "title": "Azure embeddings example", "tokens": 1015, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "3ffb11a851014719a36478fbaed07e49eb0709154f35ec497d1e30645bbc0907", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 2261, "end_char_idx": 4573, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
import typing
import time
import requests
if typing.TYPE_CHECKING:
    from azure.core.credentials import TokenCredential

class TokenRefresh(requests.auth.AuthBase):

    def __init__(self, credential: "TokenCredential", scopes: typing.List[str]) -> None:
        self.credential = credential
        self.scopes = scopes
        self.cached_token: typing.Optional[str] = None

    def __call__(self, req):
        if not self.cached_token or self.cached_token.expires_on - time.time() < 300:
            self.cached_token = self.credential.get_token(*self.scopes)
        req.headers["Authorization"] = f"Bearer {self.cached_token.token}"
        return req

session = requests.Session()
session.auth = TokenRefresh(default_credential, ["https://cognitiveservices.azure.com/.default"])

openai.requestssession = session
```

## Deployments
In this section we are going to create a deployment that we can use to create embeddings.

### Deployments: Create manually
Let's create a deployment using the `text-similarity-curie-001` model. Create a new deployment by going to your Resource in your portal under "Resource Management" -> "Model deployments".


```python
deployment_id = '' # Fill in the deployment id from the portal here
```

### Deployments: Listing
Now because creating a new deployment takes a long time, let's look in the subscription for an already finished deployment that succeeded.


```python
print('While deployment running, selecting a completed one that supports embeddings.')
deployment_id = None
result = openai.Deployment.list()
for deployment in result.data:
    if deployment["status"] != "succeeded":
        continue
    
    model = openai.Model.retrieve(deployment["model"])
    if model["capabilities"]["embeddings"] != True:
        continue
    
    deployment_id = deployment["id"]
    break

if not deployment_id:
    print('No deployment with status: succeeded found.')
else:
    print(f'Found a succeeded deployment that supports embeddings with id: {deployment_id}.')
```

### Embeddings
Now let's send a sample embedding to the deployment.


```python
embeddings = openai.Embedding.create(deployment_id=deployment_id,
                                     input="The food was delicious and the waiter...")
                                
print(embeddings)
```


key: _node_content
string_value:
{"id_": "f6abf454-b77a-4d0b-b987-8ef5d3324778", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/DALL-E.ipynb", "title": "Azure DALL\u00b7E image generation example", "tokens": 1190, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "c3ab16e4-79bf-4cf7-abbe-91b222c2c933", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/DALL-E.ipynb", "title": "Azure DALL\u00b7E image generation example", "tokens": 1190, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "c227d1babd0c3e54858abf278e8b790d41d1e58013d4c3a966f18e50cf11d353", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "b2c86bd6-d10d-4922-bf06-bc69ec95a05b", "node_type": "1", "metadata": {}, "hash": "5e45390f26c442bab7bacf8f5d2204a9119c37f08d1a9451ec9acb491ec0ae66", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 0, "end_char_idx": 2905, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
# Azure DALL·E image generation example

> Note: There is a newer version of the openai library available. See https://github.com/openai/openai-python/discussions/742

This notebook shows how to generate images with the Azure OpenAI service.

## Setup

First, we install the necessary dependencies.


```python
! pip install "openai>=0.28.1,<1.0.0"
# We need requests to retrieve the generated image
! pip install requests
# We use Pillow to display the generated image
! pip install pillow 
# (Optional) If you want to use Microsoft Active Directory
! pip install azure-identity
```


```python
import os
import openai
```


Additionally, to properly access the Azure OpenAI Service, we need to create the proper resources at the [Azure Portal](https://portal.azure.com) (you can check a detailed guide on how to do this in the [Microsoft Docs](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/how-to/create-resource?pivots=web-portal))

Once the resource is created, the first thing we need to use is its endpoint. You can get the endpoint by looking at the *"Keys and Endpoints"* section under the *"Resource Management"* section. Having this, we will set up the SDK using this information:


```python
openai.api_base = '' # Add your endpoint here

# At the moment DALL·E is only supported by the 2023-06-01-preview API version
openai.api_version = '2023-06-01-preview'
```

### Authentication

The Azure OpenAI service supports multiple authentication mechanisms that include API keys and Azure credentials.


```python
use_azure_active_directory = False
```


#### Authentication using API key

To set up the OpenAI SDK to use an *Azure API Key*, we need to set up the `api_type` to `azure` and set `api_key` to a key associated with your endpoint (you can find this key in *"Keys and Endpoints"* under *"Resource Management"* in the [Azure Portal](https://portal.azure.com))


```python
if not use_azure_active_directory:
    openai.api_type = 'azure'
    openai.api_key = os.environ["OPENAI_API_KEY"]
```

> Note: In this example, we configured the library to use the Azure API by setting the variables in code. For development, consider setting the environment variables instead:

```
OPENAI_API_BASE
OPENAI_API_KEY
OPENAI_API_TYPE
OPENAI_API_VERSION
```

#### Authentication using Microsoft Active Directory
Let's now see how we can get a key via Microsoft Active Directory Authentication.


```python
from azure.identity import DefaultAzureCredential

if use_azure_active_directory:
    default_credential = DefaultAzureCredential()
    token = default_credential.get_token("https://cognitiveservices.azure.com/.default")

    openai.api_type = 'azure_ad'
    openai.api_key = token.token
```

A token is valid for a period of time, after which it will expire. To ensure a valid token is sent with every request, you can refresh an expiring token by hooking into requests.auth:


key: _node_content
string_value:
{"id_": "b2c86bd6-d10d-4922-bf06-bc69ec95a05b", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/DALL-E.ipynb", "title": "Azure DALL\u00b7E image generation example", "tokens": 1190, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "c3ab16e4-79bf-4cf7-abbe-91b222c2c933", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/DALL-E.ipynb", "title": "Azure DALL\u00b7E image generation example", "tokens": 1190, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "c227d1babd0c3e54858abf278e8b790d41d1e58013d4c3a966f18e50cf11d353", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "f6abf454-b77a-4d0b-b987-8ef5d3324778", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/DALL-E.ipynb", "title": "Azure DALL\u00b7E image generation example", "tokens": 1190, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "c28da1edf18cd60f556f74aa40cfd7de4dcf94a1851a4f9a6eaa41a7ff266673", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "86192e34-6e03-4c99-bbfb-859a2c93a162", "node_type": "1", "metadata": {}, "hash": "b8fa68b36d32c8a9eb5599bd4bd3bebcef5ce7b22e8cdfbb005106e0f6e5f841", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 1213, "end_char_idx": 4346, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
openai.api_base = '' # Add your endpoint here

# At the moment DALL·E is only supported by the 2023-06-01-preview API version
openai.api_version = '2023-06-01-preview'
```

### Authentication

The Azure OpenAI service supports multiple authentication mechanisms that include API keys and Azure credentials.


```python
use_azure_active_directory = False
```


#### Authentication using API key

To set up the OpenAI SDK to use an *Azure API Key*, we need to set up the `api_type` to `azure` and set `api_key` to a key associated with your endpoint (you can find this key in *"Keys and Endpoints"* under *"Resource Management"* in the [Azure Portal](https://portal.azure.com))


```python
if not use_azure_active_directory:
    openai.api_type = 'azure'
    openai.api_key = os.environ["OPENAI_API_KEY"]
```

> Note: In this example, we configured the library to use the Azure API by setting the variables in code. For development, consider setting the environment variables instead:

```
OPENAI_API_BASE
OPENAI_API_KEY
OPENAI_API_TYPE
OPENAI_API_VERSION
```

#### Authentication using Microsoft Active Directory
Let's now see how we can get a key via Microsoft Active Directory Authentication.


```python
from azure.identity import DefaultAzureCredential

if use_azure_active_directory:
    default_credential = DefaultAzureCredential()
    token = default_credential.get_token("https://cognitiveservices.azure.com/.default")

    openai.api_type = 'azure_ad'
    openai.api_key = token.token
```

A token is valid for a period of time, after which it will expire. To ensure a valid token is sent with every request, you can refresh an expiring token by hooking into requests.auth:


```python
import typing
import time
import requests

if typing.TYPE_CHECKING:
    from azure.core.credentials import TokenCredential

class TokenRefresh(requests.auth.AuthBase):

    def __init__(self, credential: "TokenCredential", scopes: typing.List[str]) -> None:
        self.credential = credential
        self.scopes = scopes
        self.cached_token: typing.Optional[str] = None

    def __call__(self, req):
        if not self.cached_token or self.cached_token.expires_on - time.time() < 300:
            self.cached_token = self.credential.get_token(*self.scopes)
        req.headers["Authorization"] = f"Bearer {self.cached_token.token}"
        return req

if use_azure_active_directory:
    session = requests.Session()
    session.auth = TokenRefresh(default_credential, ["https://cognitiveservices.azure.com/.default"])

    openai.requestssession = session
```

## Generations

With setup and authentication complete, you can now generate images on the Azure OpenAI service and retrieve them from the returned URLs.

#### 1. Generate the images

The first step in this process is to actually generate the images:


```python
generation_response = openai.Image.create(
    prompt='A cyberpunk monkey hacker dreaming of a beautiful bunch of bananas, digital art',
    size='1024x1024',
    n=2
)

print(generation_response)
```

Having the response from the `Image.create` call, we download from the URL using `requests`.


key: _node_content
string_value:
{"id_": "86192e34-6e03-4c99-bbfb-859a2c93a162", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/DALL-E.ipynb", "title": "Azure DALL\u00b7E image generation example", "tokens": 1190, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "c3ab16e4-79bf-4cf7-abbe-91b222c2c933", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/DALL-E.ipynb", "title": "Azure DALL\u00b7E image generation example", "tokens": 1190, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "c227d1babd0c3e54858abf278e8b790d41d1e58013d4c3a966f18e50cf11d353", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "b2c86bd6-d10d-4922-bf06-bc69ec95a05b", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/DALL-E.ipynb", "title": "Azure DALL\u00b7E image generation example", "tokens": 1190, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "0f15e1d49a2166caaba8a0f91be35877bb183f69ec1915783fc53d3c7dc4b722", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 2908, "end_char_idx": 5186, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
import typing
import time
import requests

if typing.TYPE_CHECKING:
    from azure.core.credentials import TokenCredential

class TokenRefresh(requests.auth.AuthBase):

    def __init__(self, credential: "TokenCredential", scopes: typing.List[str]) -> None:
        self.credential = credential
        self.scopes = scopes
        self.cached_token: typing.Optional[str] = None

    def __call__(self, req):
        if not self.cached_token or self.cached_token.expires_on - time.time() < 300:
            self.cached_token = self.credential.get_token(*self.scopes)
        req.headers["Authorization"] = f"Bearer {self.cached_token.token}"
        return req

if use_azure_active_directory:
    session = requests.Session()
    session.auth = TokenRefresh(default_credential, ["https://cognitiveservices.azure.com/.default"])

    openai.requestssession = session
```

## Generations

With setup and authentication complete, you can now generate images on the Azure OpenAI service and retrieve them from the returned URLs.

#### 1. Generate the images

The first step in this process is to actually generate the images:


```python
generation_response = openai.Image.create(
    prompt='A cyberpunk monkey hacker dreaming of a beautiful bunch of bananas, digital art',
    size='1024x1024',
    n=2
)

print(generation_response)
```

Having the response from the `Image.create` call, we download from the URL using `requests`.


```python
import os
import requests

# First a little setup
image_dir = os.path.join(os.curdir, 'images')
# If the directory doesn't exist, create it
if not os.path.isdir(image_dir):
    os.mkdir(image_dir)

# With the directory in place, we can initialize the image path (note that filetype should be png)
image_path = os.path.join(image_dir, 'generated_image.png')

# Now we can retrieve the generated image
image_url = generation_response["data"][0]["url"]  # extract image URL from response
generated_image = requests.get(image_url).content  # download the image
with open(image_path, "wb") as image_file:
    image_file.write(generated_image)
```

With the image downloaded, we use the [Pillow](https://pypi.org/project/Pillow/) library to open and display it:


```python
from PIL import Image 

display(Image.open(image_path))
```


key: _node_content
string_value:
{"id_": "8e1bccea-561b-4a58-a920-d5a9902fbd4d", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/functions.ipynb", "title": "Azure functions example", "tokens": 2044, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "0144e318-f9a2-4892-a9c6-d7ac70248c24", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/functions.ipynb", "title": "Azure functions example", "tokens": 2044, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "3daba2c07f7a4cefdf7c224d1382f1fa9245d1f7c2c735e2c0343f8ab96cb2bd", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "99988a00-92af-49be-97c6-59f4b8361e02", "node_type": "1", "metadata": {}, "hash": "cab966979611e5832219ed0f63b01d7f986022326e2c24aafec8dac0c8a89a6c", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 0, "end_char_idx": 3196, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
# Azure functions example

> Note: There is a newer version of the openai library available. See https://github.com/openai/openai-python/discussions/742

This notebook shows how to use the function calling capability with the Azure OpenAI service. Functions allow a caller of chat completions to define capabilities that the model can use to extend its
functionality into external tools and data sources.

You can read more about chat functions on OpenAI's blog: https://openai.com/blog/function-calling-and-other-api-updates

**NOTE**: Chat functions require model versions beginning with gpt-4 and gpt-35-turbo's `-0613` labels. They are not supported by older versions of the models.

## Setup

First, we install the necessary dependencies.


```python
! pip install "openai>=0.28.1,<1.0.0"
# (Optional) If you want to use Microsoft Active Directory
! pip install azure-identity
```


```python
import os
import openai
```


Additionally, to properly access the Azure OpenAI Service, we need to create the proper resources at the [Azure Portal](https://portal.azure.com) (you can check a detailed guide on how to do this in the [Microsoft Docs](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/how-to/create-resource?pivots=web-portal))

Once the resource is created, the first thing we need to use is its endpoint. You can get the endpoint by looking at the *"Keys and Endpoints"* section under the *"Resource Management"* section. Having this, we will set up the SDK using this information:


```python
openai.api_base = "" # Add your endpoint here

# functions is only supported by the 2023-07-01-preview API version
openai.api_version = "2023-07-01-preview"
```

### Authentication

The Azure OpenAI service supports multiple authentication mechanisms that include API keys and Azure credentials.


```python
use_azure_active_directory = False
```


#### Authentication using API key

To set up the OpenAI SDK to use an *Azure API Key*, we need to set up the `api_type` to `azure` and set `api_key` to a key associated with your endpoint (you can find this key in *"Keys and Endpoints"* under *"Resource Management"* in the [Azure Portal](https://portal.azure.com))


```python
if not use_azure_active_directory:
    openai.api_type = "azure"
    openai.api_key = os.environ["OPENAI_API_KEY"]
```

> Note: In this example, we configured the library to use the Azure API by setting the variables in code. For development, consider setting the environment variables instead:

```
OPENAI_API_BASE
OPENAI_API_KEY
OPENAI_API_TYPE
OPENAI_API_VERSION
```

#### Authentication using Microsoft Active Directory
Let's now see how we can get a key via Microsoft Active Directory Authentication.


```python
from azure.identity import DefaultAzureCredential

if use_azure_active_directory:
    default_credential = DefaultAzureCredential()
    token = default_credential.get_token("https://cognitiveservices.azure.com/.default")

    openai.api_type = "azure_ad"
    openai.api_key = token.token
```

A token is valid for a period of time, after which it will expire. To ensure a valid token is sent with every request, you can refresh an expiring token by hooking into requests.auth:


key: _node_content
string_value:
{"id_": "99988a00-92af-49be-97c6-59f4b8361e02", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/functions.ipynb", "title": "Azure functions example", "tokens": 2044, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "0144e318-f9a2-4892-a9c6-d7ac70248c24", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/functions.ipynb", "title": "Azure functions example", "tokens": 2044, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "3daba2c07f7a4cefdf7c224d1382f1fa9245d1f7c2c735e2c0343f8ab96cb2bd", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "8e1bccea-561b-4a58-a920-d5a9902fbd4d", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/functions.ipynb", "title": "Azure functions example", "tokens": 2044, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "7410ae96b9de62f174ae1094afcd1c6ff25a24dabcbce8cf00972aa550e07808", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "2bc532a8-6bc1-48ff-bc96-5546caf8c288", "node_type": "1", "metadata": {}, "hash": "2cbc1d9adfb444a369b0c3ea7ccc19e9a9cda83f0347d49447669e7517f21eb5", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 1515, "end_char_idx": 4624, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
openai.api_base = "" # Add your endpoint here

# functions is only supported by the 2023-07-01-preview API version
openai.api_version = "2023-07-01-preview"
```

### Authentication

The Azure OpenAI service supports multiple authentication mechanisms that include API keys and Azure credentials.


```python
use_azure_active_directory = False
```


#### Authentication using API key

To set up the OpenAI SDK to use an *Azure API Key*, we need to set up the `api_type` to `azure` and set `api_key` to a key associated with your endpoint (you can find this key in *"Keys and Endpoints"* under *"Resource Management"* in the [Azure Portal](https://portal.azure.com))


```python
if not use_azure_active_directory:
    openai.api_type = "azure"
    openai.api_key = os.environ["OPENAI_API_KEY"]
```

> Note: In this example, we configured the library to use the Azure API by setting the variables in code. For development, consider setting the environment variables instead:

```
OPENAI_API_BASE
OPENAI_API_KEY
OPENAI_API_TYPE
OPENAI_API_VERSION
```

#### Authentication using Microsoft Active Directory
Let's now see how we can get a key via Microsoft Active Directory Authentication.


```python
from azure.identity import DefaultAzureCredential

if use_azure_active_directory:
    default_credential = DefaultAzureCredential()
    token = default_credential.get_token("https://cognitiveservices.azure.com/.default")

    openai.api_type = "azure_ad"
    openai.api_key = token.token
```

A token is valid for a period of time, after which it will expire. To ensure a valid token is sent with every request, you can refresh an expiring token by hooking into requests.auth:


```python
import typing
import time
import requests

if typing.TYPE_CHECKING:
    from azure.core.credentials import TokenCredential

class TokenRefresh(requests.auth.AuthBase):

    def __init__(self, credential: "TokenCredential", scopes: typing.List[str]) -> None:
        self.credential = credential
        self.scopes = scopes
        self.cached_token: typing.Optional[str] = None

    def __call__(self, req):
        if not self.cached_token or self.cached_token.expires_on - time.time() < 300:
            self.cached_token = self.credential.get_token(*self.scopes)
        req.headers["Authorization"] = f"Bearer {self.cached_token.token}"
        return req

if use_azure_active_directory:
    session = requests.Session()
    session.auth = TokenRefresh(default_credential, ["https://cognitiveservices.azure.com/.default"])

    openai.requestssession = session
```

## Functions

With setup and authentication complete, you can now use functions with the Azure OpenAI service. This will be split into a few steps:

1. Define the function(s)
2. Pass function definition(s) into chat completions API
3. Call function with arguments from the response
4. Feed function response back into chat completions API

#### 1. Define the function(s)

A list of functions can be defined, each containing the name of the function, an optional description, and the parameters the function accepts (described as a JSON schema).


key: _node_content
string_value:
{"id_": "2bc532a8-6bc1-48ff-bc96-5546caf8c288", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/functions.ipynb", "title": "Azure functions example", "tokens": 2044, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "0144e318-f9a2-4892-a9c6-d7ac70248c24", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/functions.ipynb", "title": "Azure functions example", "tokens": 2044, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "3daba2c07f7a4cefdf7c224d1382f1fa9245d1f7c2c735e2c0343f8ab96cb2bd", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "99988a00-92af-49be-97c6-59f4b8361e02", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/functions.ipynb", "title": "Azure functions example", "tokens": 2044, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "a5c05c24e01e071b537425e7311ca00ebae8bc70f8d5e8d678628b5624767f0b", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "aceedff4-866a-4bd4-b437-93411baf63ba", "node_type": "1", "metadata": {}, "hash": "106b005d31dd35c722d8fac365ebb0cfed417618d47c51e9fd0091cb47374b5b", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 3199, "end_char_idx": 5919, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
import typing
import time
import requests

if typing.TYPE_CHECKING:
    from azure.core.credentials import TokenCredential

class TokenRefresh(requests.auth.AuthBase):

    def __init__(self, credential: "TokenCredential", scopes: typing.List[str]) -> None:
        self.credential = credential
        self.scopes = scopes
        self.cached_token: typing.Optional[str] = None

    def __call__(self, req):
        if not self.cached_token or self.cached_token.expires_on - time.time() < 300:
            self.cached_token = self.credential.get_token(*self.scopes)
        req.headers["Authorization"] = f"Bearer {self.cached_token.token}"
        return req

if use_azure_active_directory:
    session = requests.Session()
    session.auth = TokenRefresh(default_credential, ["https://cognitiveservices.azure.com/.default"])

    openai.requestssession = session
```

## Functions

With setup and authentication complete, you can now use functions with the Azure OpenAI service. This will be split into a few steps:

1. Define the function(s)
2. Pass function definition(s) into chat completions API
3. Call function with arguments from the response
4. Feed function response back into chat completions API

#### 1. Define the function(s)

A list of functions can be defined, each containing the name of the function, an optional description, and the parameters the function accepts (described as a JSON schema).


```python
functions = [
    {
        "name": "get_current_weather",
        "description": "Get the current weather",
        "parameters": {
            "type": "object",
            "properties": {
                "location": {
                    "type": "string",
                    "description": "The city and state, e.g. San Francisco, CA",
                },
                "format": {
                    "type": "string",
                    "enum": ["celsius", "fahrenheit"],
                    "description": "The temperature unit to use. Infer this from the users location.",
                },
            },
            "required": ["location"],
        },
    }
]
```

#### 2. Pass function definition(s) into chat completions API

Now we can pass the function into the chat completions API. If the model determines it should call the function, a `finish_reason` of "function_call" will be populated on the choice and the details of which function to call and its arguments will be present in the `message`. Optionally, you can set the `function_call` keyword argument to force the model to call a particular function (e.g. `function_call={"name": get_current_weather}`). By default, this is set to `auto`, allowing the model to choose whether to call the function or not.


key: _node_content
string_value:
{"id_": "aceedff4-866a-4bd4-b437-93411baf63ba", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/functions.ipynb", "title": "Azure functions example", "tokens": 2044, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "0144e318-f9a2-4892-a9c6-d7ac70248c24", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/functions.ipynb", "title": "Azure functions example", "tokens": 2044, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "3daba2c07f7a4cefdf7c224d1382f1fa9245d1f7c2c735e2c0343f8ab96cb2bd", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "2bc532a8-6bc1-48ff-bc96-5546caf8c288", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/functions.ipynb", "title": "Azure functions example", "tokens": 2044, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "4f776a57806b9f9f79772dce8298aa33b7e7bb3e25bbde955af3dd3f25fcabe4", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "835c1fbb-2ed5-420f-b5f2-cc846cd6492c", "node_type": "1", "metadata": {}, "hash": "69071f9436c7333db7729dcb321def546fc67549864ed47183659c53d3d3ba57", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 4627, "end_char_idx": 7777, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
functions = [
    {
        "name": "get_current_weather",
        "description": "Get the current weather",
        "parameters": {
            "type": "object",
            "properties": {
                "location": {
                    "type": "string",
                    "description": "The city and state, e.g. San Francisco, CA",
                },
                "format": {
                    "type": "string",
                    "enum": ["celsius", "fahrenheit"],
                    "description": "The temperature unit to use. Infer this from the users location.",
                },
            },
            "required": ["location"],
        },
    }
]
```

#### 2. Pass function definition(s) into chat completions API

Now we can pass the function into the chat completions API. If the model determines it should call the function, a `finish_reason` of "function_call" will be populated on the choice and the details of which function to call and its arguments will be present in the `message`. Optionally, you can set the `function_call` keyword argument to force the model to call a particular function (e.g. `function_call={"name": get_current_weather}`). By default, this is set to `auto`, allowing the model to choose whether to call the function or not. 


```python
messages = [
    {"role": "system", "content": "Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous."},
    {"role": "user", "content": "What's the weather like today in Seattle?"}
]

chat_completion = openai.ChatCompletion.create(
    deployment_id="gpt-35-turbo-0613",
    messages=messages,
    functions=functions,
)
print(chat_completion)
```

    {
      "choices": [
        {
          "content_filter_results": {},
          "finish_reason": "function_call",
          "index": 0,
          "message": {
            "function_call": {
              "arguments": "{\n  \"location\": \"Seattle, WA\"\n}",
              "name": "get_current_weather"
            },
            "role": "assistant"
          }
        }
      ],
      "created": 1689702512,
      "id": "chatcmpl-7dj6GkYdM7Vw9eGn02bc2qqjN70Ps",
      "model": "gpt-4",
      "object": "chat.completion",
      "prompt_annotations": [
        {
          "content_filter_results": {
            "hate": {
              "filtered": false,
              "severity": "safe"
            },
            "self_harm": {
              "filtered": false,
              "severity": "safe"
            },
            "sexual": {
              "filtered": false,
              "severity": "safe"
            },
            "violence": {
              "filtered": false,
              "severity": "safe"
            }
          },
          "prompt_index": 0
        }
      ],
      "usage": {
        "completion_tokens": 18,
        "prompt_tokens": 115,
        "total_tokens": 133
      }
    }


#### 3. Call function with arguments from the response

The name of the function call will be one that was provided initially and the arguments will include JSON matching the schema included in the function definition.


key: _node_content
string_value:
{"id_": "835c1fbb-2ed5-420f-b5f2-cc846cd6492c", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/functions.ipynb", "title": "Azure functions example", "tokens": 2044, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "0144e318-f9a2-4892-a9c6-d7ac70248c24", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/functions.ipynb", "title": "Azure functions example", "tokens": 2044, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "3daba2c07f7a4cefdf7c224d1382f1fa9245d1f7c2c735e2c0343f8ab96cb2bd", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "aceedff4-866a-4bd4-b437-93411baf63ba", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/functions.ipynb", "title": "Azure functions example", "tokens": 2044, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "a3a1e9a4e6efdcfc48fca5688e87d6c286007fe4eecd64e4886d9a9770758d0b", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 7559, "end_char_idx": 9153, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
#### 3. Call function with arguments from the response

The name of the function call will be one that was provided initially and the arguments will include JSON matching the schema included in the function definition.


```python
import json

def get_current_weather(request):
    """
    This function is for illustrative purposes.
    The location and unit should be used to determine weather
    instead of returning a hardcoded response.
    """
    location = request.get("location")
    unit = request.get("unit")
    return {"temperature": "22", "unit": "celsius", "description": "Sunny"}

function_call =  chat_completion.choices[0].message.function_call
print(function_call.name)
print(function_call.arguments)

if function_call.name == "get_current_weather":
    response = get_current_weather(json.loads(function_call.arguments))
```

    get_current_weather
    {
      "location": "Seattle, WA"
    }


#### 4. Feed function response back into chat completions API

The response from the function should be serialized into a new message with the role set to "function". Now the model will use the response data to formulate its answer.


```python
messages.append(
    {
        "role": "function",
        "name": "get_current_weather",
        "content": json.dumps(response)
    }
)

function_completion = openai.ChatCompletion.create(
    deployment_id="gpt-35-turbo-0613",
    messages=messages,
    functions=functions,
)

print(function_completion.choices[0].message.content.strip())
```

    Today in Seattle, the weather is sunny with a temperature of 22 degrees celsius.


key: url
string_value:
https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb


key: _node_content
string_value:
{"id_": "67f57ff9-0dce-4491-b701-7ae5f470ea56", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "9b1f6272-7419-4aff-8f11-399ed5865b9e", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "a5ffcb818a44482890e35fbf75f880025cebadf69e3b2e423ff4951db64439ce", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "092edb18-2f9c-4237-9f0b-27d303ee6364", "node_type": "1", "metadata": {}, "hash": "586b3250940add4ad34517f4615bf1d91659742f1862071264163349fce76328", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 0, "end_char_idx": 3277, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
# Azure chat completion models with your own data (preview)

> Note: There is a newer version of the openai library available. See https://github.com/openai/openai-python/discussions/742

This example shows how to use Azure OpenAI service models with your own data. The feature is currently in preview. 

Azure OpenAI on your data enables you to run supported chat models such as GPT-3.5-Turbo and GPT-4 on your data without needing to train or fine-tune models. Running models on your data enables you to chat on top of, and analyze your data with greater accuracy and speed. One of the key benefits of Azure OpenAI on your data is its ability to tailor the content of conversational AI. Because the model has access to, and can reference specific sources to support its responses, answers are not only based on its pretrained knowledge but also on the latest information available in the designated data source. This grounding data also helps the model avoid generating responses based on outdated or incorrect information.

Azure OpenAI on your own data with Azure Cognitive Search provides a customizable, pre-built solution for knowledge retrieval, from which a conversational AI application can be built. To see alternative methods for knowledge retrieval and semantic search, check out the cookbook examples for [vector databases](https://github.com/openai/openai-cookbook/tree/main/examples/vector_databases).

## How it works

[Azure OpenAI on your own data](https://learn.microsoft.com/azure/ai-services/openai/concepts/use-your-data) connects the model with your data, giving it the ability to retrieve and utilize data in a way that enhances the model's output. Together with Azure Cognitive Search, data is retrieved from designated data sources based on the user input and provided conversation history. The data is then augmented and resubmitted as a prompt to the model, giving the model contextual information it can use to generate a response.

See the [Data, privacy, and security for Azure OpenAI Service](https://learn.microsoft.com/legal/cognitive-services/openai/data-privacy?context=%2Fazure%2Fai-services%2Fopenai%2Fcontext%2Fcontext) for more information.

## Prerequisites
To get started, we'll cover a few prequisites. 

To properly access the Azure OpenAI Service, we need to create the proper resources at the [Azure Portal](https://portal.azure.com) (you can check a detailed guide on how to do this in the [Microsoft Docs](https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource?pivots=web-portal))

To use your own data with Azure OpenAI models, you will need:

1. Azure OpenAI access and a resource with a chat model deployed (for example, GPT-3 or GPT-4)
2. Azure Cognitive Search resource
3. Azure Blob Storage resource
4. Your documents to be used as data (See [data source options](https://learn.microsoft.com/azure/ai-services/openai/concepts/use-your-data#data-source-options))


For a full walk-through on how to upload your documents to blob storage and create an index using the Azure AI Studio, see this [Quickstart](https://learn.microsoft.com/azure/ai-services/openai/use-your-data-quickstart?pivots=programming-language-studio&tabs=command-line).

## Setup

First, we install the necessary dependencies.


key: _node_content
string_value:
{"id_": "092edb18-2f9c-4237-9f0b-27d303ee6364", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "9b1f6272-7419-4aff-8f11-399ed5865b9e", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "a5ffcb818a44482890e35fbf75f880025cebadf69e3b2e423ff4951db64439ce", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "67f57ff9-0dce-4491-b701-7ae5f470ea56", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "b95c8bdcbf7bbe6003ff7b41e9fb44a8457656702b79aa730a7966023c033123", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "324ff2c0-d01b-46de-80f1-d41d9938f82d", "node_type": "1", "metadata": {}, "hash": "60c229b8b541dde9853e94bab547a578d8beb322acc34debe6fc771c0c7d4e93", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 2947, "end_char_idx": 6078, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
For a full walk-through on how to upload your documents to blob storage and create an index using the Azure AI Studio, see this [Quickstart](https://learn.microsoft.com/azure/ai-services/openai/use-your-data-quickstart?pivots=programming-language-studio&tabs=command-line).

## Setup

First, we install the necessary dependencies.


```python
! pip install "openai>=0.28.1,<1.0.0"
! pip install python-dotenv
```

In this example, we'll use `dotenv` to load our environment variables. To connect with Azure OpenAI and the Search index, the following variables should be added to a `.env` file in `KEY=VALUE` format:

* `OPENAI_API_BASE` - the Azure OpenAI endpoint. This can be found under "Keys and Endpoints" for your Azure OpenAI resource in the Azure Portal.
* `OPENAI_API_KEY` - the Azure OpenAI API key. This can be found under "Keys and Endpoints" for your Azure OpenAI resource in the Azure Portal. Omit if using Azure Active Directory authentication (see below `Authentication using Microsoft Active Directory`)
* `SEARCH_ENDPOINT` - the Cognitive Search endpoint. This URL be found on the "Overview" of your Search resource on the Azure Portal.
* `SEARCH_KEY` - the Cognitive Search API key. Found under "Keys" for your Search resource in the Azure Portal.
* `SEARCH_INDEX_NAME` - the name of the index you created with your own data.


```python
import os
import openai
import dotenv

dotenv.load_dotenv()
```


```python
openai.api_base = os.environ["OPENAI_API_BASE"]

# Azure OpenAI on your own data is only supported by the 2023-08-01-preview API version
openai.api_version = "2023-08-01-preview"
```

### Authentication

The Azure OpenAI service supports multiple authentication mechanisms that include API keys and Azure credentials.


```python
use_azure_active_directory = False  # Set this flag to True if you are using Azure Active Directory
```


#### Authentication using API key

To set up the OpenAI SDK to use an *Azure API Key*, we need to set up the `api_type` to `azure` and set `api_key` to a key associated with your endpoint (you can find this key in *"Keys and Endpoints"* under *"Resource Management"* in the [Azure Portal](https://portal.azure.com))


```python
if not use_azure_active_directory:
    openai.api_type = 'azure'
    openai.api_key = os.environ["OPENAI_API_KEY"]
```

#### Authentication using Microsoft Active Directory
Let's now see how we can get a key via Microsoft Active Directory Authentication. See the [documentation](https://learn.microsoft.com/azure/ai-services/openai/how-to/managed-identity) for more information on how to set this up.


```python
! pip install azure-identity
```


```python
from azure.identity import DefaultAzureCredential

if use_azure_active_directory:
    default_credential = DefaultAzureCredential()
    token = default_credential.get_token("https://cognitiveservices.azure.com/.default")

    openai.api_type = "azure_ad"
    openai.api_key = token.token
```

A token is valid for a period of time, after which it will expire. To ensure a valid token is sent with every request, you can refresh an expiring token by hooking into requests.auth:


key: _node_content
string_value:
{"id_": "324ff2c0-d01b-46de-80f1-d41d9938f82d", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "9b1f6272-7419-4aff-8f11-399ed5865b9e", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "a5ffcb818a44482890e35fbf75f880025cebadf69e3b2e423ff4951db64439ce", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "092edb18-2f9c-4237-9f0b-27d303ee6364", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "eaec4563b883108b463351230ecab9735e843d15a53ac91d01f364b9080b5821", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "e20cb4a2-1faa-45ef-8605-b0b2708f32da", "node_type": "1", "metadata": {}, "hash": "8ebbd22a3e245485bf782c9cb56f7d1d42cf55e080354e847069f1f66e6e6ec1", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 4370, "end_char_idx": 7910, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
openai.api_base = os.environ["OPENAI_API_BASE"]

# Azure OpenAI on your own data is only supported by the 2023-08-01-preview API version
openai.api_version = "2023-08-01-preview"
```

### Authentication

The Azure OpenAI service supports multiple authentication mechanisms that include API keys and Azure credentials.


```python
use_azure_active_directory = False  # Set this flag to True if you are using Azure Active Directory
```


#### Authentication using API key

To set up the OpenAI SDK to use an *Azure API Key*, we need to set up the `api_type` to `azure` and set `api_key` to a key associated with your endpoint (you can find this key in *"Keys and Endpoints"* under *"Resource Management"* in the [Azure Portal](https://portal.azure.com))


```python
if not use_azure_active_directory:
    openai.api_type = 'azure'
    openai.api_key = os.environ["OPENAI_API_KEY"]
```

#### Authentication using Microsoft Active Directory
Let's now see how we can get a key via Microsoft Active Directory Authentication. See the [documentation](https://learn.microsoft.com/azure/ai-services/openai/how-to/managed-identity) for more information on how to set this up.


```python
! pip install azure-identity
```


```python
from azure.identity import DefaultAzureCredential

if use_azure_active_directory:
    default_credential = DefaultAzureCredential()
    token = default_credential.get_token("https://cognitiveservices.azure.com/.default")

    openai.api_type = "azure_ad"
    openai.api_key = token.token
```

A token is valid for a period of time, after which it will expire. To ensure a valid token is sent with every request, you can refresh an expiring token by hooking into requests.auth:


```python
import typing
import time
import requests

if typing.TYPE_CHECKING:
    from azure.core.credentials import TokenCredential

class TokenRefresh(requests.auth.AuthBase):

    def __init__(self, credential: "TokenCredential", scopes: typing.List[str]) -> None:
        self.credential = credential
        self.scopes = scopes
        self.cached_token: typing.Optional[str] = None

    def __call__(self, req):
        if not self.cached_token or self.cached_token.expires_on - time.time() < 300:
            self.cached_token = self.credential.get_token(*self.scopes)
        req.headers["Authorization"] = f"Bearer {self.cached_token.token}"
        return req

```

## Chat completion model with your own data

### Setting the context

In this example, we want our model to base its responses on Azure AI services documentation data. Following the [Quickstart](https://learn.microsoft.com/azure/ai-services/openai/use-your-data-quickstart?tabs=command-line&pivots=programming-language-studio) shared previously, we have added the [markdown](https://github.com/MicrosoftDocs/azure-docs/blob/main/articles/ai-services/cognitive-services-and-machine-learning.md) file for the [Azure AI services and machine learning](https://learn.microsoft.com/azure/ai-services/cognitive-services-and-machine-learning) documentation page to our search index. The model is now ready to answer questions about Azure AI services and machine learning.

### Code

To chat with Azure OpenAI models using your own data with the Python SDK, we must first set up the code to target the chat completions extensions endpoint which is designed to work with your own data. To do this, we've created a convenience function that can be called to set a custom adapter for the library which will target the extensions endpoint for a given deployment ID.


key: _node_content
string_value:
{"id_": "e20cb4a2-1faa-45ef-8605-b0b2708f32da", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "9b1f6272-7419-4aff-8f11-399ed5865b9e", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "a5ffcb818a44482890e35fbf75f880025cebadf69e3b2e423ff4951db64439ce", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "324ff2c0-d01b-46de-80f1-d41d9938f82d", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "b9821f1a7388303c7318a9ed0341deacda50152ea98b332a80711b70adfec6a1", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "1bfe9652-22d3-40fc-83ae-5eb8bde1e1c9", "node_type": "1", "metadata": {}, "hash": "bf79c03abacc8b2efda34e208cf91abda3608fc16bc4cc6b98c1db63f102f67a", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 6081, "end_char_idx": 9618, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
import typing
import time
import requests

if typing.TYPE_CHECKING:
    from azure.core.credentials import TokenCredential

class TokenRefresh(requests.auth.AuthBase):

    def __init__(self, credential: "TokenCredential", scopes: typing.List[str]) -> None:
        self.credential = credential
        self.scopes = scopes
        self.cached_token: typing.Optional[str] = None

    def __call__(self, req):
        if not self.cached_token or self.cached_token.expires_on - time.time() < 300:
            self.cached_token = self.credential.get_token(*self.scopes)
        req.headers["Authorization"] = f"Bearer {self.cached_token.token}"
        return req

```

## Chat completion model with your own data

### Setting the context

In this example, we want our model to base its responses on Azure AI services documentation data. Following the [Quickstart](https://learn.microsoft.com/azure/ai-services/openai/use-your-data-quickstart?tabs=command-line&pivots=programming-language-studio) shared previously, we have added the [markdown](https://github.com/MicrosoftDocs/azure-docs/blob/main/articles/ai-services/cognitive-services-and-machine-learning.md) file for the [Azure AI services and machine learning](https://learn.microsoft.com/azure/ai-services/cognitive-services-and-machine-learning) documentation page to our search index. The model is now ready to answer questions about Azure AI services and machine learning.

### Code

To chat with Azure OpenAI models using your own data with the Python SDK, we must first set up the code to target the chat completions extensions endpoint which is designed to work with your own data. To do this, we've created a convenience function that can be called to set a custom adapter for the library which will target the extensions endpoint for a given deployment ID.


```python
import requests

def setup_byod(deployment_id: str) -> None:
    """Sets up the OpenAI Python SDK to use your own data for the chat endpoint.
    
    :param deployment_id: The deployment ID for the model to use with your own data.

    To remove this configuration, simply set openai.requestssession to None.
    """

    class BringYourOwnDataAdapter(requests.adapters.HTTPAdapter):

        def send(self, request, **kwargs):
            request.url = f"{openai.api_base}/openai/deployments/{deployment_id}/extensions/chat/completions?api-version={openai.api_version}"
            return super().send(request, **kwargs)

    session = requests.Session()

    # Mount a custom adapter which will use the extensions endpoint for any call using the given `deployment_id`
    session.mount(
        prefix=f"{openai.api_base}/openai/deployments/{deployment_id}",
        adapter=BringYourOwnDataAdapter()
    )

    if use_azure_active_directory:
        session.auth = TokenRefresh(default_credential, ["https://cognitiveservices.azure.com/.default"])

    openai.requestssession = session

```

Now we can call the convenience function to configure the SDK with the model we plan to use for our own data.


```python
setup_byod("gpt-4")
```

Providing our search endpoint, key, and index name for the `dataSources` keyword argument, any questions posed to the model will now be grounded in our own data. An additional property, `context`, will be provided to show the data the model referenced to answer the question.


```python
completion = openai.ChatCompletion.create(
    messages=[{"role": "user", "content": "What are the differences between Azure Machine Learning and Azure AI services?


key: _node_content
string_value:
{"id_": "1bfe9652-22d3-40fc-83ae-5eb8bde1e1c9", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "9b1f6272-7419-4aff-8f11-399ed5865b9e", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "a5ffcb818a44482890e35fbf75f880025cebadf69e3b2e423ff4951db64439ce", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "e20cb4a2-1faa-45ef-8605-b0b2708f32da", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "3c367687fc3fb1a29771b6798e0eb310f778cf5d8873ba04446870dde71b2a75", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "eea60462-a745-43af-9aea-0a22becdd8da", "node_type": "1", "metadata": {}, "hash": "b8d61ef0c1cb5187e0080b64927a23c58fad5d58d7588785081657fba1783888", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 7913, "end_char_idx": 11185, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
import requests

def setup_byod(deployment_id: str) -> None:
    """Sets up the OpenAI Python SDK to use your own data for the chat endpoint.
    
    :param deployment_id: The deployment ID for the model to use with your own data.

    To remove this configuration, simply set openai.requestssession to None.
    """

    class BringYourOwnDataAdapter(requests.adapters.HTTPAdapter):

        def send(self, request, **kwargs):
            request.url = f"{openai.api_base}/openai/deployments/{deployment_id}/extensions/chat/completions?api-version={openai.api_version}"
            return super().send(request, **kwargs)

    session = requests.Session()

    # Mount a custom adapter which will use the extensions endpoint for any call using the given `deployment_id`
    session.mount(
        prefix=f"{openai.api_base}/openai/deployments/{deployment_id}",
        adapter=BringYourOwnDataAdapter()
    )

    if use_azure_active_directory:
        session.auth = TokenRefresh(default_credential, ["https://cognitiveservices.azure.com/.default"])

    openai.requestssession = session

```

Now we can call the convenience function to configure the SDK with the model we plan to use for our own data.


```python
setup_byod("gpt-4")
```

Providing our search endpoint, key, and index name for the `dataSources` keyword argument, any questions posed to the model will now be grounded in our own data. An additional property, `context`, will be provided to show the data the model referenced to answer the question.


```python
completion = openai.ChatCompletion.create(
    messages=[{"role": "user", "content": "What are the differences between Azure Machine Learning and Azure AI services?"}],
    deployment_id="gpt-4",
    dataSources=[  # camelCase is intentional, as this is the format the API expects
        {
            "type": "AzureCognitiveSearch",
            "parameters": {
                "endpoint": os.environ["SEARCH_ENDPOINT"],
                "key": os.environ["SEARCH_KEY"],
                "indexName": os.environ["SEARCH_INDEX_NAME"],
            }
        }
    ]
)
print(completion)
```

    {
      "id": "65b485bb-b3c9-48da-8b6f-7d3a219f0b40",
      "model": "gpt-4",
      "created": 1693338769,
      "object": "extensions.chat.completion",
      "choices": [
        {
          "index": 0,
          "finish_reason": "stop",
          "message": {
            "role": "assistant",
            "content": "Azure AI services and Azure Machine Learning (AML) both aim to apply artificial intelligence (AI) to enhance business operations, but they target different audiences and offer different capabilities [doc1]. \n\nAzure AI services are designed for developers without machine learning experience and provide pre-trained models to solve general problems such as text analysis, image recognition, and natural language processing [doc5]. These services require general knowledge about your data without needing experience with machine learning or data science and provide REST APIs and language-based SDKs [doc2].\n\nOn the other hand, Azure Machine Learning is tailored for data scientists and involves a longer process of data collection, cleaning, transformation, algorithm selection, model training, and deployment [doc5].


key: _node_content
string_value:
{"id_": "eea60462-a745-43af-9aea-0a22becdd8da", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "9b1f6272-7419-4aff-8f11-399ed5865b9e", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "a5ffcb818a44482890e35fbf75f880025cebadf69e3b2e423ff4951db64439ce", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "1bfe9652-22d3-40fc-83ae-5eb8bde1e1c9", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "723fb1040b7acfd33860ac1d158e58480cffce937d5b5d2b507150eccc6061a9", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "c69de95c-b22d-46e6-ba76-850d5fc5b4ad", "node_type": "1", "metadata": {}, "hash": "63046365a9134e4d1c9a9407f30b33e3057df13080bbcd1652981f2397624258", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 9444, "end_char_idx": 12706, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
completion = openai.ChatCompletion.create(
    messages=[{"role": "user", "content": "What are the differences between Azure Machine Learning and Azure AI services?"}],
    deployment_id="gpt-4",
    dataSources=[  # camelCase is intentional, as this is the format the API expects
        {
            "type": "AzureCognitiveSearch",
            "parameters": {
                "endpoint": os.environ["SEARCH_ENDPOINT"],
                "key": os.environ["SEARCH_KEY"],
                "indexName": os.environ["SEARCH_INDEX_NAME"],
            }
        }
    ]
)
print(completion)
```

    {
      "id": "65b485bb-b3c9-48da-8b6f-7d3a219f0b40",
      "model": "gpt-4",
      "created": 1693338769,
      "object": "extensions.chat.completion",
      "choices": [
        {
          "index": 0,
          "finish_reason": "stop",
          "message": {
            "role": "assistant",
            "content": "Azure AI services and Azure Machine Learning (AML) both aim to apply artificial intelligence (AI) to enhance business operations, but they target different audiences and offer different capabilities [doc1]. \n\nAzure AI services are designed for developers without machine learning experience and provide pre-trained models to solve general problems such as text analysis, image recognition, and natural language processing [doc5]. These services require general knowledge about your data without needing experience with machine learning or data science and provide REST APIs and language-based SDKs [doc2].\n\nOn the other hand, Azure Machine Learning is tailored for data scientists and involves a longer process of data collection, cleaning, transformation, algorithm selection, model training, and deployment [doc5]. It allows users to create custom solutions for highly specialized and specific problems, requiring familiarity with the subject matter, data, and expertise in data science [doc5].\n\nIn summary, Azure AI services offer pre-trained models for developers without machine learning experience, while Azure Machine Learning is designed for data scientists to create custom solutions for specific problems.",
            "end_turn": true,
            "context": {
              "messages": [
                {
                  "role": "tool",
                  "content": "{\"citations\": [{\"content\": \"<h2 id=\\\"how-are-azure-ai-services-and-azure-machine-learning-aml-similar\\\">How are Azure AI services and Azure Machine Learning (AML) similar?.</h2>\\n<p>Both have the end-goal of applying artificial intelligence (AI) to enhance business operations, though how each provides this in the respective offerings is different..</p>\\n<p>Generally, the audiences are different:</p>\\n<ul>\\n<li>Azure AI services are for developers without machine-learning experience..</li>\\n<li>Azure Machine Learning is tailored for data scientists.\", \"id\": null, \"title\": \"Azure AI services and machine learning\", \"filepath\": \"cognitive-services-and-machine-learning.md\", \"url\": \"https://krpraticstorageacc.blob.core.windows.net/azure-openai/cognitive-services-and-machine-learning.md\", \"metadata\": {\"chunking\": \"orignal document size=1188. Scores=5.689296 and None.Org Highlight count=160.Filtering to chunk no.


key: _node_content
string_value:
{"id_": "c69de95c-b22d-46e6-ba76-850d5fc5b4ad", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "9b1f6272-7419-4aff-8f11-399ed5865b9e", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "a5ffcb818a44482890e35fbf75f880025cebadf69e3b2e423ff4951db64439ce", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "eea60462-a745-43af-9aea-0a22becdd8da", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "267fd8556672d5d487d9b342d59bbc42d1e88e4a7c008d5a94a5b09684d2369c", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "ba57447e-5f21-42d9-b5c1-048789570060", "node_type": "1", "metadata": {}, "hash": "8979ad9c3242d54ffe7da0c085ba892f222e6254642de2eb126c9a64ae38fe8d", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 11186, "end_char_idx": 15018, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
It allows users to create custom solutions for highly specialized and specific problems, requiring familiarity with the subject matter, data, and expertise in data science [doc5].\n\nIn summary, Azure AI services offer pre-trained models for developers without machine learning experience, while Azure Machine Learning is designed for data scientists to create custom solutions for specific problems.",
            "end_turn": true,
            "context": {
              "messages": [
                {
                  "role": "tool",
                  "content": "{\"citations\": [{\"content\": \"<h2 id=\\\"how-are-azure-ai-services-and-azure-machine-learning-aml-similar\\\">How are Azure AI services and Azure Machine Learning (AML) similar?.</h2>\\n<p>Both have the end-goal of applying artificial intelligence (AI) to enhance business operations, though how each provides this in the respective offerings is different..</p>\\n<p>Generally, the audiences are different:</p>\\n<ul>\\n<li>Azure AI services are for developers without machine-learning experience..</li>\\n<li>Azure Machine Learning is tailored for data scientists.\", \"id\": null, \"title\": \"Azure AI services and machine learning\", \"filepath\": \"cognitive-services-and-machine-learning.md\", \"url\": \"https://krpraticstorageacc.blob.core.windows.net/azure-openai/cognitive-services-and-machine-learning.md\", \"metadata\": {\"chunking\": \"orignal document size=1188. Scores=5.689296 and None.Org Highlight count=160.Filtering to chunk no. 2/Highlights=30 of size=137\"}, \"chunk_id\": \"2\"}, {\"content\": \"<td>Convert speech into text and text into natural-sounding speech..Translate from one language to another and enable speaker verification and recognition..</td>\\n</tr>\\n<tr>\\n<td><a href=\\\"https://azure.microsoft.com/services/cognitive-services/directory/vision/\\\">Vision</a></td>\\n<td>Recognize, identify, caption, index, and moderate your pictures, videos, and digital ink content..</td>\\n</tr>\\n<p></tbody>\\n</table></p>\\n<p>Use Azure AI services when you:</p>\\n<ul>\\n<li>Can use a generalized solution..</li>\\n</ul>\\n<p>Use other machine-learning solutions when you:</p>\\n<ul>\\n<li>Need to choose the algorithm and need to train on very specific data..</li>\\n</ul>\\n<h2 id=\\\"what-is-machine-learning\\\">What is machine learning?.</h2>\\n<p>Machine learning is a concept where you bring together data and an algorithm to solve a specific need..Once the data and algorithm are trained, the output is a model that you can use again with different data..The trained model provides insights based on the new data..</p>\\n<p>The process of building a machine learning system requires some knowledge of machine learning or data science..</p>\\n<p>Machine learning is provided using <a href=\\\"/azure/architecture/data-guide/technology-choices/data-science-and-machine-learning?.context=azure%2fmachine-learning%2fstudio%2fcontext%2fml-context\\\">Azure Machine Learning (AML) products and services</a>..</p>\\n<h2 id=\\\"what-is-an-azure-ai-service\\\">What is an Azure AI service?.</h2>\\n<p>An Azure AI service provides part or all of the components in a machine learning solution: data, algorithm, and trained model..These services are meant to require general knowledge about your data without needing experience with machine learning or data science..These services provide both REST API(s) and language-based SDKs..As a result, you need to have programming language knowledge to use the services..</p>\", \"id\": null, \"title\": \"Azure AI services and machine learning\", \"filepath\": \"cognitive-services-and-machine-learning.md\", \"url\": \"https://krpraticstorageacc.blob.core.windows.net/azure-openai/cognitive-services-and-machine-learning.md\", \"metadata\": {\"chunking\": \"orignal document size=1188.


key: _node_content
string_value:
{"id_": "ba57447e-5f21-42d9-b5c1-048789570060", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "9b1f6272-7419-4aff-8f11-399ed5865b9e", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "a5ffcb818a44482890e35fbf75f880025cebadf69e3b2e423ff4951db64439ce", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "c69de95c-b22d-46e6-ba76-850d5fc5b4ad", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "05937b125aea81f5f7951ecfa41f00a09cfd2afd9571cc6e415b3f5378088c8f", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "4d6690b5-62b4-48a3-898f-dc513c35a534", "node_type": "1", "metadata": {}, "hash": "7b49716a9ea6467ef0dd61a5fb6ae837adf9e75c4c59704c00af49d62d93feed", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 15019, "end_char_idx": 17882, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
Scores=5.689296 and None.Org Highlight count=160.Filtering to chunk no. 1/Highlights=67 of size=506\"}, \"chunk_id\": \"1\"}, {\"content\": \"<hr />\\n<p>title: Azure AI services and Machine Learning\\ntitleSuffix: Azure AI services\\ndescription: Learn where Azure AI services fits in with other Azure offerings for machine learning.\\nservices: cognitive-services\\nmanager: nitinme\\nauthor: aahill\\nms.author: aahi\\nms.service: cognitive-services\\nms.topic: conceptual\\nms.date: 10/28/2021</p>\\n<hr />\\n<h1 id=\\\"azure-ai-services-and-machine-learning\\\">Azure AI services and machine learning</h1>\\n<p>Azure AI services provides machine learning capabilities to solve general problems such as analyzing text for emotional sentiment or analyzing images to recognize objects or faces..You don't need special machine learning or data science knowledge to use these services../what-are-ai-services.md\\\">Azure AI services</a> is a group of services, each supporting different, generalized prediction capabilities..The services are divided into different categories to help you find the right service..</p>\\n<table>\\n<thead>\\n<tr>\\n<th>Service category</th>\\n<th>Purpose</th>\\n</tr>\\n</thead>\\n<tbody>\\n<tr>\\n<td><a href=\\\"https://azure.microsoft.com/services/cognitive-services/directory/decision/\\\">Decision</a></td>\\n<td>Build apps that surface recommendations for informed and efficient decision-making..</td>\\n</tr>\\n<tr>\\n<td><a href=\\\"https://azure.microsoft.com/services/cognitive-services/directory/lang/\\\">Language</a></td>\\n<td>Allow your apps to process natural language with pre-built scripts, evaluate sentiment and learn how to recognize what users want..</td>\\n</tr>\\n<tr>\\n<td><a href=\\\"https://azure.microsoft.com/services/cognitive-services/directory/search/\\\">Search</a></td>\\n<td>Add Bing Search APIs to your apps and harness the ability to comb billions of webpages, images, videos, and news with a single API call..</td>\\n</tr>\\n<tr>\\n<td><a href=\\\"https://azure.microsoft.com/services/cognitive-services/directory/speech/\\\">Speech</a></td>\", \"id\": null, \"title\": \"Azure AI services and machine learning\", \"filepath\": \"cognitive-services-and-machine-learning.md\", \"url\": \"https://krpraticstorageacc.blob.core.windows.net/azure-openai/cognitive-services-and-machine-learning.md\", \"metadata\": {\"chunking\": \"orignal document size=1188. Scores=5.689296 and None.Org Highlight count=160.Filtering to chunk no. 0/Highlights=63 of size=526\"}, \"chunk_id\": \"0\"}, {\"content\": \"<p>How is Azure Cognitive Search related to Azure AI services?</p>\\n<p><a href=\\\"../search/search-what-is-azure-search.md\\\">Azure Cognitive Search</a> is a separate cloud search service that optionally uses Azure AI services to add image and natural language processing to indexing workloads.


key: _node_content
string_value:
{"id_": "4d6690b5-62b4-48a3-898f-dc513c35a534", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "9b1f6272-7419-4aff-8f11-399ed5865b9e", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "a5ffcb818a44482890e35fbf75f880025cebadf69e3b2e423ff4951db64439ce", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "ba57447e-5f21-42d9-b5c1-048789570060", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "40a8b5d2ec9f7babf8c0ef6e5fcf94525316a6acae251b83d030b4702b4eefe3", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "dda81d12-c37c-466f-a7de-d82d6b89da4f", "node_type": "1", "metadata": {}, "hash": "31d0fc2d74f195d246d675dcf9a658ff9d584c8e090e32185482394725b7e5c9", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 17443, "end_char_idx": 20195, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
Scores=5.689296 and None.Org Highlight count=160.Filtering to chunk no. 0/Highlights=63 of size=526\"}, \"chunk_id\": \"0\"}, {\"content\": \"<p>How is Azure Cognitive Search related to Azure AI services?</p>\\n<p><a href=\\\"../search/search-what-is-azure-search.md\\\">Azure Cognitive Search</a> is a separate cloud search service that optionally uses Azure AI services to add image and natural language processing to indexing workloads. Azure AI services is exposed in Azure Cognitive Search through <a href=\\\"../search/cognitive-search-predefined-skills.md\\\">built-in skills</a> that wrap individual APIs. You can use a free resource for walkthroughs, but plan on creating and attaching a <a href=\\\"../search/cognitive-search-attach-cognitive-services.md\\\">billable resource</a> for larger volumes.</p>\\n<h2 id=\\\"how-can-you-use-azure-ai-services\\\">How can you use Azure AI services?</h2>\\n<p>Each service provides information about your data. You can combine services together to chain solutions such as converting speech (audio) to text, translating the text into many languages, then using the translated languages to get answers from a knowledge base. While Azure AI services can be used to create intelligent solutions on their own, they can also be combined with traditional machine learning projects to supplement models or accelerate the development process. </p>\\n<p>Azure AI services that provide exported models for other machine learning tools:</p>\\n<table>\\n<thead>\\n<tr>\\n<th>Azure AI service</th>\\n<th>Model information</th>\\n</tr>\\n</thead>\\n<tbody>\\n<tr>\\n<td><a href=\\\"./custom-vision-service/overview.md\\\">Custom Vision</a></td>\\n<td><a href=\\\"./custom-vision-service/export-model-python.md\\\">Export</a> for Tensorflow for Android, CoreML for iOS11, ONNX for Windows ML</td>\\n</tr>\\n</tbody>\\n</table>\\n<h2 id=\\\"learn-more\\\">Learn more</h2>\\n<ul>\\n<li><a href=\\\"/azure/architecture/data-guide/technology-choices/data-science-and-machine-learning\\\">Architecture Guide - What are the machine learning products at Microsoft?</a></li>\\n<li><a href=\\\"../machine-learning/concept-deep-learning-vs-machine-learning.md\\\">Machine learning - Introduction to deep learning vs. machine learning</a></li>\\n</ul>\\n<h2 id=\\\"next-steps\\\">Next steps</h2>\\n<ul>\\n<li>Create your Azure AI services resource in the <a href=\\\"multi-service-resource.md?pivots=azportal\\\">Azure portal</a> or with <a href=\\\"./multi-service-resource.md?pivots=azcli\\\">Azure CLI</a>.</li>\\n<li>Learn how to <a href=\\\"authentication.md\\\">authenticate</a> with your Azure AI service.</li>\\n<li>Use <a href=\\\"diagnostic-logging.md\\\">diagnostic logging</a> for issue identification and debugging.


key: _node_content
string_value:
{"id_": "dda81d12-c37c-466f-a7de-d82d6b89da4f", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "9b1f6272-7419-4aff-8f11-399ed5865b9e", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "a5ffcb818a44482890e35fbf75f880025cebadf69e3b2e423ff4951db64439ce", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "4d6690b5-62b4-48a3-898f-dc513c35a534", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "a2dcab5bab5c1c2dd8edf37479a56f5e04f58c4959b103a936a08b325891e96e", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "fc5558df-59c3-4d32-8126-0d63330492ab", "node_type": "1", "metadata": {}, "hash": "b02ac08f4030b5348b2e9c7324f9b74bb33e58ac25ceebe006c9abf92dd1bf7a", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": null, "end_char_idx": null, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
machine learning</a></li>\\n</ul>\\n<h2 id=\\\"next-steps\\\">Next steps</h2>\\n<ul>\\n<li>Create your Azure AI services resource in the <a href=\\\"multi-service-resource.md?pivots=azportal\\\">Azure portal</a> or with <a href=\\\"./multi-service-resource.md?pivots=azcli\\\">Azure CLI</a>.</li>\\n<li>Learn how to <a href=\\\"authentication.md\\\">authenticate</a> with your Azure AI service.</li>\\n<li>Use <a href=\\\"diagnostic-logging.md\\\">diagnostic logging</a> for issue identification and debugging. </li>\\n<li>Deploy an Azure AI service in a Docker <a href=\\\"cognitive-services-container-support.md\\\">container</a>.</li>\\n<li>Keep up to date with <a href=\\\"https://azure.microsoft.com/updates/?product=cognitive-services\\\">service updates</a>.</li>\\n</ul>\", \"id\": null, \"title\": \"Azure AI services and machine learning\", \"filepath\": \"cognitive-services-and-machine-learning.md\", \"url\": \"https://krpraticstorageacc.blob.core.windows.net/azure-openai/cognitive-services-and-machine-learning.md\", \"metadata\": {\"chunking\": \"orignal document size=793. Scores=3.3767838 and None.Org Highlight count=69.\"}, \"chunk_id\": \"3\"}, {\"content\": \"<p>How are Azure AI services different from machine learning?.</p>\\n<p>Azure AI services provide a trained model for you.This brings data and an algorithm together, available from a REST API(s) or SDK.An Azure AI service provides answers to general problems such as key phrases in text or item identification in images.</p>\\n<p>Machine learning is a process that generally requires a longer period of time to implement successfully.This time is spent on data collection, cleaning, transformation, algorithm selection, model training, and deployment to get to the same level of functionality provided by an Azure AI service.With machine learning, it is possible to provide answers to highly specialized and/or specific problems.Machine learning problems require familiarity with the specific subject matter and data of the problem under consideration, as well as expertise in data science.</p>\\n<h2 id=\\\"what-kind-of-data-do-you-have\\\">What kind of data do you have?.</h2>\\n<p>Azure AI services, as a group of services, can require none, some, or all custom data for the trained model.</p>\\n<h3 id=\\\"no-additional-training-data-required\\\">No additional training data required</h3>\\n<p>Services that provide a fully-trained model can be treated as a <em>opaque box</em>.You don't need to know how they work or what data was used to train them.</p>\\n<h3 id=\\\"some-or-all-training-data-required\\\">Some or all training data required</h3>\\n<p>Some services allow you to bring your own data, then train a model.This allows you to extend the model using the Service's data and algorithm with your own data.The output matches your needs.When you bring your own data, you may need to tag the data in a way specific to the service.For example, if you are training a model to identify flowers, you can provide a catalog of flower images along with the location of the flower in each image to train the model.


key: _node_content
string_value:
{"id_": "fc5558df-59c3-4d32-8126-0d63330492ab", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "9b1f6272-7419-4aff-8f11-399ed5865b9e", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "a5ffcb818a44482890e35fbf75f880025cebadf69e3b2e423ff4951db64439ce", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "dda81d12-c37c-466f-a7de-d82d6b89da4f", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "b4f99c8a9dbf8f52199fa196eeca5a298d7e018bb3f69faf387f2477be468dea", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "82886a20-529b-414e-8b80-93b808ce01c1", "node_type": "1", "metadata": {}, "hash": "a7bddc70942f09041900a29c8bcf0ae998ed248bbbd88d5bbda50751604565a3", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": null, "end_char_idx": null, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
available from a REST API(s) or SDK.An Azure AI service provides answers to general problems such as key phrases in text or item identification in images.</p>\\n<p>Machine learning is a process that generally requires a longer period of time to implement successfully.This time is spent on data collection, cleaning, transformation, algorithm selection, model training, and deployment to get to the same level of functionality provided by an Azure AI service.With machine learning, it is possible to provide answers to highly specialized and/or specific problems.Machine learning problems require familiarity with the specific subject matter and data of the problem under consideration, as well as expertise in data science.</p>\\n<h2 id=\\\"what-kind-of-data-do-you-have\\\">What kind of data do you have?.</h2>\\n<p>Azure AI services, as a group of services, can require none, some, or all custom data for the trained model.</p>\\n<h3 id=\\\"no-additional-training-data-required\\\">No additional training data required</h3>\\n<p>Services that provide a fully-trained model can be treated as a <em>opaque box</em>.You don't need to know how they work or what data was used to train them.</p>\\n<h3 id=\\\"some-or-all-training-data-required\\\">Some or all training data required</h3>\\n<p>Some services allow you to bring your own data, then train a model.This allows you to extend the model using the Service's data and algorithm with your own data.The output matches your needs.When you bring your own data, you may need to tag the data in a way specific to the service.For example, if you are training a model to identify flowers, you can provide a catalog of flower images along with the location of the flower in each image to train the model.These services process significant amounts of model data.</p>\\n<h2 id=\\\"service-requirements-for-the-data-model\\\">Service requirements for the data model</h2>\\n<p>The following data categorizes each service by which kind of data it allows or requires.</p>\\n<table>\\n<thead>\\n<tr>\\n<th>Azure AI service</th>\\n<th>No training data required</th>\\n<th>You provide some or all training data</th>\\n<th>Real-time or near real-time data collection</th>\\n</tr>\\n</thead>\\n<tbody>\\n<tr>\\n<td><a href=\\\"./LUIS/what-is-luis.md\\\">Language Understanding (LUIS)</a></td>\\n<td></td>\\n<td>x</td>\\n<td></td>\\n</tr>\\n<tr>\\n<td><a href=\\\"./personalizer/what-is-personalizer.md\\\">Personalizer</a><sup>1</sup></sup></td>\\n<td>x</td>\\n<td>x</td>\\n<td>x</td>\\n</tr>\\n<tr>\\n<td><a href=\\\"./computer-vision/overview.md\\\">Vision</a></td>\\n<td>x</td>\\n<td></td>\\n<td></td>\\n</tr>\\n</tbody>\\n</table>\\n<p><sup>1</sup> Personalizer only needs training data collected by the service (as it operates in real-time) to evaluate your policy and data.


key: _node_content
string_value:
{"id_": "82886a20-529b-414e-8b80-93b808ce01c1", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "9b1f6272-7419-4aff-8f11-399ed5865b9e", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "a5ffcb818a44482890e35fbf75f880025cebadf69e3b2e423ff4951db64439ce", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "fc5558df-59c3-4d32-8126-0d63330492ab", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "0a0f8be72c38895c4a5440e6d3fb443b235310589ea9f09881251cb3e536a4a9", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "2cceedc1-f15a-4bcd-9525-7f7c3e67dec2", "node_type": "1", "metadata": {}, "hash": "7a14c3c2c76b52835756cd93ed995e5c5cfa322049edf65214ed8fb60705fa50", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": null, "end_char_idx": null, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
For example, if you are training a model to identify flowers, you can provide a catalog of flower images along with the location of the flower in each image to train the model.These services process significant amounts of model data.</p>\\n<h2 id=\\\"service-requirements-for-the-data-model\\\">Service requirements for the data model</h2>\\n<p>The following data categorizes each service by which kind of data it allows or requires.</p>\\n<table>\\n<thead>\\n<tr>\\n<th>Azure AI service</th>\\n<th>No training data required</th>\\n<th>You provide some or all training data</th>\\n<th>Real-time or near real-time data collection</th>\\n</tr>\\n</thead>\\n<tbody>\\n<tr>\\n<td><a href=\\\"./LUIS/what-is-luis.md\\\">Language Understanding (LUIS)</a></td>\\n<td></td>\\n<td>x</td>\\n<td></td>\\n</tr>\\n<tr>\\n<td><a href=\\\"./personalizer/what-is-personalizer.md\\\">Personalizer</a><sup>1</sup></sup></td>\\n<td>x</td>\\n<td>x</td>\\n<td>x</td>\\n</tr>\\n<tr>\\n<td><a href=\\\"./computer-vision/overview.md\\\">Vision</a></td>\\n<td>x</td>\\n<td></td>\\n<td></td>\\n</tr>\\n</tbody>\\n</table>\\n<p><sup>1</sup> Personalizer only needs training data collected by the service (as it operates in real-time) to evaluate your policy and data.</p>\\n<h2 id=\\\"where-can-you-use-azure-ai-services\\\">Where can you use Azure AI services?.</h2>\\n<p>The services are used in any application that can make REST API(s) or SDK calls.Examples of applications include web sites, bots, virtual or mixed reality, desktop and mobile applications.\", \"id\": null, \"title\": \"Azure AI services and machine learning\", \"filepath\": \"cognitive-services-and-machine-learning.md\", \"url\": \"https://krpraticstorageacc.blob.core.windows.net/azure-openai/cognitive-services-and-machine-learning.md\", \"metadata\": {\"chunking\": \"orignal document size=1734. Scores=3.1447978 and None.Org Highlight count=66.Filtering to highlight size=891\"}, \"chunk_id\": \"4\"}], \"intent\": \"[\\\"What are the differences between Azure Machine Learning and Azure AI services?\\\"]\"}",
                  "end_turn": false
                }
              ]
            }
          }
        }
      ]
    }


If you would prefer to stream the response from the model, you can pass the `stream=True` keyword argument:


```python
response = openai.ChatCompletion.create(
    messages=[{"role": "user", "content": "What are the differences between Azure Machine Learning and Azure AI services?


key: _node_content
string_value:
{"id_": "2cceedc1-f15a-4bcd-9525-7f7c3e67dec2", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "9b1f6272-7419-4aff-8f11-399ed5865b9e", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "a5ffcb818a44482890e35fbf75f880025cebadf69e3b2e423ff4951db64439ce", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "82886a20-529b-414e-8b80-93b808ce01c1", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "93f17dfee91c6579a3ae0420953ce25c45e772ffacc06dbf1fb0be450ae4f1df", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "00e620df-4936-4cf9-8a95-0ff37532ad92", "node_type": "1", "metadata": {}, "hash": "b1e286223839899cbd0ebf0b0c77aca33be11af8988cdba56fab51b0a4bf2c91", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": null, "end_char_idx": null, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
/computer-vision/overview.md\\\">Vision</a></td>\\n<td>x</td>\\n<td></td>\\n<td></td>\\n</tr>\\n</tbody>\\n</table>\\n<p><sup>1</sup> Personalizer only needs training data collected by the service (as it operates in real-time) to evaluate your policy and data.</p>\\n<h2 id=\\\"where-can-you-use-azure-ai-services\\\">Where can you use Azure AI services?.</h2>\\n<p>The services are used in any application that can make REST API(s) or SDK calls.Examples of applications include web sites, bots, virtual or mixed reality, desktop and mobile applications.\", \"id\": null, \"title\": \"Azure AI services and machine learning\", \"filepath\": \"cognitive-services-and-machine-learning.md\", \"url\": \"https://krpraticstorageacc.blob.core.windows.net/azure-openai/cognitive-services-and-machine-learning.md\", \"metadata\": {\"chunking\": \"orignal document size=1734. Scores=3.1447978 and None.Org Highlight count=66.Filtering to highlight size=891\"}, \"chunk_id\": \"4\"}], \"intent\": \"[\\\"What are the differences between Azure Machine Learning and Azure AI services?\\\"]\"}",
                  "end_turn": false
                }
              ]
            }
          }
        }
      ]
    }


If you would prefer to stream the response from the model, you can pass the `stream=True` keyword argument:


```python
response = openai.ChatCompletion.create(
    messages=[{"role": "user", "content": "What are the differences between Azure Machine Learning and Azure AI services?"}],
    deployment_id="gpt-4",
    dataSources=[
        {
            "type": "AzureCognitiveSearch",
            "parameters": {
                "endpoint": os.environ["SEARCH_ENDPOINT"],
                "key": os.environ["SEARCH_KEY"],
                "indexName": os.environ["SEARCH_INDEX_NAME"],
            }
        }
    ],
    stream=True,
)

for chunk in response:
    delta = chunk.choices[0].delta

    if "role" in delta:
        print("\n"+ delta.role + ": ", end="", flush=True)
    if "content" in delta:
        print(delta.content, end="", flush=True)
    if "context" in delta:
        print(f"Context: {delta.context}", end="", flush=True)
```

    Context: {
      "messages": [
        {
          "role": "tool",
          "content": "{\"citations\":[{\"content\":\"<h2 id=\\\"how-are-azure-ai-services-and-azure-machine-learning-aml-similar\\\">How are Azure AI services and Azure Machine Learning (AML) similar?.</h2>\\n<p>Both have the end-goal of applying artificial intelligence (AI) to enhance business operations, though how each provides this in the respective offerings is different..</p>\\n<p>Generally, the audiences are different:</p>\\n<ul>\\n<li>Azure AI services are for developers without machine-learning experience..</li>\\n<li>Azure Machine Learning is tailored for data scientists.\",\"id\":null,\"title\":\"Azure AI services and machine learning\",\"filepath\":\"cognitive-services-and-machine-learning.md\",\"url\":\"https://krpraticstorageacc.blob.core.windows.net/azure-openai/cognitive-services-and-machine-learning.md\",\"metadata\":{\"chunking\":\"orignal document size=1188.


key: _node_content
string_value:
{"id_": "00e620df-4936-4cf9-8a95-0ff37532ad92", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "9b1f6272-7419-4aff-8f11-399ed5865b9e", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "a5ffcb818a44482890e35fbf75f880025cebadf69e3b2e423ff4951db64439ce", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "2cceedc1-f15a-4bcd-9525-7f7c3e67dec2", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "4cb991890dd99a0d4115661b6571a1e6755292cb1d4e6c24e0a46add288d85d7", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "6eaf3ffb-abdf-44c8-87dc-14883711fb58", "node_type": "1", "metadata": {}, "hash": "bec797742cb75a79d4750cd29ec7a8037baf43a3447d2686de3960ad066a4ac5", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 25096, "end_char_idx": 26794, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
"}],
    deployment_id="gpt-4",
    dataSources=[
        {
            "type": "AzureCognitiveSearch",
            "parameters": {
                "endpoint": os.environ["SEARCH_ENDPOINT"],
                "key": os.environ["SEARCH_KEY"],
                "indexName": os.environ["SEARCH_INDEX_NAME"],
            }
        }
    ],
    stream=True,
)

for chunk in response:
    delta = chunk.choices[0].delta

    if "role" in delta:
        print("\n"+ delta.role + ": ", end="", flush=True)
    if "content" in delta:
        print(delta.content, end="", flush=True)
    if "context" in delta:
        print(f"Context: {delta.context}", end="", flush=True)
```

    Context: {
      "messages": [
        {
          "role": "tool",
          "content": "{\"citations\":[{\"content\":\"<h2 id=\\\"how-are-azure-ai-services-and-azure-machine-learning-aml-similar\\\">How are Azure AI services and Azure Machine Learning (AML) similar?.</h2>\\n<p>Both have the end-goal of applying artificial intelligence (AI) to enhance business operations, though how each provides this in the respective offerings is different..</p>\\n<p>Generally, the audiences are different:</p>\\n<ul>\\n<li>Azure AI services are for developers without machine-learning experience..</li>\\n<li>Azure Machine Learning is tailored for data scientists.\",\"id\":null,\"title\":\"Azure AI services and machine learning\",\"filepath\":\"cognitive-services-and-machine-learning.md\",\"url\":\"https://krpraticstorageacc.blob.core.windows.net/azure-openai/cognitive-services-and-machine-learning.md\",\"metadata\":{\"chunking\":\"orignal document size=1188. Scores=5.689296 and None.Org Highlight count=160.Filtering to chunk no.


key: _node_content
string_value:
{"id_": "6eaf3ffb-abdf-44c8-87dc-14883711fb58", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "9b1f6272-7419-4aff-8f11-399ed5865b9e", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "a5ffcb818a44482890e35fbf75f880025cebadf69e3b2e423ff4951db64439ce", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "00e620df-4936-4cf9-8a95-0ff37532ad92", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "c87ed3e34552bc01a26877f354d12af5559f8e43772af728733acdaead5d029a", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "cb6c8f3e-de6c-41da-8869-32c38e01c3b3", "node_type": "1", "metadata": {}, "hash": "2997ab36c5192865ad0730b8e6b6302c062324ee8c8dd511a6034c7ff7948e41", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 25096, "end_char_idx": 29091, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
"}],
    deployment_id="gpt-4",
    dataSources=[
        {
            "type": "AzureCognitiveSearch",
            "parameters": {
                "endpoint": os.environ["SEARCH_ENDPOINT"],
                "key": os.environ["SEARCH_KEY"],
                "indexName": os.environ["SEARCH_INDEX_NAME"],
            }
        }
    ],
    stream=True,
)

for chunk in response:
    delta = chunk.choices[0].delta

    if "role" in delta:
        print("\n"+ delta.role + ": ", end="", flush=True)
    if "content" in delta:
        print(delta.content, end="", flush=True)
    if "context" in delta:
        print(f"Context: {delta.context}", end="", flush=True)
```

    Context: {
      "messages": [
        {
          "role": "tool",
          "content": "{\"citations\":[{\"content\":\"<h2 id=\\\"how-are-azure-ai-services-and-azure-machine-learning-aml-similar\\\">How are Azure AI services and Azure Machine Learning (AML) similar?.</h2>\\n<p>Both have the end-goal of applying artificial intelligence (AI) to enhance business operations, though how each provides this in the respective offerings is different..</p>\\n<p>Generally, the audiences are different:</p>\\n<ul>\\n<li>Azure AI services are for developers without machine-learning experience..</li>\\n<li>Azure Machine Learning is tailored for data scientists.\",\"id\":null,\"title\":\"Azure AI services and machine learning\",\"filepath\":\"cognitive-services-and-machine-learning.md\",\"url\":\"https://krpraticstorageacc.blob.core.windows.net/azure-openai/cognitive-services-and-machine-learning.md\",\"metadata\":{\"chunking\":\"orignal document size=1188. Scores=5.689296 and None.Org Highlight count=160.Filtering to chunk no. 2/Highlights=30 of size=137\"},\"chunk_id\":\"2\"},{\"content\":\"<td>Convert speech into text and text into natural-sounding speech..Translate from one language to another and enable speaker verification and recognition..</td>\\n</tr>\\n<tr>\\n<td><a href=\\\"https://azure.microsoft.com/services/cognitive-services/directory/vision/\\\">Vision</a></td>\\n<td>Recognize, identify, caption, index, and moderate your pictures, videos, and digital ink content..</td>\\n</tr>\\n<p></tbody>\\n</table></p>\\n<p>Use Azure AI services when you:</p>\\n<ul>\\n<li>Can use a generalized solution..</li>\\n</ul>\\n<p>Use other machine-learning solutions when you:</p>\\n<ul>\\n<li>Need to choose the algorithm and need to train on very specific data..</li>\\n</ul>\\n<h2 id=\\\"what-is-machine-learning\\\">What is machine learning?.</h2>\\n<p>Machine learning is a concept where you bring together data and an algorithm to solve a specific need..Once the data and algorithm are trained, the output is a model that you can use again with different data..The trained model provides insights based on the new data..</p>\\n<p>The process of building a machine learning system requires some knowledge of machine learning or data science..</p>\\n<p>Machine learning is provided using <a href=\\\"/azure/architecture/data-guide/technology-choices/data-science-and-machine-learning?.context=azure%2fmachine-learning%2fstudio%2fcontext%2fml-context\\\">Azure Machine Learning (AML) products and services</a>..</p>\\n<h2 id=\\\"what-is-an-azure-ai-service\\\">What is an Azure AI service?.</h2>\\n<p>An Azure AI service provides part or all of the components in a machine learning solution: data, algorithm, and trained model..These services are meant to require general knowledge about your data without needing experience with machine learning or data science..These services provide both REST API(s) and language-based SDKs..As a result, you need to have programming language knowledge to use the services..</p>\",\"id\":null,\"title\":\"Azure AI services and machine learning\",\"filepath\":\"cognitive-services-and-machine-learning.md\",\"url\":\"https://krpraticstorageacc.blob.core.windows.net/azure-openai/cognitive-services-and-machine-learning.md\",\"metadata\":{\"chunking\":\"orignal document size=1188.


key: _node_content
string_value:
{"id_": "cb6c8f3e-de6c-41da-8869-32c38e01c3b3", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "9b1f6272-7419-4aff-8f11-399ed5865b9e", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "a5ffcb818a44482890e35fbf75f880025cebadf69e3b2e423ff4951db64439ce", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "6eaf3ffb-abdf-44c8-87dc-14883711fb58", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "6bf7385ccef1d9e38231303451f27cdecc05b49285028500bb9b8d3bd7fadf70", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "d7ed90ed-9f51-4649-8b15-d5750d90e108", "node_type": "1", "metadata": {}, "hash": "7de260f6d4ca30c8bfb0319281dc8fa16250ea079e43db30af7ed6480cba6a43", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 29092, "end_char_idx": 32110, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
Scores=5.689296 and None.Org Highlight count=160.Filtering to chunk no. 1/Highlights=67 of size=506\"},\"chunk_id\":\"1\"},{\"content\":\"<hr />\\n<p>title: Azure AI services and Machine Learning\\ntitleSuffix: Azure AI services\\ndescription: Learn where Azure AI services fits in with other Azure offerings for machine learning.\\nservices: cognitive-services\\nmanager: nitinme\\nauthor: aahill\\nms.author: aahi\\nms.service: cognitive-services\\nms.topic: conceptual\\nms.date: 10/28/2021</p>\\n<hr />\\n<h1 id=\\\"azure-ai-services-and-machine-learning\\\">Azure AI services and machine learning</h1>\\n<p>Azure AI services provides machine learning capabilities to solve general problems such as analyzing text for emotional sentiment or analyzing images to recognize objects or faces..You don't need special machine learning or data science knowledge to use these services../what-are-ai-services.md\\\">Azure AI services</a> is a group of services, each supporting different, generalized prediction capabilities..The services are divided into different categories to help you find the right service..</p>\\n<table>\\n<thead>\\n<tr>\\n<th>Service category</th>\\n<th>Purpose</th>\\n</tr>\\n</thead>\\n<tbody>\\n<tr>\\n<td><a href=\\\"https://azure.microsoft.com/services/cognitive-services/directory/decision/\\\">Decision</a></td>\\n<td>Build apps that surface recommendations for informed and efficient decision-making..</td>\\n</tr>\\n<tr>\\n<td><a href=\\\"https://azure.microsoft.com/services/cognitive-services/directory/lang/\\\">Language</a></td>\\n<td>Allow your apps to process natural language with pre-built scripts, evaluate sentiment and learn how to recognize what users want..</td>\\n</tr>\\n<tr>\\n<td><a href=\\\"https://azure.microsoft.com/services/cognitive-services/directory/search/\\\">Search</a></td>\\n<td>Add Bing Search APIs to your apps and harness the ability to comb billions of webpages, images, videos, and news with a single API call..</td>\\n</tr>\\n<tr>\\n<td><a href=\\\"https://azure.microsoft.com/services/cognitive-services/directory/speech/\\\">Speech</a></td>\",\"id\":null,\"title\":\"Azure AI services and machine learning\",\"filepath\":\"cognitive-services-and-machine-learning.md\",\"url\":\"https://krpraticstorageacc.blob.core.windows.net/azure-openai/cognitive-services-and-machine-learning.md\",\"metadata\":{\"chunking\":\"orignal document size=1188. Scores=5.689296 and None.Org Highlight count=160.Filtering to chunk no. 0/Highlights=63 of size=526\"},\"chunk_id\":\"0\"},{\"content\":\"<p>How is Azure Cognitive Search related to Azure AI services?</p>\\n<p><a href=\\\"../search/search-what-is-azure-search.md\\\">Azure Cognitive Search</a> is a separate cloud search service that optionally uses Azure AI services to add image and natural language processing to indexing workloads. Azure AI services is exposed in Azure Cognitive Search through <a href=\\\"../search/cognitive-search-predefined-skills.md\\\">built-in skills</a> that wrap individual APIs.


key: _node_content
string_value:
{"id_": "d7ed90ed-9f51-4649-8b15-d5750d90e108", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "9b1f6272-7419-4aff-8f11-399ed5865b9e", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "a5ffcb818a44482890e35fbf75f880025cebadf69e3b2e423ff4951db64439ce", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "cb6c8f3e-de6c-41da-8869-32c38e01c3b3", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "73ff500412b2119bc538b08aef94950f338b00c098ae69c2d93cfea4c1e4fd99", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "cab96b85-c858-45dd-bbf1-63e61987e2d4", "node_type": "1", "metadata": {}, "hash": "0c0bed4ea213b8c8400bf976b61ab85104fbec0dd50ac4804fc9a3988b7b957c", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 31501, "end_char_idx": 34249, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
Scores=5.689296 and None.Org Highlight count=160.Filtering to chunk no. 0/Highlights=63 of size=526\"},\"chunk_id\":\"0\"},{\"content\":\"<p>How is Azure Cognitive Search related to Azure AI services?</p>\\n<p><a href=\\\"../search/search-what-is-azure-search.md\\\">Azure Cognitive Search</a> is a separate cloud search service that optionally uses Azure AI services to add image and natural language processing to indexing workloads. Azure AI services is exposed in Azure Cognitive Search through <a href=\\\"../search/cognitive-search-predefined-skills.md\\\">built-in skills</a> that wrap individual APIs. You can use a free resource for walkthroughs, but plan on creating and attaching a <a href=\\\"../search/cognitive-search-attach-cognitive-services.md\\\">billable resource</a> for larger volumes.</p>\\n<h2 id=\\\"how-can-you-use-azure-ai-services\\\">How can you use Azure AI services?</h2>\\n<p>Each service provides information about your data. You can combine services together to chain solutions such as converting speech (audio) to text, translating the text into many languages, then using the translated languages to get answers from a knowledge base. While Azure AI services can be used to create intelligent solutions on their own, they can also be combined with traditional machine learning projects to supplement models or accelerate the development process. </p>\\n<p>Azure AI services that provide exported models for other machine learning tools:</p>\\n<table>\\n<thead>\\n<tr>\\n<th>Azure AI service</th>\\n<th>Model information</th>\\n</tr>\\n</thead>\\n<tbody>\\n<tr>\\n<td><a href=\\\"./custom-vision-service/overview.md\\\">Custom Vision</a></td>\\n<td><a href=\\\"./custom-vision-service/export-model-python.md\\\">Export</a> for Tensorflow for Android, CoreML for iOS11, ONNX for Windows ML</td>\\n</tr>\\n</tbody>\\n</table>\\n<h2 id=\\\"learn-more\\\">Learn more</h2>\\n<ul>\\n<li><a href=\\\"/azure/architecture/data-guide/technology-choices/data-science-and-machine-learning\\\">Architecture Guide - What are the machine learning products at Microsoft?</a></li>\\n<li><a href=\\\"../machine-learning/concept-deep-learning-vs-machine-learning.md\\\">Machine learning - Introduction to deep learning vs. machine learning</a></li>\\n</ul>\\n<h2 id=\\\"next-steps\\\">Next steps</h2>\\n<ul>\\n<li>Create your Azure AI services resource in the <a href=\\\"multi-service-resource.md?pivots=azportal\\\">Azure portal</a> or with <a href=\\\"./multi-service-resource.md?pivots=azcli\\\">Azure CLI</a>.</li>\\n<li>Learn how to <a href=\\\"authentication.md\\\">authenticate</a> with your Azure AI service.</li>\\n<li>Use <a href=\\\"diagnostic-logging.md\\\">diagnostic logging</a> for issue identification and debugging.


key: _node_content
string_value:
{"id_": "cab96b85-c858-45dd-bbf1-63e61987e2d4", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "9b1f6272-7419-4aff-8f11-399ed5865b9e", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "a5ffcb818a44482890e35fbf75f880025cebadf69e3b2e423ff4951db64439ce", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "d7ed90ed-9f51-4649-8b15-d5750d90e108", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "acc67ba8c453876bb1ad35d24a0f617f6426246ebd809ff0ec1a6a9d2497537f", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "356ace9b-8a30-47b4-bbc4-7abcb64d63b0", "node_type": "1", "metadata": {}, "hash": "940890d952c47ce6d0e48b62dc26ee5212972543e8268b96044848cd079b2fa8", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": null, "end_char_idx": null, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
machine learning</a></li>\\n</ul>\\n<h2 id=\\\"next-steps\\\">Next steps</h2>\\n<ul>\\n<li>Create your Azure AI services resource in the <a href=\\\"multi-service-resource.md?pivots=azportal\\\">Azure portal</a> or with <a href=\\\"./multi-service-resource.md?pivots=azcli\\\">Azure CLI</a>.</li>\\n<li>Learn how to <a href=\\\"authentication.md\\\">authenticate</a> with your Azure AI service.</li>\\n<li>Use <a href=\\\"diagnostic-logging.md\\\">diagnostic logging</a> for issue identification and debugging. </li>\\n<li>Deploy an Azure AI service in a Docker <a href=\\\"cognitive-services-container-support.md\\\">container</a>.</li>\\n<li>Keep up to date with <a href=\\\"https://azure.microsoft.com/updates/?product=cognitive-services\\\">service updates</a>.</li>\\n</ul>\",\"id\":null,\"title\":\"Azure AI services and machine learning\",\"filepath\":\"cognitive-services-and-machine-learning.md\",\"url\":\"https://krpraticstorageacc.blob.core.windows.net/azure-openai/cognitive-services-and-machine-learning.md\",\"metadata\":{\"chunking\":\"orignal document size=793. Scores=3.3767838 and None.Org Highlight count=69.\"},\"chunk_id\":\"3\"},{\"content\":\"<p>How are Azure AI services different from machine learning?.</p>\\n<p>Azure AI services provide a trained model for you.This brings data and an algorithm together, available from a REST API(s) or SDK.An Azure AI service provides answers to general problems such as key phrases in text or item identification in images.</p>\\n<p>Machine learning is a process that generally requires a longer period of time to implement successfully.This time is spent on data collection, cleaning, transformation, algorithm selection, model training, and deployment to get to the same level of functionality provided by an Azure AI service.With machine learning, it is possible to provide answers to highly specialized and/or specific problems.Machine learning problems require familiarity with the specific subject matter and data of the problem under consideration, as well as expertise in data science.</p>\\n<h2 id=\\\"what-kind-of-data-do-you-have\\\">What kind of data do you have?.</h2>\\n<p>Azure AI services, as a group of services, can require none, some, or all custom data for the trained model.</p>\\n<h3 id=\\\"no-additional-training-data-required\\\">No additional training data required</h3>\\n<p>Services that provide a fully-trained model can be treated as a <em>opaque box</em>.You don't need to know how they work or what data was used to train them.</p>\\n<h3 id=\\\"some-or-all-training-data-required\\\">Some or all training data required</h3>\\n<p>Some services allow you to bring your own data, then train a model.This allows you to extend the model using the Service's data and algorithm with your own data.The output matches your needs.When you bring your own data, you may need to tag the data in a way specific to the service.For example, if you are training a model to identify flowers, you can provide a catalog of flower images along with the location of the flower in each image to train the model.These services process significant amounts of model data.


key: _node_content
string_value:
{"id_": "356ace9b-8a30-47b4-bbc4-7abcb64d63b0", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "9b1f6272-7419-4aff-8f11-399ed5865b9e", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "a5ffcb818a44482890e35fbf75f880025cebadf69e3b2e423ff4951db64439ce", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "cab96b85-c858-45dd-bbf1-63e61987e2d4", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "a05697a438657bd2c57656442438d5f29aa08dc626ceb745006d5063c467c345", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "3ae64bf2-643a-45d0-97a0-b11327b945c6", "node_type": "1", "metadata": {}, "hash": "5ff41f842687bceef9d4ad461c29f6f4371a67c506b5cbaa530a3c88c8fb013b", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": null, "end_char_idx": null, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
An Azure AI service provides answers to general problems such as key phrases in text or item identification in images.</p>\\n<p>Machine learning is a process that generally requires a longer period of time to implement successfully.This time is spent on data collection, cleaning, transformation, algorithm selection, model training, and deployment to get to the same level of functionality provided by an Azure AI service.With machine learning, it is possible to provide answers to highly specialized and/or specific problems.Machine learning problems require familiarity with the specific subject matter and data of the problem under consideration, as well as expertise in data science.</p>\\n<h2 id=\\\"what-kind-of-data-do-you-have\\\">What kind of data do you have?.</h2>\\n<p>Azure AI services, as a group of services, can require none, some, or all custom data for the trained model.</p>\\n<h3 id=\\\"no-additional-training-data-required\\\">No additional training data required</h3>\\n<p>Services that provide a fully-trained model can be treated as a <em>opaque box</em>.You don't need to know how they work or what data was used to train them.</p>\\n<h3 id=\\\"some-or-all-training-data-required\\\">Some or all training data required</h3>\\n<p>Some services allow you to bring your own data, then train a model.This allows you to extend the model using the Service's data and algorithm with your own data.The output matches your needs.When you bring your own data, you may need to tag the data in a way specific to the service.For example, if you are training a model to identify flowers, you can provide a catalog of flower images along with the location of the flower in each image to train the model.These services process significant amounts of model data.</p>\\n<h2 id=\\\"service-requirements-for-the-data-model\\\">Service requirements for the data model</h2>\\n<p>The following data categorizes each service by which kind of data it allows or requires.</p>\\n<table>\\n<thead>\\n<tr>\\n<th>Azure AI service</th>\\n<th>No training data required</th>\\n<th>You provide some or all training data</th>\\n<th>Real-time or near real-time data collection</th>\\n</tr>\\n</thead>\\n<tbody>\\n<tr>\\n<td><a href=\\\"./LUIS/what-is-luis.md\\\">Language Understanding (LUIS)</a></td>\\n<td></td>\\n<td>x</td>\\n<td></td>\\n</tr>\\n<tr>\\n<td><a href=\\\"./personalizer/what-is-personalizer.md\\\">Personalizer</a><sup>1</sup></sup></td>\\n<td>x</td>\\n<td>x</td>\\n<td>x</td>\\n</tr>\\n<tr>\\n<td><a href=\\\"./computer-vision/overview.md\\\">Vision</a></td>\\n<td>x</td>\\n<td></td>\\n<td></td>\\n</tr>\\n</tbody>\\n</table>\\n<p><sup>1</sup> Personalizer only needs training data collected by the service (as it operates in real-time) to evaluate your policy and data.


key: _node_content
string_value:
{"id_": "3ae64bf2-643a-45d0-97a0-b11327b945c6", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "9b1f6272-7419-4aff-8f11-399ed5865b9e", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "a5ffcb818a44482890e35fbf75f880025cebadf69e3b2e423ff4951db64439ce", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "356ace9b-8a30-47b4-bbc4-7abcb64d63b0", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "5bc9ad40533b9939bcd352739c7835afe03137699566cfbc6f9d47d1541ea98b", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "f634e373-9529-4d54-b0f2-305290b6248e", "node_type": "1", "metadata": {}, "hash": "3202515416a0079d3f930ddc20a4afa85dc352479a0af5454ed00aa5b16d4214", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": null, "end_char_idx": null, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
For example, if you are training a model to identify flowers, you can provide a catalog of flower images along with the location of the flower in each image to train the model.These services process significant amounts of model data.</p>\\n<h2 id=\\\"service-requirements-for-the-data-model\\\">Service requirements for the data model</h2>\\n<p>The following data categorizes each service by which kind of data it allows or requires.</p>\\n<table>\\n<thead>\\n<tr>\\n<th>Azure AI service</th>\\n<th>No training data required</th>\\n<th>You provide some or all training data</th>\\n<th>Real-time or near real-time data collection</th>\\n</tr>\\n</thead>\\n<tbody>\\n<tr>\\n<td><a href=\\\"./LUIS/what-is-luis.md\\\">Language Understanding (LUIS)</a></td>\\n<td></td>\\n<td>x</td>\\n<td></td>\\n</tr>\\n<tr>\\n<td><a href=\\\"./personalizer/what-is-personalizer.md\\\">Personalizer</a><sup>1</sup></sup></td>\\n<td>x</td>\\n<td>x</td>\\n<td>x</td>\\n</tr>\\n<tr>\\n<td><a href=\\\"./computer-vision/overview.md\\\">Vision</a></td>\\n<td>x</td>\\n<td></td>\\n<td></td>\\n</tr>\\n</tbody>\\n</table>\\n<p><sup>1</sup> Personalizer only needs training data collected by the service (as it operates in real-time) to evaluate your policy and data.</p>\\n<h2 id=\\\"where-can-you-use-azure-ai-services\\\">Where can you use Azure AI services?.</h2>\\n<p>The services are used in any application that can make REST API(s) or SDK calls.Examples of applications include web sites, bots, virtual or mixed reality, desktop and mobile applications.\",\"id\":null,\"title\":\"Azure AI services and machine learning\",\"filepath\":\"cognitive-services-and-machine-learning.md\",\"url\":\"https://krpraticstorageacc.blob.core.windows.net/azure-openai/cognitive-services-and-machine-learning.md\",\"metadata\":{\"chunking\":\"orignal document size=1734. Scores=3.1447978 and None.Org Highlight count=66.Filtering to highlight size=891\"},\"chunk_id\":\"4\"}],\"intent\":\"[\\\"What are the differences between Azure Machine Learning and Azure AI services?\\\"]\"}",
          "end_turn": false
        }
      ]
    }
    assistant: Azure AI services and Azure Machine Learning (AML) both aim to apply artificial intelligence (AI) to enhance business operations, but they target different audiences and offer different capabilities [doc1]. 
    
    Azure AI services are designed for developers without machine learning experience and provide pre-trained models to solve general problems such as text analysis, image recognition, and natural language processing [doc5]. These services require general knowledge about your data without needing experience with machine learning or data science and provide REST APIs and language-based SDKs [doc2].
    
    On the other hand, Azure Machine Learning is tailored for data scientists and offers a platform to build, train, and deploy custom machine learning models [doc1].


key: _node_content
string_value:
{"id_": "f634e373-9529-4d54-b0f2-305290b6248e", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "9b1f6272-7419-4aff-8f11-399ed5865b9e", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "a5ffcb818a44482890e35fbf75f880025cebadf69e3b2e423ff4951db64439ce", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "3ae64bf2-643a-45d0-97a0-b11327b945c6", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/archive/chat_with_your_own_data.ipynb", "title": "Azure chat completion models with your own data (preview)", "tokens": 9636, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "0ecea6e855ae93ed8803211a1c1f24e4735051bf7412407a07ac5dfb897b9f91", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": null, "end_char_idx": null, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
</p>\\n<h2 id=\\\"where-can-you-use-azure-ai-services\\\">Where can you use Azure AI services?.</h2>\\n<p>The services are used in any application that can make REST API(s) or SDK calls.Examples of applications include web sites, bots, virtual or mixed reality, desktop and mobile applications.\",\"id\":null,\"title\":\"Azure AI services and machine learning\",\"filepath\":\"cognitive-services-and-machine-learning.md\",\"url\":\"https://krpraticstorageacc.blob.core.windows.net/azure-openai/cognitive-services-and-machine-learning.md\",\"metadata\":{\"chunking\":\"orignal document size=1734. Scores=3.1447978 and None.Org Highlight count=66.Filtering to highlight size=891\"},\"chunk_id\":\"4\"}],\"intent\":\"[\\\"What are the differences between Azure Machine Learning and Azure AI services?\\\"]\"}",
          "end_turn": false
        }
      ]
    }
    assistant: Azure AI services and Azure Machine Learning (AML) both aim to apply artificial intelligence (AI) to enhance business operations, but they target different audiences and offer different capabilities [doc1]. 
    
    Azure AI services are designed for developers without machine learning experience and provide pre-trained models to solve general problems such as text analysis, image recognition, and natural language processing [doc5]. These services require general knowledge about your data without needing experience with machine learning or data science and provide REST APIs and language-based SDKs [doc2].
    
    On the other hand, Azure Machine Learning is tailored for data scientists and offers a platform to build, train, and deploy custom machine learning models [doc1]. It requires knowledge of machine learning or data science and allows users to choose the algorithm and train on very specific data [doc2].
    
    In summary, Azure AI services offer pre-trained models for developers without machine learning experience, while Azure Machine Learning is a platform for data scientists to build and deploy custom machine learning models.


key: url
string_value:
https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb


key: _node_content
string_value:
{"id_": "04df1968-31ec-450f-87bb-a2497d4ff02f", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "ad421ac5-73f9-4b31-8f8e-aa07b5c3829f", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "0a1e8673e7bb6f84df1ef3c7408e95992d51bd540e2432875166d4aa93603de1", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "e8c53fc7-9710-4e1b-b20a-ee29f8736b8a", "node_type": "1", "metadata": {}, "hash": "59b13ccba574a65fddad89c43e9ee813a49c482fc36db59b66e7352709d90877", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 0, "end_char_idx": 2986, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
# Translate a book written in LaTeX from Slovenian into English

With permission of the author, we will demonstrate how to translate the book [Euclidean Plane Geometry](https://sites.google.com/site/projektivna/), written by Milan Mitrović from Slovenian into English, without modifying any of the LaTeX commands.

To achieve this, we will first split the book into chunks, each roughly a page long, then translate each chunk into English, and finally stitch them back together.

## 1. Read in the data


```python
from openai import OpenAI
import os
from transformers import GPT2Tokenizer

client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY", "<your OpenAI API key if you didn't set as an env var>"))

# OpenAI GPT-2 tokenizer is the same as GPT-3 tokenizer
# we use it to count the number of tokens in the text
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")

with open("data/geometry_slovenian.tex", "r") as f:
    text = f.read()
```




    1485565



### 1.1 Count the tokens in each chunk


```python
chunks = text.split('\n\n')
ntokens = []
for chunk in chunks:
    ntokens.append(len(tokenizer.encode(chunk)))
max(ntokens)
```

    Token indices sequence length is longer than the specified maximum sequence length for this model (1327 > 1024). Running this sequence through the model will result in indexing errors





    1473



It turns out that a double newline is a good separator in this case, in order not to break the flow of the text. Also no individual chunk is larger than 1500 tokens. The model we will use is text-davinci-002, which has a limit of 4096 tokens, so we don't need to worry about breaking the chunks down further.

We will group the shorter chunks into chunks of around 1000 tokens, to increase the coherence of the text, and decrease the frequency of breaks within the text.


```python
def group_chunks(chunks, ntokens, max_len=1000, hard_max_len=3000):
    """
    Group very short chunks, to form approximately page long chunks.
    """
    batches = []
    cur_batch = ""
    cur_tokens = 0
    
    # iterate over chunks, and group the short ones together
    for chunk, ntoken in zip(chunks, ntokens):
        # discard chunks that exceed hard max length
        if ntoken > hard_max_len:
            print(f"Warning: Chunk discarded for being too long ({ntoken} tokens > {hard_max_len} token limit). Preview: '{chunk[:50]}...'")
            continue

        # if room in current batch, add new chunk
        if cur_tokens + 1 + ntoken <= max_len:
            cur_batch += "\n\n" + chunk
            cur_tokens += 1 + ntoken  # adds 1 token for the two newlines
        # otherwise, record the batch and start a new one
        else:
            batches.append(cur_batch)
            cur_batch = chunk
            cur_tokens = ntoken
            
    if cur_batch:  # add the last batch if it's not empty
        batches.append(cur_batch)
        
    return batches


chunks = group_chunks(chunks, ntokens)
len(chunks)
```




    869


key: _node_content
string_value:
{"id_": "e8c53fc7-9710-4e1b-b20a-ee29f8736b8a", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "ad421ac5-73f9-4b31-8f8e-aa07b5c3829f", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "0a1e8673e7bb6f84df1ef3c7408e95992d51bd540e2432875166d4aa93603de1", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "04df1968-31ec-450f-87bb-a2497d4ff02f", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "766ea11fc00449d587ae7bc65e7cd92ad729fb4446a8138e4e083940a7821ed5", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "86e75fff-f450-4639-a0cc-ef93c00ac935", "node_type": "1", "metadata": {}, "hash": "2b3ed6b41e06d266bd7e812e3757cc9c888fcc18fe5284eca22b8deccf844cac", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 1340, "end_char_idx": 3987, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
1473



It turns out that a double newline is a good separator in this case, in order not to break the flow of the text. Also no individual chunk is larger than 1500 tokens. The model we will use is text-davinci-002, which has a limit of 4096 tokens, so we don't need to worry about breaking the chunks down further.

We will group the shorter chunks into chunks of around 1000 tokens, to increase the coherence of the text, and decrease the frequency of breaks within the text.


```python
def group_chunks(chunks, ntokens, max_len=1000, hard_max_len=3000):
    """
    Group very short chunks, to form approximately page long chunks.
    """
    batches = []
    cur_batch = ""
    cur_tokens = 0
    
    # iterate over chunks, and group the short ones together
    for chunk, ntoken in zip(chunks, ntokens):
        # discard chunks that exceed hard max length
        if ntoken > hard_max_len:
            print(f"Warning: Chunk discarded for being too long ({ntoken} tokens > {hard_max_len} token limit). Preview: '{chunk[:50]}...'")
            continue

        # if room in current batch, add new chunk
        if cur_tokens + 1 + ntoken <= max_len:
            cur_batch += "\n\n" + chunk
            cur_tokens += 1 + ntoken  # adds 1 token for the two newlines
        # otherwise, record the batch and start a new one
        else:
            batches.append(cur_batch)
            cur_batch = chunk
            cur_tokens = ntoken
            
    if cur_batch:  # add the last batch if it's not empty
        batches.append(cur_batch)
        
    return batches


chunks = group_chunks(chunks, ntokens)
len(chunks)
```




    869



Notice that adding a sample untranslated and translated first command, where only the content of the chapter name needs to be translated, helps to get more consistent results.

The format of the prompt sent to the model consists of:
1. A high level instruction to translate only the text, but not commands into the desired language
2. A sample untranslated command, where only the content of the chapter name needs to be translated
3. The chunk of text to be translated
4. The translated sample command from 2, which shows the model the beginning of the translation process

The expected output is the translated chunk of text.


```python
def translate_chunk(chunk, model='gpt-3.5-turbo',
                    dest_language='English',
                    sample_translation=("\poglavje{Osnove Geometrije} \label{osn9Geom}", "\poglavje{The basics of Geometry} \label{osn9Geom}")
                    ):
    prompt = f'''Translate only the text from the following LaTeX document into {dest_language}.


key: _node_content
string_value:
{"id_": "86e75fff-f450-4639-a0cc-ef93c00ac935", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "ad421ac5-73f9-4b31-8f8e-aa07b5c3829f", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "0a1e8673e7bb6f84df1ef3c7408e95992d51bd540e2432875166d4aa93603de1", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "e8c53fc7-9710-4e1b-b20a-ee29f8736b8a", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "456a8284cf138f651522370e1fabaf9ec3a9bda636536b68355f2759cbcb4c7f", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "3abb6cf8-a326-46db-b2df-8001ecec7e14", "node_type": "1", "metadata": {}, "hash": "a782b2ad4c11a9f3d576071942f9dd0e0ff9b280b6d96d7e1d4d340c38a87c98", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 2920, "end_char_idx": 5561, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
chunks = group_chunks(chunks, ntokens)
len(chunks)
```




    869



Notice that adding a sample untranslated and translated first command, where only the content of the chapter name needs to be translated, helps to get more consistent results.

The format of the prompt sent to the model consists of:
1. A high level instruction to translate only the text, but not commands into the desired language
2. A sample untranslated command, where only the content of the chapter name needs to be translated
3. The chunk of text to be translated
4. The translated sample command from 2, which shows the model the beginning of the translation process

The expected output is the translated chunk of text.


```python
def translate_chunk(chunk, model='gpt-3.5-turbo',
                    dest_language='English',
                    sample_translation=("\poglavje{Osnove Geometrije} \label{osn9Geom}", "\poglavje{The basics of Geometry} \label{osn9Geom}")
                    ):
    prompt = f'''Translate only the text from the following LaTeX document into {dest_language}. Leave all LaTeX commands unchanged
    
"""
{sample_translation[0]}
{chunk}"""

{sample_translation[1]}
'''
    response = client.chat.completions.create(
        messages=[{"role": "user", "content":prompt}],
        model=model,
        temperature=0,
        top_p=1,
        max_tokens=1500,
    )
    result = response.choices[0].message.content.strip()
    result = result.replace('"""', '') # remove the double quotes, as we used them to surround the text
    return result
print(translate_chunk(chunks[800], model='gpt-3.5-turbo', dest_language='English'))
```

    Let $\mathcal{I}=\mathcal{S}_{AB} \circ\mathcal{S}_{CA}
        \circ\mathcal{S}_{BC}$. By  \ref{izoZrcdrsprq} is
        $\mathcal{I}$ a mirror reflection. Let $A_1$, $B_1$ and $C_1$ be in order the center points of the lines $BC$, $AC$ and $AB$ of the triangle $ABC$.
        Because it is a right triangle is $\mathcal{I}(A_1C_1)=A_1C_1$, which
        means that the line $A_1C_1$ is of this mirror reflection. It is not
        difficult to prove that for the point $A'_1=\mathcal{I}(A_1)$ (both
        lie on the axis $A_1C_1$) is
        $\overrightarrow{A_1A'_1}=3\overrightarrow{A_1C_1}$, so
        $\mathcal{I}=\mathcal{G}_{3\overrightarrow{A_1C_1}}$.
    
    \item  \res{Given are the points $A$ and $B$ on the same side of the line
    $p$.
    Draw the line  $XY$, which lies on the line $p$ and is consistent
    with the given line $l$, so that the sum
    $|AX|+|XY|+|YB|$ is minimal.}
    
    Let $A'=\mathcal{G}_{\overrightarrow{MN}}(A)$ (where $M,N\in
    p$ and $MN\cong l$).


key: _node_content
string_value:
{"id_": "3abb6cf8-a326-46db-b2df-8001ecec7e14", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "ad421ac5-73f9-4b31-8f8e-aa07b5c3829f", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "0a1e8673e7bb6f84df1ef3c7408e95992d51bd540e2432875166d4aa93603de1", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "86e75fff-f450-4639-a0cc-ef93c00ac935", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "8d89189298afddfcd4068a51523e5a9ec6f456ab0101fbc9bb6bdf1762234e36", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "814875e6-4de1-48f0-815d-fb321b8fd9db", "node_type": "1", "metadata": {}, "hash": "2da577e8eb02dc10820083d09fc69187f372063153f894a604e6d30ca466bd8a", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 4650, "end_char_idx": 6713, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
By  \ref{izoZrcdrsprq} is
        $\mathcal{I}$ a mirror reflection. Let $A_1$, $B_1$ and $C_1$ be in order the center points of the lines $BC$, $AC$ and $AB$ of the triangle $ABC$.
        Because it is a right triangle is $\mathcal{I}(A_1C_1)=A_1C_1$, which
        means that the line $A_1C_1$ is of this mirror reflection. It is not
        difficult to prove that for the point $A'_1=\mathcal{I}(A_1)$ (both
        lie on the axis $A_1C_1$) is
        $\overrightarrow{A_1A'_1}=3\overrightarrow{A_1C_1}$, so
        $\mathcal{I}=\mathcal{G}_{3\overrightarrow{A_1C_1}}$.
    
    \item  \res{Given are the points $A$ and $B$ on the same side of the line
    $p$.
    Draw the line  $XY$, which lies on the line $p$ and is consistent
    with the given line $l$, so that the sum
    $|AX|+|XY|+|YB|$ is minimal.}
    
    Let $A'=\mathcal{G}_{\overrightarrow{MN}}(A)$ (where $M,N\in
    p$ and $MN\cong l$). The point $Y$ is obtained as the intersection of the lines $p$
    and $X'Y$ (see also example \ref{HeronProbl}).
    
    \item  \res{Let $ABC$ be an isosceles right triangle with a right angle at the vertex $A$. What does the composite
    $\mathcal{G}_{\overrightarrow{AB}}\circ \mathcal{G}_{\overrightarrow{CA}}$ represent?}
    
    Let $p$ and $q$ be the simetrali of the sides $CA$ and $AB$ of the triangle
    $ABC$. By  \ref{izoZrcDrsKompSrOsn} is:
     $$\mathcal{G}_{\overrightarrow{AB}}\circ
     \mathcal{G}_{\overrightarrow{CA}}=
     \mathcal{S}_q\circ\mathcal{S}_A\circ\mathcal{S}_A\circ\mathcal{S}_p=
     \mathcal{S}_q\circ\mathcal{S}_p.$$ Because $ABC$ is an isosceles
     right triangle with a right angle at the vertex $A$, the lines $p$ and $q$ are perpendicular and intersect at the center $S$
     of the hypotenuse $BC$. Therefore
     $\mathcal{G}_{\overrightarrow{AB}}\circ
     \mathcal{G}_{\overrightarrow{CA}}=\mathcal{S}_q
     \circ\mathcal{S}_p=\mathcal{S}_S$.
    
    \item \res{In the same plane are given the lines
    $a$, $b$ and $c$.
    Draw the points $A\in a$ and $B\in b$
    so that $\mathcal{S}_c(A)=B$.}


key: _node_content
string_value:
{"id_": "814875e6-4de1-48f0-815d-fb321b8fd9db", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "ad421ac5-73f9-4b31-8f8e-aa07b5c3829f", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "0a1e8673e7bb6f84df1ef3c7408e95992d51bd540e2432875166d4aa93603de1", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "3abb6cf8-a326-46db-b2df-8001ecec7e14", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "3390e3bf7ec5d91e658ff840f6b5949182495eb2653bf80b34d97b001d5dc0a8", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "137c14b3-aaee-4114-a639-02a3096f9f8c", "node_type": "1", "metadata": {}, "hash": "b16408b18cdb67d6c3acaa8c9af161f4b854c4e53fe8ad745d0096ee85e6b151", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 5685, "end_char_idx": 7712, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
\item  \res{Let $ABC$ be an isosceles right triangle with a right angle at the vertex $A$. What does the composite
    $\mathcal{G}_{\overrightarrow{AB}}\circ \mathcal{G}_{\overrightarrow{CA}}$ represent?}
    
    Let $p$ and $q$ be the simetrali of the sides $CA$ and $AB$ of the triangle
    $ABC$. By  \ref{izoZrcDrsKompSrOsn} is:
     $$\mathcal{G}_{\overrightarrow{AB}}\circ
     \mathcal{G}_{\overrightarrow{CA}}=
     \mathcal{S}_q\circ\mathcal{S}_A\circ\mathcal{S}_A\circ\mathcal{S}_p=
     \mathcal{S}_q\circ\mathcal{S}_p.$$ Because $ABC$ is an isosceles
     right triangle with a right angle at the vertex $A$, the lines $p$ and $q$ are perpendicular and intersect at the center $S$
     of the hypotenuse $BC$. Therefore
     $\mathcal{G}_{\overrightarrow{AB}}\circ
     \mathcal{G}_{\overrightarrow{CA}}=\mathcal{S}_q
     \circ\mathcal{S}_p=\mathcal{S}_S$.
    
    \item \res{In the same plane are given the lines
    $a$, $b$ and $c$.
    Draw the points $A\in a$ and $B\in b$
    so that $\mathcal{S}_c(A)=B$.}


We can see here that this one chunk in particular translates only the text, but leaves LaTeX commands intact.

Let's now translate all the chunks in the book - this will take 2-3 hours, as we're processing requests sequentially.


```python
dest_language = "English"

translated_chunks = []
for i, chunk in enumerate(chunks):
    print(str(i+1) + " / " + str(len(chunks)))
    # translate each chunk
    translated_chunks.append(translate_chunk(chunk, model='gpt-3.5-turbo', dest_language=dest_language))

# join the chunks together
result = '\n\n'.join(translated_chunks)

# save the final result
with open(f"data/geometry_{dest_language}.tex", "w") as f:
    f.write(result)
```

    0 / 869
    1 / 869
    2 / 869
    3 / 869
    4 / 869
    5 / 869
    6 / 869
    7 / 869
    8 / 869
    9 / 869
    10 / 869
    11 / 869
    12 / 869
    13 / 869
    14 / 869
    15 / 869
    16 / 869
    17 / 869
    18 / 869
    19 / 869
    20 / 869
    21 / 869
    22 / 869
    23 / 869
    24 / 869


key: _node_content
string_value:
{"id_": "137c14b3-aaee-4114-a639-02a3096f9f8c", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "ad421ac5-73f9-4b31-8f8e-aa07b5c3829f", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "0a1e8673e7bb6f84df1ef3c7408e95992d51bd540e2432875166d4aa93603de1", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "814875e6-4de1-48f0-815d-fb321b8fd9db", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "a0be307b6461d667f0415f79dba3500b5b916b3f0921cc6b458a6fe2f43a9d19", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "2d49fda1-8e83-447b-99b7-806f61f56868", "node_type": "1", "metadata": {}, "hash": "32968ffc0221049c9f4e367a9c884807c98e401297191d73c9752e6460221452", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 6716, "end_char_idx": 8254, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
We can see here that this one chunk in particular translates only the text, but leaves LaTeX commands intact.

Let's now translate all the chunks in the book - this will take 2-3 hours, as we're processing requests sequentially.


```python
dest_language = "English"

translated_chunks = []
for i, chunk in enumerate(chunks):
    print(str(i+1) + " / " + str(len(chunks)))
    # translate each chunk
    translated_chunks.append(translate_chunk(chunk, model='gpt-3.5-turbo', dest_language=dest_language))

# join the chunks together
result = '\n\n'.join(translated_chunks)

# save the final result
with open(f"data/geometry_{dest_language}.tex", "w") as f:
    f.write(result)
```

    0 / 869
    1 / 869
    2 / 869
    3 / 869
    4 / 869
    5 / 869
    6 / 869
    7 / 869
    8 / 869
    9 / 869
    10 / 869
    11 / 869
    12 / 869
    13 / 869
    14 / 869
    15 / 869
    16 / 869
    17 / 869
    18 / 869
    19 / 869
    20 / 869
    21 / 869
    22 / 869
    23 / 869
    24 / 869
    25 / 869
    26 / 869
    27 / 869
    28 / 869
    29 / 869
    30 / 869
    31 / 869
    32 / 869
    33 / 869
    34 / 869
    35 / 869
    36 / 869
    37 / 869
    38 / 869
    39 / 869
    40 / 869
    41 / 869
    42 / 869
    43 / 869
    44 / 869
    45 / 869
    46 / 869
    47 / 869
    48 / 869
    49 / 869
    50 / 869
    51 / 869
    52 / 869
    53 / 869
    54 / 869
    55 / 869
    56 / 869
    57 / 869
    58 / 869
    59 / 869
    60 / 869
    61 / 869
    62 / 869
    63 / 869
    64 / 869
    65 / 869
    66 /


key: _node_content
string_value:
{"id_": "2d49fda1-8e83-447b-99b7-806f61f56868", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "ad421ac5-73f9-4b31-8f8e-aa07b5c3829f", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "0a1e8673e7bb6f84df1ef3c7408e95992d51bd540e2432875166d4aa93603de1", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "137c14b3-aaee-4114-a639-02a3096f9f8c", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "8acbbb5c99d74a31967d58286d8dc93a2fa00c28d6fbecf4f94df04d41bc5fb4", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "5b4d301e-5598-4d10-a528-a191635c923f", "node_type": "1", "metadata": {}, "hash": "e82aa04c62b0b5257c876b80baffb3a6786686c5d3071f0d97b117acf715a163", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 7678, "end_char_idx": 8799, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
22 / 869
    23 / 869
    24 / 869
    25 / 869
    26 / 869
    27 / 869
    28 / 869
    29 / 869
    30 / 869
    31 / 869
    32 / 869
    33 / 869
    34 / 869
    35 / 869
    36 / 869
    37 / 869
    38 / 869
    39 / 869
    40 / 869
    41 / 869
    42 / 869
    43 / 869
    44 / 869
    45 / 869
    46 / 869
    47 / 869
    48 / 869
    49 / 869
    50 / 869
    51 / 869
    52 / 869
    53 / 869
    54 / 869
    55 / 869
    56 / 869
    57 / 869
    58 / 869
    59 / 869
    60 / 869
    61 / 869
    62 / 869
    63 / 869
    64 / 869
    65 / 869
    66 / 869
    67 / 869
    68 / 869
    69 / 869
    70 / 869
    71 / 869
    72 / 869
    73 / 869
    74 / 869
    75 / 869
    76 / 869
    77 / 869
    78 / 869
    79 / 869
    80 / 869
    81 / 869
    82 / 869
    83 / 869
    84 / 869
    85 / 869
    86 / 869
    87 / 869
    88 / 869
    89 / 869
    90 / 869
    91 / 869
    92 / 869
    93 / 869
    94 / 869
    95 / 869
    96 / 869
    97 / 869
    98 / 869
    99 / 869
    100 / 869
    101 / 869
    102 / 869
    103 / 869
    104 / 869
    105 / 869
    106 / 869
    107 / 869


key: _node_content
string_value:
{"id_": "5b4d301e-5598-4d10-a528-a191635c923f", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "ad421ac5-73f9-4b31-8f8e-aa07b5c3829f", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "0a1e8673e7bb6f84df1ef3c7408e95992d51bd540e2432875166d4aa93603de1", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "2d49fda1-8e83-447b-99b7-806f61f56868", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "403ca836b4446c10e5a9f00dca6ce214bef0965b49d70e04c5dfa6544e6f98cf", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "5ee294c1-96aa-4310-8d98-228a7786f431", "node_type": "1", "metadata": {}, "hash": "590cd9182bc4c9e79a95dc4f5d314ac10eccecaf6b88f6d7f57a86f8a67102f0", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 8224, "end_char_idx": 9387, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
64 / 869
    65 / 869
    66 / 869
    67 / 869
    68 / 869
    69 / 869
    70 / 869
    71 / 869
    72 / 869
    73 / 869
    74 / 869
    75 / 869
    76 / 869
    77 / 869
    78 / 869
    79 / 869
    80 / 869
    81 / 869
    82 / 869
    83 / 869
    84 / 869
    85 / 869
    86 / 869
    87 / 869
    88 / 869
    89 / 869
    90 / 869
    91 / 869
    92 / 869
    93 / 869
    94 / 869
    95 / 869
    96 / 869
    97 / 869
    98 / 869
    99 / 869
    100 / 869
    101 / 869
    102 / 869
    103 / 869
    104 / 869
    105 / 869
    106 / 869
    107 / 869
    108 / 869
    109 / 869
    110 / 869
    111 / 869
    112 / 869
    113 / 869
    114 / 869
    115 / 869
    116 / 869
    117 / 869
    118 / 869
    119 / 869
    120 / 869
    121 / 869
    122 / 869
    123 / 869
    124 / 869
    125 / 869
    126 / 869
    127 / 869
    128 / 869
    129 / 869
    130 / 869
    131 / 869
    132 / 869
    133 / 869
    134 / 869
    135 / 869
    136 / 869
    137 / 869
    138 / 869
    139 / 869
    140 / 869
    141 / 869
    142 / 869
    143 / 869
    144 / 869
    145 / 869
    146 / 869
    147 / 869
    148 / 869
    149 / 869


key: _node_content
string_value:
{"id_": "5ee294c1-96aa-4310-8d98-228a7786f431", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "ad421ac5-73f9-4b31-8f8e-aa07b5c3829f", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "0a1e8673e7bb6f84df1ef3c7408e95992d51bd540e2432875166d4aa93603de1", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "5b4d301e-5598-4d10-a528-a191635c923f", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "99f34557de3ed8c01ca3b21c248e8a179bc6225dd6b2864a3257a4a83999099b", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "db3252a2-0044-4cb4-babd-cd5b1468c571", "node_type": "1", "metadata": {}, "hash": "e8e23f1c0469c9cd462a8e0480d776ba2fccda1b1fd0e9b240e9ecc679e73072", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 8766, "end_char_idx": 9969, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
/ 869
    106 / 869
    107 / 869
    108 / 869
    109 / 869
    110 / 869
    111 / 869
    112 / 869
    113 / 869
    114 / 869
    115 / 869
    116 / 869
    117 / 869
    118 / 869
    119 / 869
    120 / 869
    121 / 869
    122 / 869
    123 / 869
    124 / 869
    125 / 869
    126 / 869
    127 / 869
    128 / 869
    129 / 869
    130 / 869
    131 / 869
    132 / 869
    133 / 869
    134 / 869
    135 / 869
    136 / 869
    137 / 869
    138 / 869
    139 / 869
    140 / 869
    141 / 869
    142 / 869
    143 / 869
    144 / 869
    145 / 869
    146 / 869
    147 / 869
    148 / 869
    149 / 869
    150 / 869
    151 / 869
    152 / 869
    153 / 869
    154 / 869
    155 / 869
    156 / 869
    157 / 869
    158 / 869
    159 / 869
    160 / 869
    161 / 869
    162 / 869
    163 / 869
    164 / 869
    165 / 869
    166 / 869
    167 / 869
    168 / 869
    169 / 869
    170 / 869
    171 / 869
    172 / 869
    173 / 869
    174 / 869
    175 / 869
    176 / 869
    177 / 869
    178 / 869
    179 / 869
    180 / 869
    181 / 869
    182 / 869
    183 / 869
    184 / 869
    185 / 869
    186 / 869
    187 / 869
    188 / 869
    189 / 869
    190 / 869
    191


key: _node_content
string_value:
{"id_": "db3252a2-0044-4cb4-babd-cd5b1468c571", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "ad421ac5-73f9-4b31-8f8e-aa07b5c3829f", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "0a1e8673e7bb6f84df1ef3c7408e95992d51bd540e2432875166d4aa93603de1", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "5ee294c1-96aa-4310-8d98-228a7786f431", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "204e8139412faff9e686f995f1b979168f5224ecd6fb90297260d64e586eff9a", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "937793f2-8810-478c-b3a3-5741f28179ea", "node_type": "1", "metadata": {}, "hash": "bcb271b8953ffa7b245f413f281c7091920ef5ba9d32f905079deae9208336cc", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 9350, "end_char_idx": 10549, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
147 / 869
    148 / 869
    149 / 869
    150 / 869
    151 / 869
    152 / 869
    153 / 869
    154 / 869
    155 / 869
    156 / 869
    157 / 869
    158 / 869
    159 / 869
    160 / 869
    161 / 869
    162 / 869
    163 / 869
    164 / 869
    165 / 869
    166 / 869
    167 / 869
    168 / 869
    169 / 869
    170 / 869
    171 / 869
    172 / 869
    173 / 869
    174 / 869
    175 / 869
    176 / 869
    177 / 869
    178 / 869
    179 / 869
    180 / 869
    181 / 869
    182 / 869
    183 / 869
    184 / 869
    185 / 869
    186 / 869
    187 / 869
    188 / 869
    189 / 869
    190 / 869
    191 / 869
    192 / 869
    193 / 869
    194 / 869
    195 / 869
    196 / 869
    197 / 869
    198 / 869
    199 / 869
    200 / 869
    201 / 869
    202 / 869
    203 / 869
    204 / 869
    205 / 869
    206 / 869
    207 / 869
    208 / 869
    209 / 869
    210 / 869
    211 / 869
    212 / 869
    213 / 869
    214 / 869
    215 / 869
    216 / 869
    217 / 869
    218 / 869
    219 / 869
    220 / 869
    221 / 869
    222 / 869
    223 / 869
    224 / 869
    225 / 869
    226 / 869
    227 / 869
    228 / 869
    229 / 869
    230 / 869
    231 / 869
    232 / 869


key: _node_content
string_value:
{"id_": "937793f2-8810-478c-b3a3-5741f28179ea", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "ad421ac5-73f9-4b31-8f8e-aa07b5c3829f", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "0a1e8673e7bb6f84df1ef3c7408e95992d51bd540e2432875166d4aa93603de1", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "db3252a2-0044-4cb4-babd-cd5b1468c571", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "0646ef54ba569092a6dfd639935524e85e1bca515f37405f7379158e18228a44", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "79b9074e-6dde-4592-b712-25e3d80afc03", "node_type": "1", "metadata": {}, "hash": "52555891ac5c03e6ca120301ba820d69a077cdfb20020b5509f70236fcb88660", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 9930, "end_char_idx": 11133, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
869
    189 / 869
    190 / 869
    191 / 869
    192 / 869
    193 / 869
    194 / 869
    195 / 869
    196 / 869
    197 / 869
    198 / 869
    199 / 869
    200 / 869
    201 / 869
    202 / 869
    203 / 869
    204 / 869
    205 / 869
    206 / 869
    207 / 869
    208 / 869
    209 / 869
    210 / 869
    211 / 869
    212 / 869
    213 / 869
    214 / 869
    215 / 869
    216 / 869
    217 / 869
    218 / 869
    219 / 869
    220 / 869
    221 / 869
    222 / 869
    223 / 869
    224 / 869
    225 / 869
    226 / 869
    227 / 869
    228 / 869
    229 / 869
    230 / 869
    231 / 869
    232 / 869
    233 / 869
    234 / 869
    235 / 869
    236 / 869
    237 / 869
    238 / 869
    239 / 869
    240 / 869
    241 / 869
    242 / 869
    243 / 869
    244 / 869
    245 / 869
    246 / 869
    247 / 869
    248 / 869
    249 / 869
    250 / 869
    251 / 869
    252 / 869
    253 / 869
    254 / 869
    255 / 869
    256 / 869
    257 / 869
    258 / 869
    259 / 869
    260 / 869
    261 / 869
    262 / 869
    263 / 869
    264 / 869
    265 / 869
    266 / 869
    267 / 869
    268 / 869
    269 / 869
    270 / 869
    271 / 869
    272 / 869
    273 / 869
    274 /


key: _node_content
string_value:
{"id_": "79b9074e-6dde-4592-b712-25e3d80afc03", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "ad421ac5-73f9-4b31-8f8e-aa07b5c3829f", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "0a1e8673e7bb6f84df1ef3c7408e95992d51bd540e2432875166d4aa93603de1", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "937793f2-8810-478c-b3a3-5741f28179ea", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "b9f85e575428fd4033a08fab5711093353545ad9a824fde62a8fc84caaf124ef", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "4d1fcaa8-3cf4-4b4b-98db-13da55175d6a", "node_type": "1", "metadata": {}, "hash": "0030700de747c928a235e445e2c41a386807de3cd66c8da5ca24a6a4b73e7aa3", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 10512, "end_char_idx": 11711, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
230 / 869
    231 / 869
    232 / 869
    233 / 869
    234 / 869
    235 / 869
    236 / 869
    237 / 869
    238 / 869
    239 / 869
    240 / 869
    241 / 869
    242 / 869
    243 / 869
    244 / 869
    245 / 869
    246 / 869
    247 / 869
    248 / 869
    249 / 869
    250 / 869
    251 / 869
    252 / 869
    253 / 869
    254 / 869
    255 / 869
    256 / 869
    257 / 869
    258 / 869
    259 / 869
    260 / 869
    261 / 869
    262 / 869
    263 / 869
    264 / 869
    265 / 869
    266 / 869
    267 / 869
    268 / 869
    269 / 869
    270 / 869
    271 / 869
    272 / 869
    273 / 869
    274 / 869
    275 / 869
    276 / 869
    277 / 869
    278 / 869
    279 / 869
    280 / 869
    281 / 869
    282 / 869
    283 / 869
    284 / 869
    285 / 869
    286 / 869
    287 / 869
    288 / 869
    289 / 869
    290 / 869
    291 / 869
    292 / 869
    293 / 869
    294 / 869
    295 / 869
    296 / 869
    297 / 869
    298 / 869
    299 / 869
    300 / 869
    301 / 869
    302 / 869
    303 / 869
    304 / 869
    305 / 869
    306 / 869
    307 / 869
    308 / 869
    309 / 869
    310 / 869
    311 / 869
    312 / 869
    313 / 869
    314 / 869
    315 / 869


key: _node_content
string_value:
{"id_": "4d1fcaa8-3cf4-4b4b-98db-13da55175d6a", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "ad421ac5-73f9-4b31-8f8e-aa07b5c3829f", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "0a1e8673e7bb6f84df1ef3c7408e95992d51bd540e2432875166d4aa93603de1", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "79b9074e-6dde-4592-b712-25e3d80afc03", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "a118a4ac891d171cecc70db894b52b1fe555c0477b1fecbbe9d47e86b073a8c9", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "3968f983-18b6-480f-beb2-6ef9011363e9", "node_type": "1", "metadata": {}, "hash": "93e9195bb4d496b16bc85965c34a26f2ce01e9105738f2e7bc9e1a90d339d5a1", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 11100, "end_char_idx": 12299, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
272 / 869
    273 / 869
    274 / 869
    275 / 869
    276 / 869
    277 / 869
    278 / 869
    279 / 869
    280 / 869
    281 / 869
    282 / 869
    283 / 869
    284 / 869
    285 / 869
    286 / 869
    287 / 869
    288 / 869
    289 / 869
    290 / 869
    291 / 869
    292 / 869
    293 / 869
    294 / 869
    295 / 869
    296 / 869
    297 / 869
    298 / 869
    299 / 869
    300 / 869
    301 / 869
    302 / 869
    303 / 869
    304 / 869
    305 / 869
    306 / 869
    307 / 869
    308 / 869
    309 / 869
    310 / 869
    311 / 869
    312 / 869
    313 / 869
    314 / 869
    315 / 869
    316 / 869
    317 / 869
    318 / 869
    319 / 869
    320 / 869
    321 / 869
    322 / 869
    323 / 869
    324 / 869
    325 / 869
    326 / 869
    327 / 869
    328 / 869
    329 / 869
    330 / 869
    331 / 869
    332 / 869
    333 / 869
    334 / 869
    335 / 869
    336 / 869
    337 / 869
    338 / 869
    339 / 869
    340 / 869
    341 / 869
    342 / 869
    343 / 869
    344 / 869
    345 / 869
    346 / 869
    347 / 869
    348 / 869
    349 / 869
    350 / 869
    351 / 869
    352 / 869
    353 / 869
    354 / 869
    355 / 869
    356 / 869
    357 / 869


key: _node_content
string_value:
{"id_": "3968f983-18b6-480f-beb2-6ef9011363e9", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "ad421ac5-73f9-4b31-8f8e-aa07b5c3829f", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "0a1e8673e7bb6f84df1ef3c7408e95992d51bd540e2432875166d4aa93603de1", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "4d1fcaa8-3cf4-4b4b-98db-13da55175d6a", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "9f68ea04484fa0dfdfa074439ae5e5dcbb5a293d6ba480932fd43808c0305254", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "233cca2f-3155-4192-9377-03d0036a17dd", "node_type": "1", "metadata": {}, "hash": "1fea04060b673970828412b09b51f969ea47a80ec20e7621126ec93111761c9a", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 11678, "end_char_idx": 12881, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
/ 869
    314 / 869
    315 / 869
    316 / 869
    317 / 869
    318 / 869
    319 / 869
    320 / 869
    321 / 869
    322 / 869
    323 / 869
    324 / 869
    325 / 869
    326 / 869
    327 / 869
    328 / 869
    329 / 869
    330 / 869
    331 / 869
    332 / 869
    333 / 869
    334 / 869
    335 / 869
    336 / 869
    337 / 869
    338 / 869
    339 / 869
    340 / 869
    341 / 869
    342 / 869
    343 / 869
    344 / 869
    345 / 869
    346 / 869
    347 / 869
    348 / 869
    349 / 869
    350 / 869
    351 / 869
    352 / 869
    353 / 869
    354 / 869
    355 / 869
    356 / 869
    357 / 869
    358 / 869
    359 / 869
    360 / 869
    361 / 869
    362 / 869
    363 / 869
    364 / 869
    365 / 869
    366 / 869
    367 / 869
    368 / 869
    369 / 869
    370 / 869
    371 / 869
    372 / 869
    373 / 869
    374 / 869
    375 / 869
    376 / 869
    377 / 869
    378 / 869
    379 / 869
    380 / 869
    381 / 869
    382 / 869
    383 / 869
    384 / 869
    385 / 869
    386 / 869
    387 / 869
    388 / 869
    389 / 869
    390 / 869
    391 / 869
    392 / 869
    393 / 869
    394 / 869
    395 / 869
    396 / 869
    397 / 869
    398 / 869
    399


key: _node_content
string_value:
{"id_": "233cca2f-3155-4192-9377-03d0036a17dd", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "ad421ac5-73f9-4b31-8f8e-aa07b5c3829f", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "0a1e8673e7bb6f84df1ef3c7408e95992d51bd540e2432875166d4aa93603de1", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "3968f983-18b6-480f-beb2-6ef9011363e9", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "7eee97a62b79bae58b281075185fd24115325026c8fe7183d7750b518c6d5064", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "1b5e9635-01ee-4435-b662-4c952243553a", "node_type": "1", "metadata": {}, "hash": "5d9e63171bf1c3f17841ea92032f13fe24fe572bc35df5ffb2a8f706d9d7cdac", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 12262, "end_char_idx": 13461, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
355 / 869
    356 / 869
    357 / 869
    358 / 869
    359 / 869
    360 / 869
    361 / 869
    362 / 869
    363 / 869
    364 / 869
    365 / 869
    366 / 869
    367 / 869
    368 / 869
    369 / 869
    370 / 869
    371 / 869
    372 / 869
    373 / 869
    374 / 869
    375 / 869
    376 / 869
    377 / 869
    378 / 869
    379 / 869
    380 / 869
    381 / 869
    382 / 869
    383 / 869
    384 / 869
    385 / 869
    386 / 869
    387 / 869
    388 / 869
    389 / 869
    390 / 869
    391 / 869
    392 / 869
    393 / 869
    394 / 869
    395 / 869
    396 / 869
    397 / 869
    398 / 869
    399 / 869
    400 / 869
    401 / 869
    402 / 869
    403 / 869
    404 / 869
    405 / 869
    406 / 869
    407 / 869
    408 / 869
    409 / 869
    410 / 869
    411 / 869
    412 / 869
    413 / 869
    414 / 869
    415 / 869
    416 / 869
    417 / 869
    418 / 869
    419 / 869
    420 / 869
    421 / 869
    422 / 869
    423 / 869
    424 / 869
    425 / 869
    426 / 869
    427 / 869
    428 / 869
    429 / 869
    430 / 869
    431 / 869
    432 / 869
    433 / 869
    434 / 869
    435 / 869
    436 / 869
    437 / 869
    438 / 869
    439 / 869
    440 / 869


key: _node_content
string_value:
{"id_": "1b5e9635-01ee-4435-b662-4c952243553a", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "ad421ac5-73f9-4b31-8f8e-aa07b5c3829f", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "0a1e8673e7bb6f84df1ef3c7408e95992d51bd540e2432875166d4aa93603de1", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "233cca2f-3155-4192-9377-03d0036a17dd", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "bc06c8aa87f18b2c39a42ab8d2fba26c5eb523e8926b221d83c96fdf5a7fb748", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "b0c8a45d-a263-4dab-9b15-76a987826543", "node_type": "1", "metadata": {}, "hash": "000d74d9da6416046430e2811bde2946efc54717907af290c76fbb2c11c8fcf7", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 12842, "end_char_idx": 14045, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
869
    397 / 869
    398 / 869
    399 / 869
    400 / 869
    401 / 869
    402 / 869
    403 / 869
    404 / 869
    405 / 869
    406 / 869
    407 / 869
    408 / 869
    409 / 869
    410 / 869
    411 / 869
    412 / 869
    413 / 869
    414 / 869
    415 / 869
    416 / 869
    417 / 869
    418 / 869
    419 / 869
    420 / 869
    421 / 869
    422 / 869
    423 / 869
    424 / 869
    425 / 869
    426 / 869
    427 / 869
    428 / 869
    429 / 869
    430 / 869
    431 / 869
    432 / 869
    433 / 869
    434 / 869
    435 / 869
    436 / 869
    437 / 869
    438 / 869
    439 / 869
    440 / 869
    441 / 869
    442 / 869
    443 / 869
    444 / 869
    445 / 869
    446 / 869
    447 / 869
    448 / 869
    449 / 869
    450 / 869
    451 / 869
    452 / 869
    453 / 869
    454 / 869
    455 / 869
    456 / 869
    457 / 869
    458 / 869
    459 / 869
    460 / 869
    461 / 869
    462 / 869
    463 / 869
    464 / 869
    465 / 869
    466 / 869
    467 / 869
    468 / 869
    469 / 869
    470 / 869
    471 / 869
    472 / 869
    473 / 869
    474 / 869
    475 / 869
    476 / 869
    477 / 869
    478 / 869
    479 / 869
    480 / 869
    481 / 869
    482 /


key: _node_content
string_value:
{"id_": "b0c8a45d-a263-4dab-9b15-76a987826543", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "ad421ac5-73f9-4b31-8f8e-aa07b5c3829f", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "0a1e8673e7bb6f84df1ef3c7408e95992d51bd540e2432875166d4aa93603de1", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "1b5e9635-01ee-4435-b662-4c952243553a", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "69399f15f4bab2fa7ba9500d545fa321085380f010ed63628da8040fd57a7183", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "3838a9a1-74ac-4e87-8396-0c3d858919ed", "node_type": "1", "metadata": {}, "hash": "8ea29681746367447db2c71acc820750c9092636630b7d6a0bdb5d1130e7c76f", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 13424, "end_char_idx": 14623, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
438 / 869
    439 / 869
    440 / 869
    441 / 869
    442 / 869
    443 / 869
    444 / 869
    445 / 869
    446 / 869
    447 / 869
    448 / 869
    449 / 869
    450 / 869
    451 / 869
    452 / 869
    453 / 869
    454 / 869
    455 / 869
    456 / 869
    457 / 869
    458 / 869
    459 / 869
    460 / 869
    461 / 869
    462 / 869
    463 / 869
    464 / 869
    465 / 869
    466 / 869
    467 / 869
    468 / 869
    469 / 869
    470 / 869
    471 / 869
    472 / 869
    473 / 869
    474 / 869
    475 / 869
    476 / 869
    477 / 869
    478 / 869
    479 / 869
    480 / 869
    481 / 869
    482 / 869
    483 / 869
    484 / 869
    485 / 869
    486 / 869
    487 / 869
    488 / 869
    489 / 869
    490 / 869
    491 / 869
    492 / 869
    493 / 869
    494 / 869
    495 / 869
    496 / 869
    497 / 869
    498 / 869
    499 / 869
    500 / 869
    501 / 869
    502 / 869
    503 / 869
    504 / 869
    505 / 869
    506 / 869
    507 / 869
    508 / 869
    509 / 869
    510 / 869
    511 / 869
    512 / 869
    513 / 869
    514 / 869
    515 / 869
    516 / 869
    517 / 869
    518 / 869
    519 / 869
    520 / 869
    521 / 869
    522 / 869
    523 / 869


key: _node_content
string_value:
{"id_": "3838a9a1-74ac-4e87-8396-0c3d858919ed", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "ad421ac5-73f9-4b31-8f8e-aa07b5c3829f", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "0a1e8673e7bb6f84df1ef3c7408e95992d51bd540e2432875166d4aa93603de1", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "b0c8a45d-a263-4dab-9b15-76a987826543", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "fc7623f761e765577c34a263dd9355aa09c980c512dd49eaa30ab0562a8d1916", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "8c3e4294-d818-4ae3-b71a-e3c0b3beff9e", "node_type": "1", "metadata": {}, "hash": "9f4656977b393ed03628a16d6698c146e6a43a6ef9cd3e738e7ed46c06ebafd8", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 14012, "end_char_idx": 15211, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
480 / 869
    481 / 869
    482 / 869
    483 / 869
    484 / 869
    485 / 869
    486 / 869
    487 / 869
    488 / 869
    489 / 869
    490 / 869
    491 / 869
    492 / 869
    493 / 869
    494 / 869
    495 / 869
    496 / 869
    497 / 869
    498 / 869
    499 / 869
    500 / 869
    501 / 869
    502 / 869
    503 / 869
    504 / 869
    505 / 869
    506 / 869
    507 / 869
    508 / 869
    509 / 869
    510 / 869
    511 / 869
    512 / 869
    513 / 869
    514 / 869
    515 / 869
    516 / 869
    517 / 869
    518 / 869
    519 / 869
    520 / 869
    521 / 869
    522 / 869
    523 / 869
    524 / 869
    525 / 869
    526 / 869
    527 / 869
    528 / 869
    529 / 869
    530 / 869
    531 / 869
    532 / 869
    533 / 869
    534 / 869
    535 / 869
    536 / 869
    537 / 869
    538 / 869
    539 / 869
    540 / 869
    541 / 869
    542 / 869
    543 / 869
    544 / 869
    545 / 869
    546 / 869
    547 / 869
    548 / 869
    549 / 869
    550 / 869
    551 / 869
    552 / 869
    553 / 869
    554 / 869
    555 / 869
    556 / 869
    557 / 869
    558 / 869
    559 / 869
    560 / 869
    561 / 869
    562 / 869
    563 / 869
    564 / 869
    565 / 869


key: _node_content
string_value:
{"id_": "8c3e4294-d818-4ae3-b71a-e3c0b3beff9e", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "ad421ac5-73f9-4b31-8f8e-aa07b5c3829f", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "0a1e8673e7bb6f84df1ef3c7408e95992d51bd540e2432875166d4aa93603de1", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "3838a9a1-74ac-4e87-8396-0c3d858919ed", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "24e8cd3a848b4aad666db9d9fd6033e524e4d0fb3585547912f1c6266e48fc6e", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "4c34419c-511f-49e2-a559-a685a1e41128", "node_type": "1", "metadata": {}, "hash": "3aef619707a5e237aae88f166547aa28e65b7f14ae9e3f88b88086cf3640d69a", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 14590, "end_char_idx": 15793, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
/ 869
    522 / 869
    523 / 869
    524 / 869
    525 / 869
    526 / 869
    527 / 869
    528 / 869
    529 / 869
    530 / 869
    531 / 869
    532 / 869
    533 / 869
    534 / 869
    535 / 869
    536 / 869
    537 / 869
    538 / 869
    539 / 869
    540 / 869
    541 / 869
    542 / 869
    543 / 869
    544 / 869
    545 / 869
    546 / 869
    547 / 869
    548 / 869
    549 / 869
    550 / 869
    551 / 869
    552 / 869
    553 / 869
    554 / 869
    555 / 869
    556 / 869
    557 / 869
    558 / 869
    559 / 869
    560 / 869
    561 / 869
    562 / 869
    563 / 869
    564 / 869
    565 / 869
    566 / 869
    567 / 869
    568 / 869
    569 / 869
    570 / 869
    571 / 869
    572 / 869
    573 / 869
    574 / 869
    575 / 869
    576 / 869
    577 / 869
    578 / 869
    579 / 869
    580 / 869
    581 / 869
    582 / 869
    583 / 869
    584 / 869
    585 / 869
    586 / 869
    587 / 869
    588 / 869
    589 / 869
    590 / 869
    591 / 869
    592 / 869
    593 / 869
    594 / 869
    595 / 869
    596 / 869
    597 / 869
    598 / 869
    599 / 869
    600 / 869
    601 / 869
    602 / 869
    603 / 869
    604 / 869
    605 / 869
    606 / 869
    607


key: _node_content
string_value:
{"id_": "4c34419c-511f-49e2-a559-a685a1e41128", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "ad421ac5-73f9-4b31-8f8e-aa07b5c3829f", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "0a1e8673e7bb6f84df1ef3c7408e95992d51bd540e2432875166d4aa93603de1", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "8c3e4294-d818-4ae3-b71a-e3c0b3beff9e", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "29097cb2430559fd85bc1e756a644f929251cda471ad3d0c96c941e8bb581c21", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "692b6302-9771-4577-a35a-9614c3229858", "node_type": "1", "metadata": {}, "hash": "4eb7dc84d1966049a89dbd277efbc9735543079e342e3e7697f46ce9beb00042", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 15174, "end_char_idx": 16373, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
563 / 869
    564 / 869
    565 / 869
    566 / 869
    567 / 869
    568 / 869
    569 / 869
    570 / 869
    571 / 869
    572 / 869
    573 / 869
    574 / 869
    575 / 869
    576 / 869
    577 / 869
    578 / 869
    579 / 869
    580 / 869
    581 / 869
    582 / 869
    583 / 869
    584 / 869
    585 / 869
    586 / 869
    587 / 869
    588 / 869
    589 / 869
    590 / 869
    591 / 869
    592 / 869
    593 / 869
    594 / 869
    595 / 869
    596 / 869
    597 / 869
    598 / 869
    599 / 869
    600 / 869
    601 / 869
    602 / 869
    603 / 869
    604 / 869
    605 / 869
    606 / 869
    607 / 869
    608 / 869
    609 / 869
    610 / 869
    611 / 869
    612 / 869
    613 / 869
    614 / 869
    615 / 869
    616 / 869
    617 / 869
    618 / 869
    619 / 869
    620 / 869
    621 / 869
    622 / 869
    623 / 869
    624 / 869
    625 / 869
    626 / 869
    627 / 869
    628 / 869
    629 / 869
    630 / 869
    631 / 869
    632 / 869
    633 / 869
    634 / 869
    635 / 869
    636 / 869
    637 / 869
    638 / 869
    639 / 869
    640 / 869
    641 / 869
    642 / 869
    643 / 869
    644 / 869
    645 / 869
    646 / 869
    647 / 869
    648 / 869


key: _node_content
string_value:
{"id_": "692b6302-9771-4577-a35a-9614c3229858", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "ad421ac5-73f9-4b31-8f8e-aa07b5c3829f", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "0a1e8673e7bb6f84df1ef3c7408e95992d51bd540e2432875166d4aa93603de1", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "4c34419c-511f-49e2-a559-a685a1e41128", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "f8966e9d2b94cc8a9ad1c42bd4826537f1a1ae74906df419ed20f0bd82e07bb4", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "148580a6-4e09-4f10-9270-da6f86073cd4", "node_type": "1", "metadata": {}, "hash": "1c72a880bc404f2724a4e83a2118591fbae761d0802b59fea943537f2791cf1a", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 15754, "end_char_idx": 16957, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
869
    605 / 869
    606 / 869
    607 / 869
    608 / 869
    609 / 869
    610 / 869
    611 / 869
    612 / 869
    613 / 869
    614 / 869
    615 / 869
    616 / 869
    617 / 869
    618 / 869
    619 / 869
    620 / 869
    621 / 869
    622 / 869
    623 / 869
    624 / 869
    625 / 869
    626 / 869
    627 / 869
    628 / 869
    629 / 869
    630 / 869
    631 / 869
    632 / 869
    633 / 869
    634 / 869
    635 / 869
    636 / 869
    637 / 869
    638 / 869
    639 / 869
    640 / 869
    641 / 869
    642 / 869
    643 / 869
    644 / 869
    645 / 869
    646 / 869
    647 / 869
    648 / 869
    649 / 869
    650 / 869
    651 / 869
    652 / 869
    653 / 869
    654 / 869
    655 / 869
    656 / 869
    657 / 869
    658 / 869
    659 / 869
    660 / 869
    661 / 869
    662 / 869
    663 / 869
    664 / 869
    665 / 869
    666 / 869
    667 / 869
    668 / 869
    669 / 869
    670 / 869
    671 / 869
    672 / 869
    673 / 869
    674 / 869
    675 / 869
    676 / 869
    677 / 869
    678 / 869
    679 / 869
    680 / 869
    681 / 869
    682 / 869
    683 / 869
    684 / 869
    685 / 869
    686 / 869
    687 / 869
    688 / 869
    689 / 869
    690 /


key: _node_content
string_value:
{"id_": "148580a6-4e09-4f10-9270-da6f86073cd4", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "ad421ac5-73f9-4b31-8f8e-aa07b5c3829f", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "0a1e8673e7bb6f84df1ef3c7408e95992d51bd540e2432875166d4aa93603de1", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "692b6302-9771-4577-a35a-9614c3229858", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "a6e51f4774e700394d2cea9cb43f56cb482fc96663644bc5be34053375887df0", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "7305675d-5f53-4860-bda9-8fb3c369e1a7", "node_type": "1", "metadata": {}, "hash": "a0b1434975fec54653bbbdafec9c1de685047f9e4f3b0bfc9f41c32f83c5fbef", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 16336, "end_char_idx": 17535, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
646 / 869
    647 / 869
    648 / 869
    649 / 869
    650 / 869
    651 / 869
    652 / 869
    653 / 869
    654 / 869
    655 / 869
    656 / 869
    657 / 869
    658 / 869
    659 / 869
    660 / 869
    661 / 869
    662 / 869
    663 / 869
    664 / 869
    665 / 869
    666 / 869
    667 / 869
    668 / 869
    669 / 869
    670 / 869
    671 / 869
    672 / 869
    673 / 869
    674 / 869
    675 / 869
    676 / 869
    677 / 869
    678 / 869
    679 / 869
    680 / 869
    681 / 869
    682 / 869
    683 / 869
    684 / 869
    685 / 869
    686 / 869
    687 / 869
    688 / 869
    689 / 869
    690 / 869
    691 / 869
    692 / 869
    693 / 869
    694 / 869
    695 / 869
    696 / 869
    697 / 869
    698 / 869
    699 / 869
    700 / 869
    701 / 869
    702 / 869
    703 / 869
    704 / 869
    705 / 869
    706 / 869
    707 / 869
    708 / 869
    709 / 869
    710 / 869
    711 / 869
    712 / 869
    713 / 869
    714 / 869
    715 / 869
    716 / 869
    717 / 869
    718 / 869
    719 / 869
    720 / 869
    721 / 869
    722 / 869
    723 / 869
    724 / 869
    725 / 869
    726 / 869
    727 / 869
    728 / 869
    729 / 869
    730 / 869
    731 / 869


key: _node_content
string_value:
{"id_": "7305675d-5f53-4860-bda9-8fb3c369e1a7", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "ad421ac5-73f9-4b31-8f8e-aa07b5c3829f", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "0a1e8673e7bb6f84df1ef3c7408e95992d51bd540e2432875166d4aa93603de1", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "148580a6-4e09-4f10-9270-da6f86073cd4", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "8f571feca8555788d5af0fc26774248a9c6e276e0844aa905b436316311b8cf7", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "f31b09f4-4086-47da-8207-c9e072e3f6ed", "node_type": "1", "metadata": {}, "hash": "36e8e598e303b92e9180b4792018d84110ba0d7e356f5681466eddbadf0028eb", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 16924, "end_char_idx": 18123, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
688 / 869
    689 / 869
    690 / 869
    691 / 869
    692 / 869
    693 / 869
    694 / 869
    695 / 869
    696 / 869
    697 / 869
    698 / 869
    699 / 869
    700 / 869
    701 / 869
    702 / 869
    703 / 869
    704 / 869
    705 / 869
    706 / 869
    707 / 869
    708 / 869
    709 / 869
    710 / 869
    711 / 869
    712 / 869
    713 / 869
    714 / 869
    715 / 869
    716 / 869
    717 / 869
    718 / 869
    719 / 869
    720 / 869
    721 / 869
    722 / 869
    723 / 869
    724 / 869
    725 / 869
    726 / 869
    727 / 869
    728 / 869
    729 / 869
    730 / 869
    731 / 869
    732 / 869
    733 / 869
    734 / 869
    735 / 869
    736 / 869
    737 / 869
    738 / 869
    739 / 869
    740 / 869
    741 / 869
    742 / 869
    743 / 869
    744 / 869
    745 / 869
    746 / 869
    747 / 869
    748 / 869
    749 / 869
    750 / 869
    751 / 869
    752 / 869
    753 / 869
    754 / 869
    755 / 869
    756 / 869
    757 / 869
    758 / 869
    759 / 869
    760 / 869
    761 / 869
    762 / 869
    763 / 869
    764 / 869
    765 / 869
    766 / 869
    767 / 869
    768 / 869
    769 / 869
    770 / 869
    771 / 869
    772 / 869
    773 / 869


key: _node_content
string_value:
{"id_": "f31b09f4-4086-47da-8207-c9e072e3f6ed", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "ad421ac5-73f9-4b31-8f8e-aa07b5c3829f", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "0a1e8673e7bb6f84df1ef3c7408e95992d51bd540e2432875166d4aa93603de1", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "7305675d-5f53-4860-bda9-8fb3c369e1a7", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "fe5cd2ed3e4e0cbab5075b72db5ffbd2af241f285bce689fbc5dada74ea72a6c", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "c921e020-9b68-4aed-bb64-e5e84275f9cd", "node_type": "1", "metadata": {}, "hash": "350db7f546bcaa37594aa96547962839bd4713310c25fcac605bd24297ada823", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 17502, "end_char_idx": 18705, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
/ 869
    730 / 869
    731 / 869
    732 / 869
    733 / 869
    734 / 869
    735 / 869
    736 / 869
    737 / 869
    738 / 869
    739 / 869
    740 / 869
    741 / 869
    742 / 869
    743 / 869
    744 / 869
    745 / 869
    746 / 869
    747 / 869
    748 / 869
    749 / 869
    750 / 869
    751 / 869
    752 / 869
    753 / 869
    754 / 869
    755 / 869
    756 / 869
    757 / 869
    758 / 869
    759 / 869
    760 / 869
    761 / 869
    762 / 869
    763 / 869
    764 / 869
    765 / 869
    766 / 869
    767 / 869
    768 / 869
    769 / 869
    770 / 869
    771 / 869
    772 / 869
    773 / 869
    774 / 869
    775 / 869
    776 / 869
    777 / 869
    778 / 869
    779 / 869
    780 / 869
    781 / 869
    782 / 869
    783 / 869
    784 / 869
    785 / 869
    786 / 869
    787 / 869
    788 / 869
    789 / 869
    790 / 869
    791 / 869
    792 / 869
    793 / 869
    794 / 869
    795 / 869
    796 / 869
    797 / 869
    798 / 869
    799 / 869
    800 / 869
    801 / 869
    802 / 869
    803 / 869
    804 / 869
    805 / 869
    806 / 869
    807 / 869
    808 / 869
    809 / 869
    810 / 869
    811 / 869
    812 / 869
    813 / 869
    814 / 869
    815


key: _node_content
string_value:
{"id_": "c921e020-9b68-4aed-bb64-e5e84275f9cd", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "ad421ac5-73f9-4b31-8f8e-aa07b5c3829f", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "0a1e8673e7bb6f84df1ef3c7408e95992d51bd540e2432875166d4aa93603de1", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "f31b09f4-4086-47da-8207-c9e072e3f6ed", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "f49a48e326d6c8aafe1018ae912dcbe68e36389409578a84e0113f4ce443816f", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "693c3cea-32e0-4a56-8d55-e4f87f8b558e", "node_type": "1", "metadata": {}, "hash": "dcfe5736b44210ff7dd9bef0d431aed242da6caa4dc6610586e8d0a185b92c07", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 18086, "end_char_idx": 19285, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
771 / 869
    772 / 869
    773 / 869
    774 / 869
    775 / 869
    776 / 869
    777 / 869
    778 / 869
    779 / 869
    780 / 869
    781 / 869
    782 / 869
    783 / 869
    784 / 869
    785 / 869
    786 / 869
    787 / 869
    788 / 869
    789 / 869
    790 / 869
    791 / 869
    792 / 869
    793 / 869
    794 / 869
    795 / 869
    796 / 869
    797 / 869
    798 / 869
    799 / 869
    800 / 869
    801 / 869
    802 / 869
    803 / 869
    804 / 869
    805 / 869
    806 / 869
    807 / 869
    808 / 869
    809 / 869
    810 / 869
    811 / 869
    812 / 869
    813 / 869
    814 / 869
    815 / 869
    816 / 869
    817 / 869
    818 / 869
    819 / 869
    820 / 869
    821 / 869
    822 / 869
    823 / 869
    824 / 869
    825 / 869
    826 / 869
    827 / 869
    828 / 869
    829 / 869
    830 / 869
    831 / 869
    832 / 869
    833 / 869
    834 / 869
    835 / 869
    836 / 869
    837 / 869
    838 / 869
    839 / 869
    840 / 869
    841 / 869
    842 / 869
    843 / 869
    844 / 869
    845 / 869
    846 / 869
    847 / 869
    848 / 869
    849 / 869
    850 / 869
    851 / 869
    852 / 869
    853 / 869
    854 / 869
    855 / 869
    856 / 869


key: _node_content
string_value:
{"id_": "693c3cea-32e0-4a56-8d55-e4f87f8b558e", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "ad421ac5-73f9-4b31-8f8e-aa07b5c3829f", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "0a1e8673e7bb6f84df1ef3c7408e95992d51bd540e2432875166d4aa93603de1", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "c921e020-9b68-4aed-bb64-e5e84275f9cd", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/book_translation/translate_latex_book.ipynb", "title": "Translate a book written in LaTeX from Slovenian into English", "tokens": 8114, "retrieve_doc": false, "source": "openai_cookbooks"}, "hash": "b132c767b684bee1a079a2b7133925aa6f11175f43495d3ffedf176e471cd396", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 18666, "end_char_idx": 19453, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
869
    813 / 869
    814 / 869
    815 / 869
    816 / 869
    817 / 869
    818 / 869
    819 / 869
    820 / 869
    821 / 869
    822 / 869
    823 / 869
    824 / 869
    825 / 869
    826 / 869
    827 / 869
    828 / 869
    829 / 869
    830 / 869
    831 / 869
    832 / 869
    833 / 869
    834 / 869
    835 / 869
    836 / 869
    837 / 869
    838 / 869
    839 / 869
    840 / 869
    841 / 869
    842 / 869
    843 / 869
    844 / 869
    845 / 869
    846 / 869
    847 / 869
    848 / 869
    849 / 869
    850 / 869
    851 / 869
    852 / 869
    853 / 869
    854 / 869
    855 / 869
    856 / 869
    857 / 869
    858 / 869
    859 / 869
    860 / 869
    861 / 869
    862 / 869
    863 / 869
    864 / 869
    865 / 869
    866 / 869
    867 / 869
    868 / 869


key: url
string_value:
https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-3-train-qa.ipynb


key: _node_content
string_value:
{"id_": "547ea539-fe25-4394-8682-ab27e7dd4e0f", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-3-train-qa.ipynb", "title": "3. Train a fine-tuning model specialized for Q&A", "tokens": 3706, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "d678dd15-f627-407c-881b-3f384b1d9028", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-3-train-qa.ipynb", "title": "3. Train a fine-tuning model specialized for Q&A", "tokens": 3706, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "f254b8aa71e3e1906f07ce37ffb70b3c250910fc2ccc3f31ca144d7e478357e4", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "a2ee2568-77fc-4251-ab2d-0c88b1a38072", "node_type": "1", "metadata": {}, "hash": "449b3241257ca1cb72749d8fed1cfd30ec0751c3a75e96bf390c9bc867ecbb9d", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 0, "end_char_idx": 2466, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
<span style="color:orange; font-weight:bold">Note: To answer questions based on text documents, we recommend the procedure in <a href="https://github.com/openai/openai-cookbook/blob/main/examples/Question_answering_using_embeddings.ipynb">Question Answering using Embeddings</a>. Some of the code below may rely on <a href="https://github.com/openai/openai-cookbook/tree/main/transition_guides_for_deprecated_API_endpoints">deprecated API endpoints</a>.</span>

# 3. Train a fine-tuning model specialized for Q&A
This notebook will utilize the dataset of context, question and answer pairs to additionally create adversarial questions and context pairs, where the question was not generated on that context. In those cases the model will be prompted to answer "No sufficient context for answering the question". We will also train a discriminator model, which predicts whether the question can be answered based on the context or not.

We will add hard adversarial examples as well, which will be based either on semantically similar sections, or neighbouring sections, originating from the same article.


```python
import openai
import pandas as pd
df = pd.read_csv('olympics-data/olympics_qa.csv')
olympics_search_fileid = "file-c3shd8wqF3vSCKaukW4Jr1TT"
df.head()
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>title</th>
      <th>heading</th>
      <th>content</th>
      <th>tokens</th>
      <th>context</th>
      <th>questions</th>
      <th>answers</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>2020 Summer Olympics</td>
      <td>Summary</td>
      <td>The 2020 Summer Olympics (Japanese: 2020年夏季オリン...</td>
      <td>713</td>
      <td>2020 Summer Olympics\nSummary\n\nThe 2020 Summ...</td>
      <td>1. What is the 2020 Summer Olympics?\n2. When ...</td>
      <td>1. The 2020 Summer Olympics is an internationa...</td>
    </tr>
    <tr>
      <th>1</th>
      <td>2020 Summer Olympics</td>
      <td>Host city selection</td>
      <td>The International Olympic Committee (IOC) vote...</td>
      <td>126</td>
      <td>2020 Summer Olympics\nHost city selection\n\nT...</td>
      <td>1. \n2. \n3. \n4.</td>
      <td>1.


key: _node_content
string_value:
{"id_": "a2ee2568-77fc-4251-ab2d-0c88b1a38072", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-3-train-qa.ipynb", "title": "3. Train a fine-tuning model specialized for Q&A", "tokens": 3706, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "d678dd15-f627-407c-881b-3f384b1d9028", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-3-train-qa.ipynb", "title": "3. Train a fine-tuning model specialized for Q&A", "tokens": 3706, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "f254b8aa71e3e1906f07ce37ffb70b3c250910fc2ccc3f31ca144d7e478357e4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "547ea539-fe25-4394-8682-ab27e7dd4e0f", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-3-train-qa.ipynb", "title": "3. Train a fine-tuning model specialized for Q&A", "tokens": 3706, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "d1ce3341ada6351811320c9b3c34779b231b2f0cfed54786fd7368f9dfa9981b", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "7715fc97-fd5e-4002-a9da-1197c4b59295", "node_type": "1", "metadata": {}, "hash": "f12c67abb246a9f846d950c368a933c933fe9360d9809f903f302b6f620653cd", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 1276, "end_char_idx": 3670, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>title</th>
      <th>heading</th>
      <th>content</th>
      <th>tokens</th>
      <th>context</th>
      <th>questions</th>
      <th>answers</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>2020 Summer Olympics</td>
      <td>Summary</td>
      <td>The 2020 Summer Olympics (Japanese: 2020年夏季オリン...</td>
      <td>713</td>
      <td>2020 Summer Olympics\nSummary\n\nThe 2020 Summ...</td>
      <td>1. What is the 2020 Summer Olympics?\n2. When ...</td>
      <td>1. The 2020 Summer Olympics is an internationa...</td>
    </tr>
    <tr>
      <th>1</th>
      <td>2020 Summer Olympics</td>
      <td>Host city selection</td>
      <td>The International Olympic Committee (IOC) vote...</td>
      <td>126</td>
      <td>2020 Summer Olympics\nHost city selection\n\nT...</td>
      <td>1. \n2. \n3. \n4.</td>
      <td>1. What is the International Olympic Committee...</td>
    </tr>
    <tr>
      <th>2</th>
      <td>2020 Summer Olympics</td>
      <td>Impact of the COVID-19 pandemic</td>
      <td>In January 2020, concerns were raised about th...</td>
      <td>369</td>
      <td>2020 Summer Olympics\nImpact of the COVID-19 p...</td>
      <td>1. What was the COVID-19 pandemic?\n2. How did...</td>
      <td>1. The COVID-19 pandemic was a pandemic that o...</td>
    </tr>
    <tr>
      <th>3</th>
      <td>2020 Summer Olympics</td>
      <td>Qualifying event cancellation and postponement</td>
      <td>Concerns about the pandemic began to affect qu...</td>
      <td>298</td>
      <td>2020 Summer Olympics\nQualifying event cancell...</td>
      <td>1. What was the original location of the Asia ...</td>
      <td>1. The original location of the Asia &amp; Oceania...</td>
    </tr>
    <tr>
      <th>4</th>
      <td>2020 Summer Olympics</td>
      <td>Effect on doping tests</td>
      <td>Mandatory doping tests were being severely res...</td>
      <td>163</td>
      <td>2020 Summer Olympics\nEffect on doping tests\n...</td>
      <td>1. What was the COVID-19 pandemic?\n2. What di...</td>
      <td>1.


key: _node_content
string_value:
{"id_": "7715fc97-fd5e-4002-a9da-1197c4b59295", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-3-train-qa.ipynb", "title": "3. Train a fine-tuning model specialized for Q&A", "tokens": 3706, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "d678dd15-f627-407c-881b-3f384b1d9028", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-3-train-qa.ipynb", "title": "3. Train a fine-tuning model specialized for Q&A", "tokens": 3706, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "f254b8aa71e3e1906f07ce37ffb70b3c250910fc2ccc3f31ca144d7e478357e4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "a2ee2568-77fc-4251-ab2d-0c88b1a38072", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-3-train-qa.ipynb", "title": "3. Train a fine-tuning model specialized for Q&A", "tokens": 3706, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "fa18bbfccc43c200190693145fa25609d4b94ae459a4ab8b083903b7298aca91", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "25f7ab39-e8d9-4d63-803e-bc8edcef554a", "node_type": "1", "metadata": {}, "hash": "85546658aff985b4d27e4eee28ab43cccb8c549be21360871bda44c87310aeed", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 2434, "end_char_idx": 5075, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
\n2. \n3. \n4.</td>
      <td>1. What is the International Olympic Committee...</td>
    </tr>
    <tr>
      <th>2</th>
      <td>2020 Summer Olympics</td>
      <td>Impact of the COVID-19 pandemic</td>
      <td>In January 2020, concerns were raised about th...</td>
      <td>369</td>
      <td>2020 Summer Olympics\nImpact of the COVID-19 p...</td>
      <td>1. What was the COVID-19 pandemic?\n2. How did...</td>
      <td>1. The COVID-19 pandemic was a pandemic that o...</td>
    </tr>
    <tr>
      <th>3</th>
      <td>2020 Summer Olympics</td>
      <td>Qualifying event cancellation and postponement</td>
      <td>Concerns about the pandemic began to affect qu...</td>
      <td>298</td>
      <td>2020 Summer Olympics\nQualifying event cancell...</td>
      <td>1. What was the original location of the Asia ...</td>
      <td>1. The original location of the Asia &amp; Oceania...</td>
    </tr>
    <tr>
      <th>4</th>
      <td>2020 Summer Olympics</td>
      <td>Effect on doping tests</td>
      <td>Mandatory doping tests were being severely res...</td>
      <td>163</td>
      <td>2020 Summer Olympics\nEffect on doping tests\n...</td>
      <td>1. What was the COVID-19 pandemic?\n2. What di...</td>
      <td>1. The COVID-19 pandemic was a pandemic that o...</td>
    </tr>
  </tbody>
</table>
</div>



Split the sections into a training and testing set


```python
from sklearn.model_selection import train_test_split
train_df, test_df = train_test_split(df, test_size=0.2, random_state=42)
len(train_df), len(test_df)
```




    (3014, 754)



we check that the separator we intend to use isn't present within the contexts


```python
df.context.str.contains('->').sum()
```




    0



## 3.1 Create the fine-tuning datasets for Q&A and discriminator models
The fine-tuning dataset is created in the following way. For every corresponding question, answer and context pair we create:
- Positive example: correct question, answer, context pair
- Negative examples:
  - random negative example, where the random context is paired with the question 
  - two hard negative examples
    - one originating from the same wikipedia article
    - another, which is most similar to the correct context

This process is noisy, as sometimes the question might be answerable given a different context, but on average we hope this won't affect the performance too much.

We apply the same process of dataset creation for both the discriminator, and the Q&A answering model. We apply the process separately for the training and testing set, to ensure that the examples from the training set don't feature within the test set.


key: _node_content
string_value:
{"id_": "25f7ab39-e8d9-4d63-803e-bc8edcef554a", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-3-train-qa.ipynb", "title": "3. Train a fine-tuning model specialized for Q&A", "tokens": 3706, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "d678dd15-f627-407c-881b-3f384b1d9028", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-3-train-qa.ipynb", "title": "3. Train a fine-tuning model specialized for Q&A", "tokens": 3706, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "f254b8aa71e3e1906f07ce37ffb70b3c250910fc2ccc3f31ca144d7e478357e4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "7715fc97-fd5e-4002-a9da-1197c4b59295", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-3-train-qa.ipynb", "title": "3. Train a fine-tuning model specialized for Q&A", "tokens": 3706, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "d4e009f49cbefb5395ffbcbf880db7fef123375970d873385f046aa9359e2dc2", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "5255a6a8-54d9-4d12-8353-83905e991e98", "node_type": "1", "metadata": {}, "hash": "733bf2b599de79aef180c1639506e25efc36cd90590dc885243e4fe29e061b32", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 3606, "end_char_idx": 6839, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
What was the COVID-19 pandemic?\n2. What di...</td>
      <td>1. The COVID-19 pandemic was a pandemic that o...</td>
    </tr>
  </tbody>
</table>
</div>



Split the sections into a training and testing set


```python
from sklearn.model_selection import train_test_split
train_df, test_df = train_test_split(df, test_size=0.2, random_state=42)
len(train_df), len(test_df)
```




    (3014, 754)



we check that the separator we intend to use isn't present within the contexts


```python
df.context.str.contains('->').sum()
```




    0



## 3.1 Create the fine-tuning datasets for Q&A and discriminator models
The fine-tuning dataset is created in the following way. For every corresponding question, answer and context pair we create:
- Positive example: correct question, answer, context pair
- Negative examples:
  - random negative example, where the random context is paired with the question 
  - two hard negative examples
    - one originating from the same wikipedia article
    - another, which is most similar to the correct context

This process is noisy, as sometimes the question might be answerable given a different context, but on average we hope this won't affect the performance too much.

We apply the same process of dataset creation for both the discriminator, and the Q&A answering model. We apply the process separately for the training and testing set, to ensure that the examples from the training set don't feature within the test set.


```python
import random

def get_random_similar_contexts(question, context, file_id=olympics_search_fileid, search_model='ada', max_rerank=10):
    """
    Find similar contexts to the given context using the search file
    """
    try:
        # TODO: openai.Engine(search_model) is deprecated
        results = openai.Engine(search_model).search(
            search_model=search_model, 
            query=question, 
            max_rerank=max_rerank,
            file=file_id
        )
        candidates = []
        for result in results['data'][:3]:
            if result['text'] == context:
                continue
            candidates.append(result['text'])
        random_candidate = random.choice(candidates)
        return random_candidate
    except Exception as e:
        print(e)
        return ""

def create_fine_tuning_dataset(df, discriminator=False, n_negative=1, add_related=False):
    """
    Create a dataset for fine tuning the OpenAI model; either for a discriminator model, 
    or a model specializing in Q&A, where it says if no relevant context is found.

    Parameters
    ----------
    df: pd.DataFrame
        The dataframe containing the question, answer and context pairs
    discriminator: bool
        Whether to create a dataset for the discriminator
    n_negative: int
        The number of random negative samples to add (using a random context)
    add_related: bool
        Whether to add the related contexts to the correct context. These are hard negative examples

    Returns
    -------
    pd.DataFrame
        The dataframe containing the prompts and completions, ready for fine-tuning
    """
    rows = []
    for i, row in df.iterrows():
        for q, a in zip(("1." + row.questions).split('\n'), ("1."


key: _node_content
string_value:
{"id_": "5255a6a8-54d9-4d12-8353-83905e991e98", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-3-train-qa.ipynb", "title": "3. Train a fine-tuning model specialized for Q&A", "tokens": 3706, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "d678dd15-f627-407c-881b-3f384b1d9028", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-3-train-qa.ipynb", "title": "3. Train a fine-tuning model specialized for Q&A", "tokens": 3706, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "f254b8aa71e3e1906f07ce37ffb70b3c250910fc2ccc3f31ca144d7e478357e4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "25f7ab39-e8d9-4d63-803e-bc8edcef554a", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-3-train-qa.ipynb", "title": "3. Train a fine-tuning model specialized for Q&A", "tokens": 3706, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "f64af391e3d219d48b9c0d4187474c00ad6ac7c4f830468b5896d5fec20d2b0d", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "2377472f-c78d-4b1d-ae74-d24a5a819fd2", "node_type": "1", "metadata": {}, "hash": "7cd47454946ab6139afae71ab201585ccc7d5c8834aa578c5fc36a089c5db99f", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 5078, "end_char_idx": 7275, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
import random

def get_random_similar_contexts(question, context, file_id=olympics_search_fileid, search_model='ada', max_rerank=10):
    """
    Find similar contexts to the given context using the search file
    """
    try:
        # TODO: openai.Engine(search_model) is deprecated
        results = openai.Engine(search_model).search(
            search_model=search_model, 
            query=question, 
            max_rerank=max_rerank,
            file=file_id
        )
        candidates = []
        for result in results['data'][:3]:
            if result['text'] == context:
                continue
            candidates.append(result['text'])
        random_candidate = random.choice(candidates)
        return random_candidate
    except Exception as e:
        print(e)
        return ""

def create_fine_tuning_dataset(df, discriminator=False, n_negative=1, add_related=False):
    """
    Create a dataset for fine tuning the OpenAI model; either for a discriminator model, 
    or a model specializing in Q&A, where it says if no relevant context is found.

    Parameters
    ----------
    df: pd.DataFrame
        The dataframe containing the question, answer and context pairs
    discriminator: bool
        Whether to create a dataset for the discriminator
    n_negative: int
        The number of random negative samples to add (using a random context)
    add_related: bool
        Whether to add the related contexts to the correct context. These are hard negative examples

    Returns
    -------
    pd.DataFrame
        The dataframe containing the prompts and completions, ready for fine-tuning
    """
    rows = []
    for i, row in df.iterrows():
        for q, a in zip(("1." + row.questions).split('\n'), ("1." + row.answers).split('\n')):
            if len(q) >10 and len(a) >10:
                if discriminator:
                    rows.append({"prompt":f"{row.context}\nQuestion: {q[2:].strip()}\n Related:", "completion":f" yes"})
                else:
                    rows.append({"prompt":f"{row.context}\nQuestion: {q[2:].strip()}\nAnswer:", "completion":f" {a[2:].strip()}"})

    for i, row in df.iterrows():
        for q in ("1."


key: _node_content
string_value:
{"id_": "2377472f-c78d-4b1d-ae74-d24a5a819fd2", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-3-train-qa.ipynb", "title": "3. Train a fine-tuning model specialized for Q&A", "tokens": 3706, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "d678dd15-f627-407c-881b-3f384b1d9028", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-3-train-qa.ipynb", "title": "3. Train a fine-tuning model specialized for Q&A", "tokens": 3706, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "f254b8aa71e3e1906f07ce37ffb70b3c250910fc2ccc3f31ca144d7e478357e4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "5255a6a8-54d9-4d12-8353-83905e991e98", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-3-train-qa.ipynb", "title": "3. Train a fine-tuning model specialized for Q&A", "tokens": 3706, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "0e7d6e4cd48b2b65afb3b048a7eb02e82e76c4ce9bcc3de23db8cc7f488f17c5", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "ce6bac88-e4aa-4fa6-bc06-32b0203da502", "node_type": "1", "metadata": {}, "hash": "4952c8c96d01674ece45e094eebc13b338fd2ce070d706a4f9c1675cac1e3b7b", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 6171, "end_char_idx": 9452, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
Parameters
    ----------
    df: pd.DataFrame
        The dataframe containing the question, answer and context pairs
    discriminator: bool
        Whether to create a dataset for the discriminator
    n_negative: int
        The number of random negative samples to add (using a random context)
    add_related: bool
        Whether to add the related contexts to the correct context. These are hard negative examples

    Returns
    -------
    pd.DataFrame
        The dataframe containing the prompts and completions, ready for fine-tuning
    """
    rows = []
    for i, row in df.iterrows():
        for q, a in zip(("1." + row.questions).split('\n'), ("1." + row.answers).split('\n')):
            if len(q) >10 and len(a) >10:
                if discriminator:
                    rows.append({"prompt":f"{row.context}\nQuestion: {q[2:].strip()}\n Related:", "completion":f" yes"})
                else:
                    rows.append({"prompt":f"{row.context}\nQuestion: {q[2:].strip()}\nAnswer:", "completion":f" {a[2:].strip()}"})

    for i, row in df.iterrows():
        for q in ("1." + row.questions).split('\n'):
            if len(q) >10:
                for j in range(n_negative + (2 if add_related else 0)):
                    random_context = ""
                    if j == 0 and add_related:
                        # add the related contexts based on originating from the same wikipedia page
                        subset = df[(df.title == row.title) & (df.context != row.context)]
                        
                        if len(subset) < 1:
                            continue
                        random_context = subset.sample(1).iloc[0].context
                    if j == 1 and add_related:
                        # add the related contexts based on the most similar contexts according to the search
                        random_context = get_random_similar_contexts(q[2:].strip(), row.context, search_model='ada', max_rerank=10)
                    else:
                        while True:
                            # add random context, which isn't the correct context
                            random_context = df.sample(1).iloc[0].context
                            if random_context != row.context:
                                break
                    if discriminator:
                        rows.append({"prompt":f"{random_context}\nQuestion: {q[2:].strip()}\n Related:", "completion":f" no"})
                    else:
                        rows.append({"prompt":f"{random_context}\nQuestion: {q[2:].strip()}\nAnswer:", "completion":f" No appropriate context found to answer the question."})

    return pd.DataFrame(rows) 
```

We apply the same process of dataset creation for both the discriminator, and the Q&A answering model. We apply the process separately for the training and testing set, to ensure that the examples from the training set don't feature within the test set.


```python
for name, is_disc in [('discriminator', True), ('qa', False)]:
    for train_test, dt in [('train', train_df), ('test', test_df)]:
        ft = create_fine_tuning_dataset(dt, discriminator=is_disc, n_negative=1, add_related=True)
        ft.to_json(f'{name}_{train_test}.jsonl', orient='records', lines=True)
```


key: _node_content
string_value:
{"id_": "ce6bac88-e4aa-4fa6-bc06-32b0203da502", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-3-train-qa.ipynb", "title": "3. Train a fine-tuning model specialized for Q&A", "tokens": 3706, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "d678dd15-f627-407c-881b-3f384b1d9028", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-3-train-qa.ipynb", "title": "3. Train a fine-tuning model specialized for Q&A", "tokens": 3706, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "f254b8aa71e3e1906f07ce37ffb70b3c250910fc2ccc3f31ca144d7e478357e4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "2377472f-c78d-4b1d-ae74-d24a5a819fd2", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-3-train-qa.ipynb", "title": "3. Train a fine-tuning model specialized for Q&A", "tokens": 3706, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "2824c393eb81a87f87cd12801cb9e646b8f12657d1ffb3d5e3aa3873af1b7418", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "20964ebd-0866-4604-be6f-4f1a5d8eb4e4", "node_type": "1", "metadata": {}, "hash": "480a487e2559ab1c532fa75f9b253037d81cee8e2b0cdc80cebb6e7fc7199876", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 8842, "end_char_idx": 11388, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
return pd.DataFrame(rows) 
```

We apply the same process of dataset creation for both the discriminator, and the Q&A answering model. We apply the process separately for the training and testing set, to ensure that the examples from the training set don't feature within the test set.


```python
for name, is_disc in [('discriminator', True), ('qa', False)]:
    for train_test, dt in [('train', train_df), ('test', test_df)]:
        ft = create_fine_tuning_dataset(dt, discriminator=is_disc, n_negative=1, add_related=True)
        ft.to_json(f'{name}_{train_test}.jsonl', orient='records', lines=True)
```




    



We formatted the data according to the recommendations from the fine-tuning tool, which is available using
> openai tools fine_tunes.prepare_data -f qa_train.jsonl

We highly recommend that you use this tool, which suggests improvements in your data formatting for fine-tuning.


## 3.2 Submit the datasets for fine-tuning


```python
!openai api fine_tunes.create -t "olympics-data/discriminator_train.jsonl" -v "olympics-data/discriminator_test.jsonl" --batch_size 16  --compute_classification_metrics --classification_positive_class " yes" --model ada
```




    




```python
!openai api fine_tunes.create -t "olympics-data/qa_train.jsonl" -v "olympics-data/qa_test.jsonl" --batch_size 16
```




    



## 3.3 Using the fine-tuned models

We will now use the fine-tuned discriminator and the fine-tuned Q&A model. By requesting logprobs, we can see how certain the discriminator is in a `yes` vs `no` answer.


```python
ft_discriminator = "curie:ft-openai-internal-2021-08-23-23-58-57"
ft_qa = "curie:ft-openai-internal-2021-08-23-17-54-10"

def apply_ft_discriminator(context, question, discriminator_model):
    """
    Apply the fine tuned discriminator to a question, to assess whether it can be answered from the context.
    """
    prompt = f"{context}\nQuestion: {question}\n Related:"
    result = openai.chat.completions.create(model=discriminator_model, prompt=prompt, max_tokens=1, temperature=0, top_p=1, n=1, logprobs=2)
    return result['choices'][0]['logprobs']['top_logprobs']

apply_ft_discriminator('The first human-made object in space was the Soviet Union satellite Sputnik 1 on 4 October 1957.', 
                        'What was the first human-made object in space?', ft_discriminator)
```




    [<OpenAIObject at 0x7fe812e602b0> JSON: {
       " no": -10.819577,
       " yes": -2.045765e-05
     }]



We can see that the model can generalize well to different contexts and questions.


key: _node_content
string_value:
{"id_": "20964ebd-0866-4604-be6f-4f1a5d8eb4e4", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-3-train-qa.ipynb", "title": "3. Train a fine-tuning model specialized for Q&A", "tokens": 3706, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "d678dd15-f627-407c-881b-3f384b1d9028", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-3-train-qa.ipynb", "title": "3. Train a fine-tuning model specialized for Q&A", "tokens": 3706, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "f254b8aa71e3e1906f07ce37ffb70b3c250910fc2ccc3f31ca144d7e478357e4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "ce6bac88-e4aa-4fa6-bc06-32b0203da502", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-3-train-qa.ipynb", "title": "3. Train a fine-tuning model specialized for Q&A", "tokens": 3706, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "bb266521f3e9c76b049097d1880ecc80308124e96d0c5cd75fc2e0b3ee03bda2", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "09034718-6b4e-4a85-825c-1f775d8786e8", "node_type": "1", "metadata": {}, "hash": "27ff64ed005db3e16c69aebcedb676879b47791f883923d5c60482304732d79e", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 10037, "end_char_idx": 12758, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
!openai api fine_tunes.create -t "olympics-data/qa_train.jsonl" -v "olympics-data/qa_test.jsonl" --batch_size 16
```




    



## 3.3 Using the fine-tuned models

We will now use the fine-tuned discriminator and the fine-tuned Q&A model. By requesting logprobs, we can see how certain the discriminator is in a `yes` vs `no` answer.


```python
ft_discriminator = "curie:ft-openai-internal-2021-08-23-23-58-57"
ft_qa = "curie:ft-openai-internal-2021-08-23-17-54-10"

def apply_ft_discriminator(context, question, discriminator_model):
    """
    Apply the fine tuned discriminator to a question, to assess whether it can be answered from the context.
    """
    prompt = f"{context}\nQuestion: {question}\n Related:"
    result = openai.chat.completions.create(model=discriminator_model, prompt=prompt, max_tokens=1, temperature=0, top_p=1, n=1, logprobs=2)
    return result['choices'][0]['logprobs']['top_logprobs']

apply_ft_discriminator('The first human-made object in space was the Soviet Union satellite Sputnik 1 on 4 October 1957.', 
                        'What was the first human-made object in space?', ft_discriminator)
```




    [<OpenAIObject at 0x7fe812e602b0> JSON: {
       " no": -10.819577,
       " yes": -2.045765e-05
     }]



We can see that the model can generalize well to different contexts and questions. 


```python
def apply_ft_qa_answer(context, question, answering_model):
    """
    Apply the fine tuned discriminator to a question
    """
    prompt = f"{context}\nQuestion: {question}\nAnswer:"
    result = openai.chat.completions.create(model=answering_model, prompt=prompt, max_tokens=30, temperature=0, top_p=1, n=1, stop=['.','\n'])
    return result['choices'][0]['text']

apply_ft_qa_answer('The first human-made object in space was the Soviet Union satellite Sputnik 1 on 4 October 1957.', 
                    'What was the first human-made object in space?', ft_qa)

```




    ' The first human-made object in space was the Soviet Union satellite Sputnik 1 on 4 October 1957'



We can see that the model can answer the question, when the context is appropriate.


```python
apply_ft_qa_answer('The first human-made object in space was the Soviet Union satellite Sputnik 1 on 4 October 1957.',
                    'What is impressive about the Soviet Union?', ft_qa)
```




    ' The Soviet Union was the first country to successfully launch a satellite into space'




```python
apply_ft_qa_answer('The first human-made object in space was the Soviet Union satellite Sputnik 1 on 4 October 1957.',
                    'How many cars were produced in the Soviet Union in 1970?', ft_qa)
```




    ' No appropriate context found to answer the question'


key: _node_content
string_value:
{"id_": "09034718-6b4e-4a85-825c-1f775d8786e8", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-3-train-qa.ipynb", "title": "3. Train a fine-tuning model specialized for Q&A", "tokens": 3706, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "d678dd15-f627-407c-881b-3f384b1d9028", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-3-train-qa.ipynb", "title": "3. Train a fine-tuning model specialized for Q&A", "tokens": 3706, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "f254b8aa71e3e1906f07ce37ffb70b3c250910fc2ccc3f31ca144d7e478357e4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "20964ebd-0866-4604-be6f-4f1a5d8eb4e4", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-3-train-qa.ipynb", "title": "3. Train a fine-tuning model specialized for Q&A", "tokens": 3706, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "86cba393c0e54b27fcf9e8d6542829db024d4038284898bd3ba283621ca2c53e", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "99a1dc68-13ad-4fa5-8f2c-10aa4ec8b2e7", "node_type": "1", "metadata": {}, "hash": "dd0e2870aa9a41a060aa057792de758d51ceea743c78493f1bdf148ccfa7b3ba", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 11198, "end_char_idx": 14490, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
[<OpenAIObject at 0x7fe812e602b0> JSON: {
       " no": -10.819577,
       " yes": -2.045765e-05
     }]



We can see that the model can generalize well to different contexts and questions. 


```python
def apply_ft_qa_answer(context, question, answering_model):
    """
    Apply the fine tuned discriminator to a question
    """
    prompt = f"{context}\nQuestion: {question}\nAnswer:"
    result = openai.chat.completions.create(model=answering_model, prompt=prompt, max_tokens=30, temperature=0, top_p=1, n=1, stop=['.','\n'])
    return result['choices'][0]['text']

apply_ft_qa_answer('The first human-made object in space was the Soviet Union satellite Sputnik 1 on 4 October 1957.', 
                    'What was the first human-made object in space?', ft_qa)

```




    ' The first human-made object in space was the Soviet Union satellite Sputnik 1 on 4 October 1957'



We can see that the model can answer the question, when the context is appropriate.


```python
apply_ft_qa_answer('The first human-made object in space was the Soviet Union satellite Sputnik 1 on 4 October 1957.',
                    'What is impressive about the Soviet Union?', ft_qa)
```




    ' The Soviet Union was the first country to successfully launch a satellite into space'




```python
apply_ft_qa_answer('The first human-made object in space was the Soviet Union satellite Sputnik 1 on 4 October 1957.',
                    'How many cars were produced in the Soviet Union in 1970?', ft_qa)
```




    ' No appropriate context found to answer the question'



We can see that the model knows when to answer the question, and when to say that insufficient context is present to answer the question.

We can also combine a discriminator and a base model, or a fine-tuned Q&A model. Discriminator can essentially serve as a decision whether the question can be answered given the context or not.


```python
def answer_question_conditionally(answering_model, discriminator_model, context, question, discriminator_logprob_yes_modifier=0):
    logprobs = apply_ft_discriminator(context, question, discriminator_model)
    yes_logprob = logprobs[' yes'] if ' yes' in logprobs else -100
    no_logprob = logprobs[' no'] if ' no' in logprobs else -100
    if yes_logprob + discriminator_logprob_yes_modifier < no_logprob:
        return " No appropriate context found to answer the question based on the discriminator."
    return apply_ft_qa_answer(context, question, answering_model)
answer_question_conditionally(ft_qa, ft_discriminator, 
                                "Crowdless games are a rare although not unheard-of occurrence in sports. \
                                 When they do occur, it is usually the result of events beyond the control \
                                 of the teams or fans, such as weather-related concerns, public health concerns, \
                                 or wider civil disturbances unrelated to the game. For instance, \
                                 the COVID-19 pandemic caused many sports leagues around the world \
                                 to be played behind closed doors.",
                                "Could weather cause a sport event to have no crowd?")
```




    ' Weather could cause a sport event to have no crowd'


key: _node_content
string_value:
{"id_": "99a1dc68-13ad-4fa5-8f2c-10aa4ec8b2e7", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-3-train-qa.ipynb", "title": "3. Train a fine-tuning model specialized for Q&A", "tokens": 3706, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "d678dd15-f627-407c-881b-3f384b1d9028", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-3-train-qa.ipynb", "title": "3. Train a fine-tuning model specialized for Q&A", "tokens": 3706, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "f254b8aa71e3e1906f07ce37ffb70b3c250910fc2ccc3f31ca144d7e478357e4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "09034718-6b4e-4a85-825c-1f775d8786e8", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-3-train-qa.ipynb", "title": "3. Train a fine-tuning model specialized for Q&A", "tokens": 3706, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "c21064ea8f2663339fb675bcc43976902e954243f7556353a11c1e4c35487814", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 12704, "end_char_idx": 15566, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
' No appropriate context found to answer the question'



We can see that the model knows when to answer the question, and when to say that insufficient context is present to answer the question.

We can also combine a discriminator and a base model, or a fine-tuned Q&A model. Discriminator can essentially serve as a decision whether the question can be answered given the context or not.


```python
def answer_question_conditionally(answering_model, discriminator_model, context, question, discriminator_logprob_yes_modifier=0):
    logprobs = apply_ft_discriminator(context, question, discriminator_model)
    yes_logprob = logprobs[' yes'] if ' yes' in logprobs else -100
    no_logprob = logprobs[' no'] if ' no' in logprobs else -100
    if yes_logprob + discriminator_logprob_yes_modifier < no_logprob:
        return " No appropriate context found to answer the question based on the discriminator."
    return apply_ft_qa_answer(context, question, answering_model)
answer_question_conditionally(ft_qa, ft_discriminator, 
                                "Crowdless games are a rare although not unheard-of occurrence in sports. \
                                 When they do occur, it is usually the result of events beyond the control \
                                 of the teams or fans, such as weather-related concerns, public health concerns, \
                                 or wider civil disturbances unrelated to the game. For instance, \
                                 the COVID-19 pandemic caused many sports leagues around the world \
                                 to be played behind closed doors.",
                                "Could weather cause a sport event to have no crowd?")
```




    ' Weather could cause a sport event to have no crowd'



The above function illustrates how to potentially combine a discriminator and a fine-tuned Q&A model. This gives a more fine-grained control over how certain we want the model to be before it answers the question.

We'll now take a look on how answers endpoint works - combining search to retrieve the relevant context from a knowledge base, and then using the fine-tuned Q&A model to answer the question.

## 3.4 Answering the question based on a knowledge base
Finally we can use a logic similar to the [/answers](https://beta.openai.com/docs/api-reference/answers) endpoint, where we first search for the relevant context, and then ask a Q&A model to answer the question given that context. If you'd like to see the implementation details, check out the [`answers_with_ft.py`](answers_with_ft.py) file.


```python
from answers_with_ft import answer_question
answer_question(olympics_search_fileid, ft_qa, "Which country won the Women's football tournament at the 2020 Olympic games?")
```




    " Canada won the Women's football tournament at the 2020 Olympic games"


key: url
string_value:
https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-1-collect-data.ipynb


key: _node_content
string_value:
{"id_": "5b921ab4-1eed-4845-a299-8c62cfb6a722", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-1-collect-data.ipynb", "title": "1. Collect Wikipedia data about Olympic Games 2020", "tokens": 2914, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "30bf688b-1c8a-40f4-9ce8-8413a0a6a2d7", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-1-collect-data.ipynb", "title": "1. Collect Wikipedia data about Olympic Games 2020", "tokens": 2914, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "7ea9ff6c1202762e7155acb3717eb1a0e74993130131a336b46f05cfdab5f1d4", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "926a88a4-0470-4a11-972d-7195159fe331", "node_type": "1", "metadata": {}, "hash": "abf35b6ef3cb253f1c316c5be58c6c77bc7ed80f0ffa7eec342ad985636ebf89", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 0, "end_char_idx": 2536, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
<span style="color:orange; font-weight:bold">Note: To answer questions based on text documents, we recommend the procedure in <a href="https://github.com/openai/openai-cookbook/blob/main/examples/Question_answering_using_embeddings.ipynb">Question Answering using Embeddings</a>. Some of the code below may rely on <a href="https://github.com/openai/openai-cookbook/tree/main/transition_guides_for_deprecated_API_endpoints">deprecated API endpoints</a>.</span>

# 1. Collect Wikipedia data about Olympic Games 2020

The idea of this project is to create a question answering model, based on a few paragraphs of provided text. Base GPT-3 models do a good job at answering questions when the answer is contained within the paragraph, however if the answer isn't contained, the base models tend to try their best to answer anyway, often leading to confabulated answers. 

To create a model which answers questions only if there is sufficient context for doing so, we first create a dataset of questions and answers based on paragraphs of text. In order to train the model to answer only when the answer is present, we also add adversarial examples, where the question doesn't match the context. In those cases, we ask the model to output "No sufficient context for answering the question". 

We will perform this task in three notebooks:
1. The first (this) notebook focuses on collecting recent data, which GPT-3 didn't see during its pre-training. We picked the topic of Olympic Games 2020 (which actually took place in the summer of 2021), and downloaded 713 unique pages. We organized the dataset by individual sections, which will serve as context for asking and answering the questions.
2. The [second notebook](olympics-2-create-qa.ipynb) will utilize Davinci-instruct to ask a few questions based on a Wikipedia section, as well as answer those questions, based on that section.
3. The [third notebook](olympics-3-train-qa.ipynb) will utilize the dataset of context, question and answer pairs to additionally create adversarial questions and context pairs, where the question was not generated on that context. In those cases the model will be prompted to answer "No sufficient context for answering the question". We will also train a discriminator model, which predicts whether the question can be answered based on the context or not.

## 1.1 Data extraction using the wikipedia API
Extracting the data will take about half an hour, and processing will likely take about as much.


```python
import pandas as pd
import wikipedia


key: _node_content
string_value:
{"id_": "926a88a4-0470-4a11-972d-7195159fe331", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-1-collect-data.ipynb", "title": "1. Collect Wikipedia data about Olympic Games 2020", "tokens": 2914, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "30bf688b-1c8a-40f4-9ce8-8413a0a6a2d7", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-1-collect-data.ipynb", "title": "1. Collect Wikipedia data about Olympic Games 2020", "tokens": 2914, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "7ea9ff6c1202762e7155acb3717eb1a0e74993130131a336b46f05cfdab5f1d4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "5b921ab4-1eed-4845-a299-8c62cfb6a722", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-1-collect-data.ipynb", "title": "1. Collect Wikipedia data about Olympic Games 2020", "tokens": 2914, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "348abafd00891507cdcf2bc20c2701e3b9f796f9bb9538b7af4aced95fccc920", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "dc514b42-8ce8-4f5a-bfcf-6751c7afd121", "node_type": "1", "metadata": {}, "hash": "8a3a087c022e785b48b6f357cb462adc284d1c5d1116f1be227075e044f7641a", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 2490, "end_char_idx": 5382, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
import pandas as pd
import wikipedia


def filter_olympic_2020_titles(titles):
    """
    Get the titles which are related to Olympic games hosted in 2020, given a list of titles
    """
    titles = [title for title in titles if '2020' in title and 'olympi' in title.lower()]
    
    return titles

def get_wiki_page(title):
    """
    Get the wikipedia page given a title
    """
    try:
        return wikipedia.page(title)
    except wikipedia.exceptions.DisambiguationError as e:
        return wikipedia.page(e.options[0])
    except wikipedia.exceptions.PageError as e:
        return None

def recursively_find_all_pages(titles, titles_so_far=set()):
    """
    Recursively find all the pages that are linked to the Wikipedia titles in the list
    """
    all_pages = []
    
    titles = list(set(titles) - titles_so_far)
    titles = filter_olympic_2020_titles(titles)
    titles_so_far.update(titles)
    for title in titles:
        page = get_wiki_page(title)
        if page is None:
            continue
        all_pages.append(page)

        new_pages = recursively_find_all_pages(page.links, titles_so_far)
        for pg in new_pages:
            if pg.title not in [p.title for p in all_pages]:
                all_pages.append(pg)
        titles_so_far.update(page.links)
    return all_pages


pages = recursively_find_all_pages(["2020 Summer Olympics"])
len(pages)
```




    909



## 1.2 Filtering the Wikipedia pages and splitting them into sections by headings
We remove sections unlikely to contain textual information, and ensure that each section is not longer than the token limit


```python

import re
from typing import Set
from transformers import GPT2TokenizerFast

import numpy as np
from nltk.tokenize import sent_tokenize

tokenizer = GPT2TokenizerFast.from_pretrained("gpt2")

def count_tokens(text: str) -> int:
    """count the number of tokens in a string"""
    return len(tokenizer.encode(text))

def reduce_long(
    long_text: str, long_text_tokens: bool = False, max_len: int = 590
) -> str:
    """
    Reduce a long text to a maximum of `max_len` tokens by potentially cutting at a sentence end
    """
    if not long_text_tokens:
        long_text_tokens = count_tokens(long_text)
    if long_text_tokens > max_len:
        sentences = sent_tokenize(long_text.replace("\n", " "))
        ntokens = 0
        for i, sentence in enumerate(sentences):
            ntokens += 1 + count_tokens(sentence)
            if ntokens > max_len:
                return ". ".join(sentences[:i]) + "."

    return long_text

discard_categories = ['See also', 'References', 'External links', 'Further reading', "Footnotes",
    "Bibliography", "Sources", "Citations", "Literature", "Footnotes", "Notes and references",
    "Photo gallery", "Works cited", "Photos", "Gallery", "Notes", "References and sources",
    "References and notes",]


key: _node_content
string_value:
{"id_": "dc514b42-8ce8-4f5a-bfcf-6751c7afd121", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-1-collect-data.ipynb", "title": "1. Collect Wikipedia data about Olympic Games 2020", "tokens": 2914, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "30bf688b-1c8a-40f4-9ce8-8413a0a6a2d7", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-1-collect-data.ipynb", "title": "1. Collect Wikipedia data about Olympic Games 2020", "tokens": 2914, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "7ea9ff6c1202762e7155acb3717eb1a0e74993130131a336b46f05cfdab5f1d4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "926a88a4-0470-4a11-972d-7195159fe331", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-1-collect-data.ipynb", "title": "1. Collect Wikipedia data about Olympic Games 2020", "tokens": 2914, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "48394ece7edf83343860ae3d4d0418240d4164a253ba6c2cc96b70fb27425bf7", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "a6bf6803-4365-42cd-aa56-9ea0c7e0beea", "node_type": "1", "metadata": {}, "hash": "56fa6a030c3d7a503e8064cea6a1b7c497fb0554dca98016efbe46ad3037f503", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 3822, "end_char_idx": 7820, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
pages = recursively_find_all_pages(["2020 Summer Olympics"])
len(pages)
```




    909



## 1.2 Filtering the Wikipedia pages and splitting them into sections by headings
We remove sections unlikely to contain textual information, and ensure that each section is not longer than the token limit


```python

import re
from typing import Set
from transformers import GPT2TokenizerFast

import numpy as np
from nltk.tokenize import sent_tokenize

tokenizer = GPT2TokenizerFast.from_pretrained("gpt2")

def count_tokens(text: str) -> int:
    """count the number of tokens in a string"""
    return len(tokenizer.encode(text))

def reduce_long(
    long_text: str, long_text_tokens: bool = False, max_len: int = 590
) -> str:
    """
    Reduce a long text to a maximum of `max_len` tokens by potentially cutting at a sentence end
    """
    if not long_text_tokens:
        long_text_tokens = count_tokens(long_text)
    if long_text_tokens > max_len:
        sentences = sent_tokenize(long_text.replace("\n", " "))
        ntokens = 0
        for i, sentence in enumerate(sentences):
            ntokens += 1 + count_tokens(sentence)
            if ntokens > max_len:
                return ". ".join(sentences[:i]) + "."

    return long_text

discard_categories = ['See also', 'References', 'External links', 'Further reading', "Footnotes",
    "Bibliography", "Sources", "Citations", "Literature", "Footnotes", "Notes and references",
    "Photo gallery", "Works cited", "Photos", "Gallery", "Notes", "References and sources",
    "References and notes",]


def extract_sections(
    wiki_text: str,
    title: str,
    max_len: int = 1500,
    discard_categories: Set[str] = discard_categories,
) -> str:
    """
    Extract the sections of a Wikipedia page, discarding the references and other low information sections
    """
    if len(wiki_text) == 0:
        return []

    # find all headings and the corresponding contents
    headings = re.findall("==+ .* ==+", wiki_text)
    for heading in headings:
        wiki_text = wiki_text.replace(heading, "==+ !! ==+")
    contents = wiki_text.split("==+ !! ==+")
    contents = [c.strip() for c in contents]
    assert len(headings) == len(contents) - 1

    cont = contents.pop(0).strip()
    outputs = [(title, "Summary", cont, count_tokens(cont)+4)]
    
    # discard the discard categories, accounting for a tree structure
    max_level = 100
    keep_group_level = max_level
    remove_group_level = max_level
    nheadings, ncontents = [], []
    for heading, content in zip(headings, contents):
        plain_heading = " ".join(heading.split(" ")[1:-1])
        num_equals = len(heading.split(" ")[0])
        if num_equals <= keep_group_level:
            keep_group_level = max_level

        if num_equals > remove_group_level:
            if (
                num_equals <= keep_group_level
            ):
                continue
        keep_group_level = max_level
        if plain_heading in discard_categories:
            remove_group_level = num_equals
            keep_group_level = max_level
            continue
        nheadings.append(heading.replace("=", "").strip())
        ncontents.append(content)
        remove_group_level = max_level

    # count the tokens of each section
    ncontent_ntokens = [
        count_tokens(c)
        + 3
        + count_tokens(" ".join(h.split(" ")[1:-1]))
        - (1 if len(c) == 0 else 0)
        for h, c in zip(nheadings, ncontents)
    ]

    # Create a tuple of (title, section_name, content, number of tokens)
    outputs += [(title, h, c, t) if t<max_len 
                else (title, h, reduce_long(c, max_len), count_tokens(reduce_long(c,max_len))) 
                    for h, c, t in zip(nheadings, ncontents, ncontent_ntokens)]
    
    return outputs

# Example page being processed into sections
bermuda_page = get_wiki_page('Bermuda at the 2020 Summer Olympics')
ber = extract_sections(bermuda_page.content, bermuda_page.title)

# Example section
ber[-1]

```


key: _node_content
string_value:
{"id_": "a6bf6803-4365-42cd-aa56-9ea0c7e0beea", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-1-collect-data.ipynb", "title": "1. Collect Wikipedia data about Olympic Games 2020", "tokens": 2914, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "30bf688b-1c8a-40f4-9ce8-8413a0a6a2d7", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-1-collect-data.ipynb", "title": "1. Collect Wikipedia data about Olympic Games 2020", "tokens": 2914, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "7ea9ff6c1202762e7155acb3717eb1a0e74993130131a336b46f05cfdab5f1d4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "dc514b42-8ce8-4f5a-bfcf-6751c7afd121", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-1-collect-data.ipynb", "title": "1. Collect Wikipedia data about Olympic Games 2020", "tokens": 2914, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "4d1726e73d28316c3f7bc411ddfac2bd6a705d6eaafcc1b3b525a053f587a81f", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "321c2a15-b8ad-4113-8300-e7d921a97d94", "node_type": "1", "metadata": {}, "hash": "508edbe293cf0bbc794a55ce9d45113eb806f2234d4c3bc76f9449cff669115e", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 7829, "end_char_idx": 10513, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
('Bermuda at the 2020 Summer Olympics',
     'Equestrian',
     "Bermuda entered one dressage rider into the Olympic competition by finishing in the top four, outside the group selection, of the individual FEI Olympic Rankings for Groups D and E (North, Central, and South America), marking the country's recurrence to the sport after an eight-year absence. The quota was later withdrawn, following an injury of Annabelle Collins' main horse Joyero and a failure to obtain minimum eligibility requirements (MER) aboard a new horse Chuppy Checker.",
     104)



### 1.2.1 We create a dataset and filter out any sections with fewer than 40 tokens, as those are unlikely to contain enough context to ask a good question.


```python
res = []
for page in pages:
    res += extract_sections(page.content, page.title)
df = pd.DataFrame(res, columns=["title", "heading", "content", "tokens"])
df = df[df.tokens>40]
df = df.drop_duplicates(['title','heading'])
df = df.reset_index().drop('index',axis=1) # reset index
df.head()
```

    Token indices sequence length is longer than the specified maximum sequence length for this model (1060 > 1024). Running this sequence through the model will result in indexing errors





<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>title</th>
      <th>heading</th>
      <th>content</th>
      <th>tokens</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>2020 Summer Olympics</td>
      <td>Summary</td>
      <td>The 2020 Summer Olympics (Japanese: 2020年夏季オリン...</td>
      <td>713</td>
    </tr>
    <tr>
      <th>1</th>
      <td>2020 Summer Olympics</td>
      <td>Host city selection</td>
      <td>The International Olympic Committee (IOC) vote...</td>
      <td>126</td>
    </tr>
    <tr>
      <th>2</th>
      <td>2020 Summer Olympics</td>
      <td>Impact of the COVID-19 pandemic</td>
      <td>In January 2020, concerns were raised about th...</td>
      <td>369</td>
    </tr>
    <tr>
      <th>3</th>
      <td>2020 Summer Olympics</td>
      <td>Qualifying event cancellation and postponement</td>
      <td>Concerns about the pandemic began to affect qu...</td>
      <td>298</td>
    </tr>
    <tr>
      <th>4</th>
      <td>2020 Summer Olympics</td>
      <td>Effect on doping tests</td>
      <td>Mandatory doping tests were being severely res...</td>
      <td>163</td>
    </tr>
  </tbody>
</table>
</div>


key: _node_content
string_value:
{"id_": "321c2a15-b8ad-4113-8300-e7d921a97d94", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-1-collect-data.ipynb", "title": "1. Collect Wikipedia data about Olympic Games 2020", "tokens": 2914, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "30bf688b-1c8a-40f4-9ce8-8413a0a6a2d7", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-1-collect-data.ipynb", "title": "1. Collect Wikipedia data about Olympic Games 2020", "tokens": 2914, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "7ea9ff6c1202762e7155acb3717eb1a0e74993130131a336b46f05cfdab5f1d4", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "a6bf6803-4365-42cd-aa56-9ea0c7e0beea", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-1-collect-data.ipynb", "title": "1. Collect Wikipedia data about Olympic Games 2020", "tokens": 2914, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "45e8d34618c0b72b77bf6765466aae3b4a5a0f33b79d941f9f907544b65107d1", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 10517, "end_char_idx": 12065, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
### Save the section dataset
We will save the section dataset, for the [next notebook](olympics-2-create-qa.ipynb)


```python
df.to_csv('olympics-data/olympics_sections.csv', index=False)
```

## 1.3 (Optional) Exploring the data 


```python
df.title.value_counts().head()
```




    Concerns and controversies at the 2020 Summer Olympics    51
    United States at the 2020 Summer Olympics                 46
    Great Britain at the 2020 Summer Olympics                 42
    Canada at the 2020 Summer Olympics                        39
    Olympic Games                                             39
    Name: title, dtype: int64



There appear to be winter and summer Olympics 2020. We chose to leave a little ambiguity and noise in the dataset, even though we were interested only in the Summer Olympics 2020.


```python
df.title.str.contains('Summer').value_counts()
```




    True     3567
    False     305
    Name: title, dtype: int64




```python
df.title.str.contains('Winter').value_counts()
```




    False    3774
    True       98
    Name: title, dtype: int64




```python
import pandas as pd
from matplotlib import pyplot as plt

df = pd.read_csv('olympics-data/olympics_sections.csv')
df[['tokens']].hist()
# add axis descriptions and title
plt.xlabel('Number of tokens')
plt.ylabel('Number of Wikipedia sections')
plt.title('Distribution of number of tokens in Wikipedia sections')
plt.show()
```


    
![png](output_15_0.png)
    


We can see that the majority of section are fairly short (less than 500 tokens).


key: url
string_value:
https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-2-create-qa.ipynb


key: _node_content
string_value:
{"id_": "e1dae696-0fc3-4f42-85e2-eea0233f5d7b", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-2-create-qa.ipynb", "title": "2. Creating a synthetic Q&A dataset", "tokens": 5416, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "da500b3c-9093-4acb-af8a-9bdf5a59d788", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-2-create-qa.ipynb", "title": "2. Creating a synthetic Q&A dataset", "tokens": 5416, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "1dace799e477d710c8a5f5e07dd9eb059b8ce0b711356d9bc729f127541dac5b", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "6e99e081-23cb-4ee8-8fa0-a16c8fafeb8a", "node_type": "1", "metadata": {}, "hash": "f898e720fef8a88b50fc010a9994ac9429bfb03f2224422bc1dda50fe6814fe5", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 0, "end_char_idx": 1397, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
<span style="color:orange; font-weight:bold">Note: To answer questions based on text documents, we recommend the procedure in <a href="https://github.com/openai/openai-cookbook/blob/main/examples/Question_answering_using_embeddings.ipynb">Question Answering using Embeddings</a>. Some of the code below may rely on <a href="https://github.com/openai/openai-cookbook/tree/main/transition_guides_for_deprecated_API_endpoints">deprecated API endpoints</a>.</span>

# 2. Creating a synthetic Q&A dataset
We use [`davinci-instruct-beta-v3`](https://beta.openai.com/docs/engines/instruct-series-beta), a model specialized in following instructions, to create questions based on the given context. Then we also use [`davinci-instruct-beta-v3`](https://beta.openai.com/docs/engines/instruct-series-beta) to answer those questions, given the same context. 

This is expensive, and will also take a long time, as we call the davinci engine for each section. You can simply download the final dataset instead.

We're using the dataset created using the [previous notebook](olympics-1-collect-data.ipynb)

## 2.1 Read in the data, and create a context
Create a context by concatenating the title, the heading and the content of that section


```python
import pandas as pd
df = pd.read_csv('olympics-data/olympics_sections.csv')
df['context'] = df.title + "\n" + df.heading + "\n\n" + df.content
df.head()
```


key: _node_content
string_value:
{"id_": "6e99e081-23cb-4ee8-8fa0-a16c8fafeb8a", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-2-create-qa.ipynb", "title": "2. Creating a synthetic Q&A dataset", "tokens": 5416, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "da500b3c-9093-4acb-af8a-9bdf5a59d788", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-2-create-qa.ipynb", "title": "2. Creating a synthetic Q&A dataset", "tokens": 5416, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "1dace799e477d710c8a5f5e07dd9eb059b8ce0b711356d9bc729f127541dac5b", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "e1dae696-0fc3-4f42-85e2-eea0233f5d7b", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-2-create-qa.ipynb", "title": "2. Creating a synthetic Q&A dataset", "tokens": 5416, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "6a735d22633015e25d9e78263ede3d5beb57dc8604e491531b877509f2125c6b", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "e43f6807-c153-4603-8454-d4d815c3b3d6", "node_type": "1", "metadata": {}, "hash": "bd95ab2fbdc31bed7ed03497313e3fea2bd8f3253b0bb22457cffb08619c6ff3", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 0, "end_char_idx": 3215, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
<span style="color:orange; font-weight:bold">Note: To answer questions based on text documents, we recommend the procedure in <a href="https://github.com/openai/openai-cookbook/blob/main/examples/Question_answering_using_embeddings.ipynb">Question Answering using Embeddings</a>. Some of the code below may rely on <a href="https://github.com/openai/openai-cookbook/tree/main/transition_guides_for_deprecated_API_endpoints">deprecated API endpoints</a>.</span>

# 2. Creating a synthetic Q&A dataset
We use [`davinci-instruct-beta-v3`](https://beta.openai.com/docs/engines/instruct-series-beta), a model specialized in following instructions, to create questions based on the given context. Then we also use [`davinci-instruct-beta-v3`](https://beta.openai.com/docs/engines/instruct-series-beta) to answer those questions, given the same context. 

This is expensive, and will also take a long time, as we call the davinci engine for each section. You can simply download the final dataset instead.

We're using the dataset created using the [previous notebook](olympics-1-collect-data.ipynb)

## 2.1 Read in the data, and create a context
Create a context by concatenating the title, the heading and the content of that section


```python
import pandas as pd
df = pd.read_csv('olympics-data/olympics_sections.csv')
df['context'] = df.title + "\n" + df.heading + "\n\n" + df.content
df.head()
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>title</th>
      <th>heading</th>
      <th>content</th>
      <th>tokens</th>
      <th>context</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>2020 Summer Olympics</td>
      <td>Summary</td>
      <td>The 2020 Summer Olympics (Japanese: 2020年夏季オリン...</td>
      <td>713</td>
      <td>2020 Summer Olympics\nSummary\n\nThe 2020 Summ...</td>
    </tr>
    <tr>
      <th>1</th>
      <td>2020 Summer Olympics</td>
      <td>Host city selection</td>
      <td>The International Olympic Committee (IOC) vote...</td>
      <td>126</td>
      <td>2020 Summer Olympics\nHost city selection\n\nT...</td>
    </tr>
    <tr>
      <th>2</th>
      <td>2020 Summer Olympics</td>
      <td>Impact of the COVID-19 pandemic</td>
      <td>In January 2020, concerns were raised about th...</td>
      <td>369</td>
      <td>2020 Summer Olympics\nImpact of the COVID-19 p...</td>
    </tr>
    <tr>
      <th>3</th>
      <td>2020 Summer Olympics</td>
      <td>Qualifying event cancellation and postponement</td>
      <td>Concerns about the pandemic began to affect qu...</td>
      <td>298</td>
      <td>2020 Summer Olympics\nQualifying event cancell...</td>
    </tr>
    <tr>
      <th>4</th>
      <td>2020 Summer Olympics</td>
      <td>Effect on doping tests</td>
      <td>Mandatory doping tests were being severely res...</td>
      <td>163</td>
      <td>2020 Summer Olympics\nEffect on doping tests\n...</td>
    </tr>
  </tbody>
</table>
</div>


key: _node_content
string_value:
{"id_": "e43f6807-c153-4603-8454-d4d815c3b3d6", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-2-create-qa.ipynb", "title": "2. Creating a synthetic Q&A dataset", "tokens": 5416, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "da500b3c-9093-4acb-af8a-9bdf5a59d788", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-2-create-qa.ipynb", "title": "2. Creating a synthetic Q&A dataset", "tokens": 5416, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "1dace799e477d710c8a5f5e07dd9eb059b8ce0b711356d9bc729f127541dac5b", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "6e99e081-23cb-4ee8-8fa0-a16c8fafeb8a", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-2-create-qa.ipynb", "title": "2. Creating a synthetic Q&A dataset", "tokens": 5416, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "d60d944bc36e857d7f2b93a57e284652d26f6d0958f33c2cb69352f82097de9d", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "1e9a90c1-bf3a-455c-bb90-58193dd90ce7", "node_type": "1", "metadata": {}, "hash": "37ff249c3e5c99e7e601e25d040626d3ec1872124757ebe04272156e33ff6db4", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 3219, "end_char_idx": 5081, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
## 2.2 Create questions based on the context
Use davinci-instruct to generate a number of plausible questions relating to the Wikipedia section contents.

Note: We have used temperature=0, but it may be beneficial to experiment with a higher temperature to get a higher diversity of questions.

<span style="color:orange; font-weight:bold">WARNING: This step will last a long time, and consume a lot of tokens, as it calls davinci-instruct for every section to generate a number of questions.</span>


```python
from openai import OpenAI

client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY", "<your OpenAI API key if not set as env var>"))

def get_questions(context):
    try:
        response = client.chat.completions.create(model="davinci-instruct-beta-v3",
        prompt=f"Write questions based on the text below\n\nText: {context}\n\nQuestions:\n1.",
        temperature=0,
        max_tokens=257,
        top_p=1,
        frequency_penalty=0,
        presence_penalty=0,
        stop=["\n\n"])
        return response.choices[0].text
    except:
        return ""


df['questions']= df.context.apply(get_questions)
df['questions'] = "1." + df.questions
print(df[['questions']].values[0][0])
```

    1. What is the 2020 Summer Olympics?
    2. When did the 2020 Summer Olympics take place?
    3. Who won the most medals at the 2020 Summer Olympics?
    4. Who won the most gold medals at the 2020 Summer Olympics?
    5. Who won the most medals at the 2020 Summer Olympics?


The prompt is designed to generate a number of questions. Example questions above were generated based on the summary section of the 2020 Summer Olympics page.

We can observe that the questions 3 and 5 above repeat. Sometimes the generated questions could be ambiguous without the context. We will show that even despite these limitations we can create a successful model.


key: _node_content
string_value:
{"id_": "1e9a90c1-bf3a-455c-bb90-58193dd90ce7", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-2-create-qa.ipynb", "title": "2. Creating a synthetic Q&A dataset", "tokens": 5416, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "da500b3c-9093-4acb-af8a-9bdf5a59d788", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-2-create-qa.ipynb", "title": "2. Creating a synthetic Q&A dataset", "tokens": 5416, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "1dace799e477d710c8a5f5e07dd9eb059b8ce0b711356d9bc729f127541dac5b", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "e43f6807-c153-4603-8454-d4d815c3b3d6", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-2-create-qa.ipynb", "title": "2. Creating a synthetic Q&A dataset", "tokens": 5416, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "cf436fcf584059b642229f24e698316541da77eb6f6a154772b6e84162cf8e08", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "cdc84c4e-2f43-4774-a9ff-01b6a34f2d7e", "node_type": "1", "metadata": {}, "hash": "e0726c13f839c826ae16182175605a6a17f0059ca5f8eeac18e89949dfc481ce", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 3721, "end_char_idx": 8249, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
from openai import OpenAI

client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY", "<your OpenAI API key if not set as env var>"))

def get_questions(context):
    try:
        response = client.chat.completions.create(model="davinci-instruct-beta-v3",
        prompt=f"Write questions based on the text below\n\nText: {context}\n\nQuestions:\n1.",
        temperature=0,
        max_tokens=257,
        top_p=1,
        frequency_penalty=0,
        presence_penalty=0,
        stop=["\n\n"])
        return response.choices[0].text
    except:
        return ""


df['questions']= df.context.apply(get_questions)
df['questions'] = "1." + df.questions
print(df[['questions']].values[0][0])
```

    1. What is the 2020 Summer Olympics?
    2. When did the 2020 Summer Olympics take place?
    3. Who won the most medals at the 2020 Summer Olympics?
    4. Who won the most gold medals at the 2020 Summer Olympics?
    5. Who won the most medals at the 2020 Summer Olympics?


The prompt is designed to generate a number of questions. Example questions above were generated based on the summary section of the 2020 Summer Olympics page.

We can observe that the questions 3 and 5 above repeat. Sometimes the generated questions could be ambiguous without the context. We will show that even despite these limitations we can create a successful model.


```python
print(df.content.values[0])
```

    The 2020 Summer Olympics (Japanese: 2020年夏季オリンピック, Hepburn: Nisen Nijū-nen Kaki Orinpikku), officially the Games of the XXXII Olympiad (第三十二回オリンピック競技大会, Dai Sanjūni-kai Orinpikku Kyōgi Taikai) and branded as Tokyo 2020 (東京2020, Tōkyō Nii Zero Nii Zero), was an international multi-sport event held from 23 July to 8 August 2021 in Tokyo, Japan, with some preliminary events that began on 21 July.
    Tokyo was selected as the host city during the 125th IOC Session in Buenos Aires, Argentina, on 7 September 2013. Originally scheduled to take place from 24 July to 9 August 2020, the event was postponed to 2021 in March 2020 as a result of the COVID-19 pandemic, the first such instance in the history of the Olympic Games (previous games had been cancelled but not rescheduled). However, the event retained the Tokyo 2020 name for marketing and branding purposes. It was largely held behind closed doors with no public spectators permitted due to the declaration of a state of emergency in the Greater Tokyo Area in response to the pandemic. The Summer Paralympics were held between 24 August and 5 September 2021, 16 days after the completion of the Olympics.The 2020 Games were the fourth Olympic Games to be held in Japan, following the Tokyo 1964 (Summer), Sapporo 1972 (Winter) and Nagano 1998 (Winter) games. Tokyo is the first city in Asia to hold the Summer Games twice. The 2020 Games were the second of three consecutive Olympics to be held in East Asia, following the 2018 Winter Olympics in Pyeongchang, South Korea and preceding the 2022 Winter Olympics in Beijing, China.
    New events were introduced in existing sports for 2020, including 3x3 basketball, freestyle BMX and mixed gender team events in a number of existing sports, as well as the return of madison cycling for men and an introduction of the same event for women. New IOC policies also allowed the host organizing committee to add new sports to the Olympic program for just one Games. The disciplines added by the Japanese Olympic Committee were baseball and softball, karate, sport climbing, surfing and skateboarding, the last four of which made their Olympic debuts, and the last three of which will remain on the Olympic program.The United States topped the medal count by both total golds (39) and total medals (113), with China finishing second by both respects (38 and 88). Host nation Japan finished third, setting a record for the most gold medals and total medals ever won by their delegation at an Olympic Games with 27 and 58. Great Britain finished fourth, with a total of 22 gold and 65 medals, becoming the first nation at the Summer Olympics to increase or equal their total medals won in the two Games subsequent to hosting them. The Russian delegation competing as the ROC (not to be confused with the Republic of China (Taiwan) which competed as Chinese Taipei, not ROC) finished fifth with 20 gold medals and third in the overall medal count, with 71 medals. Bermuda, the Philippines and Qatar won their first-ever Olympic gold medals. Burkina Faso, San Marino and Turkmenistan won their first-ever Olympic medals.


key: _node_content
string_value:
{"id_": "cdc84c4e-2f43-4774-a9ff-01b6a34f2d7e", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-2-create-qa.ipynb", "title": "2. Creating a synthetic Q&A dataset", "tokens": 5416, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "da500b3c-9093-4acb-af8a-9bdf5a59d788", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-2-create-qa.ipynb", "title": "2. Creating a synthetic Q&A dataset", "tokens": 5416, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "1dace799e477d710c8a5f5e07dd9eb059b8ce0b711356d9bc729f127541dac5b", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "1e9a90c1-bf3a-455c-bb90-58193dd90ce7", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-2-create-qa.ipynb", "title": "2. Creating a synthetic Q&A dataset", "tokens": 5416, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "d548f2cc12df3e3b7a6eba71d9b39b522601e72923332c318020f7573b637757", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "ec54d880-8559-49cb-bc0d-5771c86e3d23", "node_type": "1", "metadata": {}, "hash": "e2d82bcea0391beeace8fa174d127e5a91d9890e89be86df52aab2755179c96d", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 8252, "end_char_idx": 11280, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
## 2.3 Create answers based on the context
Use davinci-instruct to answer the questions given the relevant Wikipedia section contents

Note: We have used temperature=0, but it may be beneficial to experiment with a higher temperature to get a higher diversity of questions.

<span style="color:orange">**WARNING: This step will last a long time, and consume a lot of tokens, as it calls davinci-instruct for every section to answer all the questions.**</span>


```python
def get_answers(row):
    try:
        response = client.chat.completions.create(
            engine="davinci-instruct-beta-v3",
            prompt=f"Write answer based on the text below\n\nText: {row.context}\n\nQuestions:\n{row.questions}\n\nAnswers:\n1.",
            temperature=0,
            max_tokens=257,
            top_p=1,
            frequency_penalty=0,
            presence_penalty=0
        )
        return response.choices[0].text
    except Exception as e:
        print (e)
        return ""


df['answers']= df.apply(get_answers, axis=1)
df['answers'] = "1." + df.answers
df = df.dropna().reset_index().drop('index',axis=1)
print(df[['answers']].values[0][0])
```

    1. The 2020 Summer Olympics is an international multi-sport event held from 23 July to 8 August 2021 in Tokyo, Japan.
    2. The 2020 Summer Olympics took place from 23 July to 8 August 2021.
    3. The United States topped the medal count by both total golds (39) and total medals (113), with China finishing second by both respects (38 and 88).
    4. The United States topped the medal count by both total golds (39) and total medals (113), with China finishing second by both respects (38 and 88).
    5. The United States topped the medal count by both total golds (39) and total medals (113), with China finishing second by both respects (38 and 88).


These are the answers to the questions above based on the context around the host city selection. 

We can see that answers 3-5 contain the correct answer, but instead of answering the question directly, the answer is a verbatim extraction. Despite these occasional lower quality answers, we will show that the model can learn the task reasonably well, given a high number of examples.

## 2.4 Save the Olympics Q&A dataset based on Wikipedia sections
We save the file for use in the [next notebook](olympics-3-train-qa.ipynb)


```python
df.to_csv('olympics-data/olympics_qa.csv', index=False)
```

## 2.5 Search file (DEPRECATED)
We create a search file ([API reference](https://beta.openai.com/docs/api-reference/files/list)), which can be used to retrieve the relevant context when a question is asked.

<span style="color:orange; font-weight:bold">DEPRECATED: The /search endpoint is deprecated in favour of using embeddings. Embeddings are cheaper, faster and can support a better search experience. See <a href="https://github.com/openai/openai-cookbook/blob/main/examples/Question_answering_using_embeddings.ipynb">Question Answering Guide</a> for a search implementation using the embeddings</span>


key: _node_content
string_value:
{"id_": "ec54d880-8559-49cb-bc0d-5771c86e3d23", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-2-create-qa.ipynb", "title": "2. Creating a synthetic Q&A dataset", "tokens": 5416, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "da500b3c-9093-4acb-af8a-9bdf5a59d788", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-2-create-qa.ipynb", "title": "2. Creating a synthetic Q&A dataset", "tokens": 5416, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "1dace799e477d710c8a5f5e07dd9eb059b8ce0b711356d9bc729f127541dac5b", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "cdc84c4e-2f43-4774-a9ff-01b6a34f2d7e", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-2-create-qa.ipynb", "title": "2. Creating a synthetic Q&A dataset", "tokens": 5416, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "9cf622602c2607d3253a740f647ea1035019f56db5c1ef51d6068c11f8461d8c", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "0ffb8141-6bab-4b90-969e-bc1fcbece6b5", "node_type": "1", "metadata": {}, "hash": "b26160b2fc3dd4b93c22d0e38b68ab6b5ca5deab448fedc70b44d3852594faa9", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 10073, "end_char_idx": 12028, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
These are the answers to the questions above based on the context around the host city selection. 

We can see that answers 3-5 contain the correct answer, but instead of answering the question directly, the answer is a verbatim extraction. Despite these occasional lower quality answers, we will show that the model can learn the task reasonably well, given a high number of examples.

## 2.4 Save the Olympics Q&A dataset based on Wikipedia sections
We save the file for use in the [next notebook](olympics-3-train-qa.ipynb)


```python
df.to_csv('olympics-data/olympics_qa.csv', index=False)
```

## 2.5 Search file (DEPRECATED)
We create a search file ([API reference](https://beta.openai.com/docs/api-reference/files/list)), which can be used to retrieve the relevant context when a question is asked.

<span style="color:orange; font-weight:bold">DEPRECATED: The /search endpoint is deprecated in favour of using embeddings. Embeddings are cheaper, faster and can support a better search experience. See <a href="https://github.com/openai/openai-cookbook/blob/main/examples/Question_answering_using_embeddings.ipynb">Question Answering Guide</a> for a search implementation using the embeddings</span>



```python
df = df[df.tokens<2000]
df[['context', 'tokens']].rename(columns={'context':'text','tokens':'metadata'}).to_json('olympics-data/olympics_search.jsonl', orient='records', lines=True)

search_file = client.files.create(
  file=open("olympics-data/olympics_search.jsonl"),
  purpose='search'
)
olympics_search_fileid = search_file['id']
```

## 2.6 Answer questions based on the context provided

We will use a simple implementation of the answers endpoint. This works by simply using the [/search endpoint](https://beta.openai.com/docs/api-reference/searches), which searches over an indexed file to obtain the relevant sections which can be included in the context, following by a question and answering prompt given a specified model.


key: _node_content
string_value:
{"id_": "0ffb8141-6bab-4b90-969e-bc1fcbece6b5", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-2-create-qa.ipynb", "title": "2. Creating a synthetic Q&A dataset", "tokens": 5416, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "da500b3c-9093-4acb-af8a-9bdf5a59d788", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-2-create-qa.ipynb", "title": "2. Creating a synthetic Q&A dataset", "tokens": 5416, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "1dace799e477d710c8a5f5e07dd9eb059b8ce0b711356d9bc729f127541dac5b", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "ec54d880-8559-49cb-bc0d-5771c86e3d23", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-2-create-qa.ipynb", "title": "2. Creating a synthetic Q&A dataset", "tokens": 5416, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "7d97047947889f466f9a82056ec82541870568e1b73f65bb2529b4709256e370", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "45022073-0536-4298-a178-7e22e52a4962", "node_type": "1", "metadata": {}, "hash": "2f0267626d95ae41aff474e162dacff7692b380bdd626554cae4e525be23dca0", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 10602, "end_char_idx": 14076, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
df.to_csv('olympics-data/olympics_qa.csv', index=False)
```

## 2.5 Search file (DEPRECATED)
We create a search file ([API reference](https://beta.openai.com/docs/api-reference/files/list)), which can be used to retrieve the relevant context when a question is asked.

<span style="color:orange; font-weight:bold">DEPRECATED: The /search endpoint is deprecated in favour of using embeddings. Embeddings are cheaper, faster and can support a better search experience. See <a href="https://github.com/openai/openai-cookbook/blob/main/examples/Question_answering_using_embeddings.ipynb">Question Answering Guide</a> for a search implementation using the embeddings</span>



```python
df = df[df.tokens<2000]
df[['context', 'tokens']].rename(columns={'context':'text','tokens':'metadata'}).to_json('olympics-data/olympics_search.jsonl', orient='records', lines=True)

search_file = client.files.create(
  file=open("olympics-data/olympics_search.jsonl"),
  purpose='search'
)
olympics_search_fileid = search_file['id']
```

## 2.6 Answer questions based on the context provided

We will use a simple implementation of the answers endpoint. This works by simply using the [/search endpoint](https://beta.openai.com/docs/api-reference/searches), which searches over an indexed file to obtain the relevant sections which can be included in the context, following by a question and answering prompt given a specified model.


```python
from answers_with_ft import create_context, answer_question
print(create_context("Where did women's 4 x 100 metres relay event take place during the 2020 Summer Olympics?", olympics_search_fileid, max_len=400))
```

    Athletics at the 2020 Summer Olympics – Women's 4 × 100 metres relay
    Summary
    
    The women's 4 × 100 metres relay event at the 2020 Summer Olympics took place on 5 and 6 August 2021 at the Japan National Stadium. There were 16 competing relay teams, with each team having 5 members from which 4 were selected in each round.
    
    ###
    
    Athletics at the 2020 Summer Olympics – Men's 4 × 100 metres relay
    Qualification
    
    National Olympic Committees (NOCs) could qualify one relay team in one of three following ways:
    The top 8 NOCs at the 2019 World Athletics Championships qualified a relay team.
    The top 8 NOCs at the 2021 World Athletics Relays qualified a relay team.
    Where an NOC placed in the top 8 at both the 2019 World Championships and the 2021 World Relays, the quota place was allocated to the world top list as of 29 June 2021. In this case, 4 teams did so, so there are 4 places available through the world rankings.A total of five athletes may be entered for a relay team. Should a NOC have also entered individual athletes in the corresponding individual event (100 m), the entered individual athletes must be included in the total of five (5) athletes entered for the relay event. In addition of five, NOCs can nominate a maximum of one alternate athlete for each team.
    The qualifying period was originally from 1 May 2019 to 29 June 2020. Due to the COVID-19 pandemic, the period was suspended from 6 April 2020 to 30 November 2020, with the end date extended to 29 June 2021. The qualifying time standards could be obtained in various meets during the given period that have the approval of the IAAF. Both indoor and outdoor meets are eligible. The most recent Area Championships may be counted in the ranking, even if not during the qualifying period.


key: _node_content
string_value:
{"id_": "45022073-0536-4298-a178-7e22e52a4962", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-2-create-qa.ipynb", "title": "2. Creating a synthetic Q&A dataset", "tokens": 5416, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "da500b3c-9093-4acb-af8a-9bdf5a59d788", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-2-create-qa.ipynb", "title": "2. Creating a synthetic Q&A dataset", "tokens": 5416, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "1dace799e477d710c8a5f5e07dd9eb059b8ce0b711356d9bc729f127541dac5b", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "0ffb8141-6bab-4b90-969e-bc1fcbece6b5", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-2-create-qa.ipynb", "title": "2. Creating a synthetic Q&A dataset", "tokens": 5416, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "9fcd86934fc9d29d04399771233349e9a941a5b1f6702df6b8936e09b15b6d50", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "3d6b2832-b1d6-4a3a-bdf5-cdff5f08ae8c", "node_type": "1", "metadata": {}, "hash": "e162a1ea5a2e13a63d47a7a226af8464a6db998ee2dfe66f4371b7ccdf6096d5", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 14080, "end_char_idx": 15448, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
answer_question(olympics_search_fileid, "davinci-instruct-beta-v3", 
            "Where did women's 4 x 100 metres relay event take place during the 2020 Summer Olympics?")
```




    ' Japan National Stadium'



After we fine-tune the model for Q&A we'll be able to use it instead of [`davinci-instruct-beta-v3`](https://beta.openai.com/docs/engines/instruct-series-beta), to obtain better answers when the question can't be answered based on the context. We see a downside of [`davinci-instruct-beta-v3`](https://beta.openai.com/docs/engines/instruct-series-beta), which always attempts to answer the question, regardless of the relevant context being present or not. (Note the second question is asking about a future event, set in 2024.)


```python
answer_question(olympics_search_fileid, "davinci-instruct-beta-v3", 
            "Where did women's 4 x 100 metres relay event take place during the 2048 Summer Olympics?", max_len=1000)
```




    ' Japan National Stadium'



We can see that davinci has a tendency to answer the question, even if the question can't be answered given the context provided. Note the question asked regarding 2048 Summer Olympics, which didn't happen yet, and the retrieved content has only returned results for 2020.

## 2.7 (Optional) Investigation into how likely the search endpoint is to return the relevant context


key: _node_content
string_value:
{"id_": "3d6b2832-b1d6-4a3a-bdf5-cdff5f08ae8c", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-2-create-qa.ipynb", "title": "2. Creating a synthetic Q&A dataset", "tokens": 5416, "retrieve_doc": true, "source": "openai_cookbooks"}, "excluded_embed_metadata_keys": ["url", "tokens", "retrieve_doc", "source"], "excluded_llm_metadata_keys": ["title", "tokens", "retrieve_doc", "source"], "relationships": {"1": {"node_id": "da500b3c-9093-4acb-af8a-9bdf5a59d788", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-2-create-qa.ipynb", "title": "2. Creating a synthetic Q&A dataset", "tokens": 5416, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "1dace799e477d710c8a5f5e07dd9eb059b8ce0b711356d9bc729f127541dac5b", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "45022073-0536-4298-a178-7e22e52a4962", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/fine-tuned_qa/olympics-2-create-qa.ipynb", "title": "2. Creating a synthetic Q&A dataset", "tokens": 5416, "retrieve_doc": true, "source": "openai_cookbooks"}, "hash": "89c66e22e494442c292c09d1c4d22d57be09fb649104880b24a7cbb2f5d62f8e", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "85190d63-ff96-4fba-976b-c1ee493f1348", "node_type": "1", "metadata": {}, "hash": "c69dba5798852c1c363dc15b6b742c21b38c995a565652d205b7fd4e889cb418", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 14080, "end_char_idx": 17343, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


