# embedding_metadata

key: _node_content
string_value:
{"id_": "b32fe774-edf2-4337-81c9-9f59033460ea", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/Getting_started_with_AnalyticDB_and_OpenAI.ipynb", "title": "Using AnalyticDB as a vector database for OpenAI embeddings", "tokens": 2780, "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": "164dda0e-f7c0-4b5a-ae39-9891ba94cd3d", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/Getting_started_with_AnalyticDB_and_OpenAI.ipynb", "title": "Using AnalyticDB as a vector database for OpenAI embeddings", "tokens": 2780, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "5aae3de48381cf24d3238eacfd7440ef3cfd52b4fd5c8382f98929d25cae302f", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "df5b36f3-00f9-4001-99ad-064a73d37d4c", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/Getting_started_with_AnalyticDB_and_OpenAI.ipynb", "title": "Using AnalyticDB as a vector database for OpenAI embeddings", "tokens": 2780, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "3166493eacffc668716cff8ca0462d813c2d1af07cf8dc953e824a65d3680009", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "29a2f7ff-e80c-4e8e-8d2b-60eecebabd84", "node_type": "1", "metadata": {}, "hash": "d0a8ce63266db8f9fc793f11e091932a91b1a815c450471c0aedc04a8d1e4806", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 6858, "end_char_idx": 9171, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


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

# Path to your local CSV file
csv_file_path = '../../data/vector_database_wikipedia_articles_embedded.csv'

# Define a generator function to process the file line by line
def process_file(file_path):
    with open(file_path, 'r') as file:
        for line in file:
            # Replace '[' with '{' and ']' with '}'
            modified_line = line.replace('[', '{').replace(']', '}')
            yield modified_line

# Create a StringIO object to store the modified lines
modified_lines = io.StringIO(''.join(list(process_file(csv_file_path))))

# Create the COPY command for the copy_expert method
copy_command = '''
COPY public.articles (id, url, title, content, title_vector, content_vector, vector_id)
FROM STDIN WITH (FORMAT CSV, HEADER true, DELIMITER ',');
'''

# Execute the COPY command using the copy_expert method
cursor.copy_expert(copy_command, modified_lines)

# Commit the changes
connection.commit()
```


```python
# Check the collection size to make sure all the points have been stored
count_sql = """select count(*) from public.articles;"""
cursor.execute(count_sql)
result = cursor.fetchone()
print(f"Count:{result[0]}")

```

    Count:25000


## Search data

Once the data is put into Qdrant we will start querying the collection for the closest vectors. We may provide an additional parameter `vector_name` to switch from title to content based search. Since the precomputed embeddings were created with `text-embedding-3-small` OpenAI model we also have to use it during search.



```python
def query_analyticdb(query, collection_name, vector_name="title_vector", top_k=20):

    # Creates embedding vector from user query
    embedded_query = openai.Embedding.create(
        input=query,
        model="text-embedding-3-small",
    )["data"][0]["embedding"]

    # Convert the embedded_query to PostgreSQL compatible format
    embedded_query_pg = "{" + ",".join(map(str, embedded_query)) + "}"

    # Create SQL query
    query_sql = f"""
    SELECT id, url, title, l2_distance({vector_name},'{embedded_query_pg}'::real[]) AS similarity
    FROM {collection_name}
    ORDER BY {vector_name} <-> '{embedded_query_pg}'::real[]
    LIMIT {top_k};
    """
    # Execute the query
    cursor.execute(query_sql)
    results = cursor.fetchall()

    return results
```


key: _node_content
string_value:
{"id_": "29a2f7ff-e80c-4e8e-8d2b-60eecebabd84", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/Getting_started_with_AnalyticDB_and_OpenAI.ipynb", "title": "Using AnalyticDB as a vector database for OpenAI embeddings", "tokens": 2780, "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": "164dda0e-f7c0-4b5a-ae39-9891ba94cd3d", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/Getting_started_with_AnalyticDB_and_OpenAI.ipynb", "title": "Using AnalyticDB as a vector database for OpenAI embeddings", "tokens": 2780, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "5aae3de48381cf24d3238eacfd7440ef3cfd52b4fd5c8382f98929d25cae302f", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "b32fe774-edf2-4337-81c9-9f59033460ea", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/Getting_started_with_AnalyticDB_and_OpenAI.ipynb", "title": "Using AnalyticDB as a vector database for OpenAI embeddings", "tokens": 2780, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "521d27630c85f3e2a3706437b3b1d1f6bc3d5592ef6d89c74152471a7d318e8d", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "ccdfb8e4-8efc-4e14-a2fa-ea9ca64ca2a9", "node_type": "1", "metadata": {}, "hash": "6ce52553ba68fd60408533b222c6b341632b7e2e06b15225e5616ccedf8b775a", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 7803, "end_char_idx": 10087, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
# Check the collection size to make sure all the points have been stored
count_sql = """select count(*) from public.articles;"""
cursor.execute(count_sql)
result = cursor.fetchone()
print(f"Count:{result[0]}")

```

    Count:25000


## Search data

Once the data is put into Qdrant we will start querying the collection for the closest vectors. We may provide an additional parameter `vector_name` to switch from title to content based search. Since the precomputed embeddings were created with `text-embedding-3-small` OpenAI model we also have to use it during search.



```python
def query_analyticdb(query, collection_name, vector_name="title_vector", top_k=20):

    # Creates embedding vector from user query
    embedded_query = openai.Embedding.create(
        input=query,
        model="text-embedding-3-small",
    )["data"][0]["embedding"]

    # Convert the embedded_query to PostgreSQL compatible format
    embedded_query_pg = "{" + ",".join(map(str, embedded_query)) + "}"

    # Create SQL query
    query_sql = f"""
    SELECT id, url, title, l2_distance({vector_name},'{embedded_query_pg}'::real[]) AS similarity
    FROM {collection_name}
    ORDER BY {vector_name} <-> '{embedded_query_pg}'::real[]
    LIMIT {top_k};
    """
    # Execute the query
    cursor.execute(query_sql)
    results = cursor.fetchall()

    return results
```


```python
import openai

query_results = query_analyticdb("modern art in Europe", "Articles")
for i, result in enumerate(query_results):
    print(f"{i + 1}. {result[2]} (Score: {round(1 - result[3], 3)})")
```

    1. Museum of Modern Art (Score: 0.75)
    2. Western Europe (Score: 0.735)
    3. Renaissance art (Score: 0.728)
    4. Pop art (Score: 0.721)
    5. Northern Europe (Score: 0.71)
    6. Hellenistic art (Score: 0.706)
    7. Modernist literature (Score: 0.694)
    8. Art film (Score: 0.687)
    9. Central Europe (Score: 0.685)
    10. European (Score: 0.683)
    11. Art (Score: 0.683)
    12. Byzantine art (Score: 0.682)
    13. Postmodernism (Score: 0.68)
    14. Eastern Europe (Score: 0.679)
    15. Europe (Score: 0.678)
    16. Cubism (Score: 0.678)
    17. Impressionism (Score: 0.677)
    18. Bauhaus (Score: 0.676)
    19. Surrealism (Score: 0.674)
    20. Expressionism (Score: 0.674)


key: _node_content
string_value:
{"id_": "ccdfb8e4-8efc-4e14-a2fa-ea9ca64ca2a9", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/Getting_started_with_AnalyticDB_and_OpenAI.ipynb", "title": "Using AnalyticDB as a vector database for OpenAI embeddings", "tokens": 2780, "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": "164dda0e-f7c0-4b5a-ae39-9891ba94cd3d", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/Getting_started_with_AnalyticDB_and_OpenAI.ipynb", "title": "Using AnalyticDB as a vector database for OpenAI embeddings", "tokens": 2780, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "5aae3de48381cf24d3238eacfd7440ef3cfd52b4fd5c8382f98929d25cae302f", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "29a2f7ff-e80c-4e8e-8d2b-60eecebabd84", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/Getting_started_with_AnalyticDB_and_OpenAI.ipynb", "title": "Using AnalyticDB as a vector database for OpenAI embeddings", "tokens": 2780, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "e15ac6550da97fbb06791eb4cf14b2c5ebec9c83254893315160aa513190e7b5", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 9174, "end_char_idx": 11060, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


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

query_results = query_analyticdb("modern art in Europe", "Articles")
for i, result in enumerate(query_results):
    print(f"{i + 1}. {result[2]} (Score: {round(1 - result[3], 3)})")
```

    1. Museum of Modern Art (Score: 0.75)
    2. Western Europe (Score: 0.735)
    3. Renaissance art (Score: 0.728)
    4. Pop art (Score: 0.721)
    5. Northern Europe (Score: 0.71)
    6. Hellenistic art (Score: 0.706)
    7. Modernist literature (Score: 0.694)
    8. Art film (Score: 0.687)
    9. Central Europe (Score: 0.685)
    10. European (Score: 0.683)
    11. Art (Score: 0.683)
    12. Byzantine art (Score: 0.682)
    13. Postmodernism (Score: 0.68)
    14. Eastern Europe (Score: 0.679)
    15. Europe (Score: 0.678)
    16. Cubism (Score: 0.678)
    17. Impressionism (Score: 0.677)
    18. Bauhaus (Score: 0.676)
    19. Surrealism (Score: 0.674)
    20. Expressionism (Score: 0.674)



```python
# This time we'll query using content vector
query_results = query_analyticdb("Famous battles in Scottish history", "Articles", "content_vector")
for i, result in enumerate(query_results):
    print(f"{i + 1}. {result[2]} (Score: {round(1 - result[3], 3)})")
```

    1. Battle of Bannockburn (Score: 0.739)
    2. Wars of Scottish Independence (Score: 0.723)
    3. 1651 (Score: 0.705)
    4. First War of Scottish Independence (Score: 0.699)
    5. Robert I of Scotland (Score: 0.692)
    6. 841 (Score: 0.688)
    7. 1716 (Score: 0.688)
    8. 1314 (Score: 0.674)
    9. 1263 (Score: 0.673)
    10. William Wallace (Score: 0.671)
    11. Stirling (Score: 0.663)
    12. 1306 (Score: 0.662)
    13. 1746 (Score: 0.661)
    14. 1040s (Score: 0.656)
    15. 1106 (Score: 0.654)
    16. 1304 (Score: 0.653)
    17. David II of Scotland (Score: 0.65)
    18. Braveheart (Score: 0.649)
    19. 1124 (Score: 0.648)
    20. July 27 (Score: 0.646)



```python

```


key: url
string_value:
https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb


key: _node_content
string_value:
{"id_": "b1da2a1d-e1af-4d2c-a76e-043c4fd1358e", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "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": "8ad6517f-8c3a-4060-ba7a-7cdccad68e98", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "30d78a1dbf2b0b4cfaca0f7dffef40e192fa56285d4a39b5bc286b7ba2dcf961", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "53ac805b-7a61-413f-8efa-daf527477e5d", "node_type": "1", "metadata": {}, "hash": "d7a506978c21fb9b927d19497ba294e0dd3b1522b75af18a64e0397330b37d8d", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 0, "end_char_idx": 3195, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
# Question Answering with Langchain, AnalyticDB and OpenAI
This notebook presents how to implement a Question Answering system with Langchain, AnalyticDB as a knowledge based and OpenAI embeddings. If you are not familiar with AnalyticDB, it’s better to check out the [Getting_started_with_AnalyticDB_and_OpenAI.ipynb](Getting_started_with_AnalyticDB_and_OpenAI.ipynb) notebook.

This notebook presents an end-to-end process of:
- Calculating the embeddings with OpenAI API.
- Storing the embeddings in an AnalyticDB instance to build a knowledge base.
- Converting raw text query to an embedding with OpenAI API.
- Using AnalyticDB to perform the nearest neighbour search in the created collection to find some context.
- Asking LLM to find the answer in a given context.

All the steps will be simplified to calling some corresponding Langchain methods.

## Prerequisites
For the purposes of this exercise we need to prepare a couple of things:
[AnalyticDB cloud instance](https://www.alibabacloud.com/help/en/analyticdb-for-postgresql/latest/product-introduction-overview).
[Langchain](https://github.com/hwchase17/langchain) as a framework.
An OpenAI API key.

### Install requirements
This notebook requires the following Python packages: `openai`, `tiktoken`, `langchain` and `psycopg2cffi`.
- `openai` provides convenient access to the OpenAI API.
- `tiktoken` is a fast BPE tokeniser for use with OpenAI's models.
- `langchain` helps us to build applications with LLM more easily.
- `psycopg2cffi` library is used to interact with the vector database, but any other PostgreSQL client library is also acceptable.


```python
! pip install openai tiktoken langchain psycopg2cffi 
```


```python
! export OPENAI_API_KEY="your API key"
```


```python
# Test that your OpenAI API key is correctly set as an environment variable
# Note. if you run this notebook locally, you will need to reload your terminal and the notebook for the env variables to be live.
import os

# Note. alternatively you can set a temporary env variable like this:
# os.environ["OPENAI_API_KEY"] = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

if os.getenv("OPENAI_API_KEY") is not None:
    print("OPENAI_API_KEY is ready")
else:
    print("OPENAI_API_KEY environment variable not found")
```

    OPENAI_API_KEY is ready


### Prepare your OpenAI API key
The OpenAI API key is used for vectorization of the documents and queries.

If you don't have an OpenAI API key, you can get one from [https://platform.openai.com/account/api-keys ).

Once you get your key, please add it to your environment variables as `OPENAI_API_KEY` by running following command:

### Prepare your AnalyticDB connection string
To build the AnalyticDB connection string, you need to have the following parameters: `PG_HOST`, `PG_PORT`, `PG_DATABASE`, `PG_USER`, and `PG_PASSWORD`. You need to export them first to set correct connect string. Then build the connection string.


```python
! export PG_HOST="your AnalyticDB host url"
! export PG_PORT=5432 # Optional, default value is 5432
! export PG_DATABASE=postgres # Optional, default value is postgres
! export PG_USER="your username"
! export PG_PASSWORD="your password"
```


key: _node_content
string_value:
{"id_": "53ac805b-7a61-413f-8efa-daf527477e5d", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "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": "8ad6517f-8c3a-4060-ba7a-7cdccad68e98", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "30d78a1dbf2b0b4cfaca0f7dffef40e192fa56285d4a39b5bc286b7ba2dcf961", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "b1da2a1d-e1af-4d2c-a76e-043c4fd1358e", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "681b4a52e3e77fe6583559387bb4567a7d86a63835d13541b71e44fa14f5ed14", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "3599abbf-0a8e-44e2-9317-b51de29c9d12", "node_type": "1", "metadata": {}, "hash": "c9617e590c550fe2d103fc3983b663bb57d96470cf03450c6790b7179a3339be", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 1622, "end_char_idx": 4641, "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 openai tiktoken langchain psycopg2cffi 
```


```python
! export OPENAI_API_KEY="your API key"
```


```python
# Test that your OpenAI API key is correctly set as an environment variable
# Note. if you run this notebook locally, you will need to reload your terminal and the notebook for the env variables to be live.
import os

# Note. alternatively you can set a temporary env variable like this:
# os.environ["OPENAI_API_KEY"] = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

if os.getenv("OPENAI_API_KEY") is not None:
    print("OPENAI_API_KEY is ready")
else:
    print("OPENAI_API_KEY environment variable not found")
```

    OPENAI_API_KEY is ready


### Prepare your OpenAI API key
The OpenAI API key is used for vectorization of the documents and queries.

If you don't have an OpenAI API key, you can get one from [https://platform.openai.com/account/api-keys ).

Once you get your key, please add it to your environment variables as `OPENAI_API_KEY` by running following command:

### Prepare your AnalyticDB connection string
To build the AnalyticDB connection string, you need to have the following parameters: `PG_HOST`, `PG_PORT`, `PG_DATABASE`, `PG_USER`, and `PG_PASSWORD`. You need to export them first to set correct connect string. Then build the connection string.


```python
! export PG_HOST="your AnalyticDB host url"
! export PG_PORT=5432 # Optional, default value is 5432
! export PG_DATABASE=postgres # Optional, default value is postgres
! export PG_USER="your username"
! export PG_PASSWORD="your password"
```


```python
import os
from langchain.vectorstores.analyticdb import AnalyticDB

CONNECTION_STRING = AnalyticDB.connection_string_from_db_params(
    driver=os.environ.get("PG_DRIVER", "psycopg2cffi"),
    host=os.environ.get("PG_HOST", "localhost"),
    port=int(os.environ.get("PG_PORT", "5432")),
    database=os.environ.get("PG_DATABASE", "postgres"),
    user=os.environ.get("PG_USER", "postgres"),
    password=os.environ.get("PG_PASSWORD", "postgres"),
)
```


```python
import json

with open("questions.json", "r") as fp:
    questions = json.load(fp)

with open("answers.json", "r") as fp:
    answers = json.load(fp)
```

## Load data
In this section we are going to load the data containing some natural questions and answers to them. All the data will be used to create a Langchain application with AnalyticDB being the knowledge base.


```python
print(questions[0])
```

    when is the last episode of season 8 of the walking dead



```python
import wget

# All the examples come from https://ai.google.com/research/NaturalQuestions
# This is a sample of the training set that we download and extract for some
# further processing.
wget.download("https://storage.googleapis.com/dataset-natural-questions/questions.json")
wget.download("https://storage.googleapis.com/dataset-natural-questions/answers.json")
```


```python
print(answers[0])
```

    No . overall No. in season Title Directed by Written by Original air date U.S.


key: _node_content
string_value:
{"id_": "3599abbf-0a8e-44e2-9317-b51de29c9d12", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "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": "8ad6517f-8c3a-4060-ba7a-7cdccad68e98", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "30d78a1dbf2b0b4cfaca0f7dffef40e192fa56285d4a39b5bc286b7ba2dcf961", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "53ac805b-7a61-413f-8efa-daf527477e5d", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "fa4e3327519c6de3bbb1e98bed776b9cd4ba9ec6a9d6acdce13c28d70ce31ee2", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "cac0f32a-eee5-437a-ae75-67af4029c4a8", "node_type": "1", "metadata": {}, "hash": "17c202e3bcc232572bf173037fc46d2538d48c0ccdb49247e493b304d12248c2", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 3198, "end_char_idx": 6355, "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
from langchain.vectorstores.analyticdb import AnalyticDB

CONNECTION_STRING = AnalyticDB.connection_string_from_db_params(
    driver=os.environ.get("PG_DRIVER", "psycopg2cffi"),
    host=os.environ.get("PG_HOST", "localhost"),
    port=int(os.environ.get("PG_PORT", "5432")),
    database=os.environ.get("PG_DATABASE", "postgres"),
    user=os.environ.get("PG_USER", "postgres"),
    password=os.environ.get("PG_PASSWORD", "postgres"),
)
```


```python
import json

with open("questions.json", "r") as fp:
    questions = json.load(fp)

with open("answers.json", "r") as fp:
    answers = json.load(fp)
```

## Load data
In this section we are going to load the data containing some natural questions and answers to them. All the data will be used to create a Langchain application with AnalyticDB being the knowledge base.


```python
print(questions[0])
```

    when is the last episode of season 8 of the walking dead



```python
import wget

# All the examples come from https://ai.google.com/research/NaturalQuestions
# This is a sample of the training set that we download and extract for some
# further processing.
wget.download("https://storage.googleapis.com/dataset-natural-questions/questions.json")
wget.download("https://storage.googleapis.com/dataset-natural-questions/answers.json")
```


```python
print(answers[0])
```

    No . overall No. in season Title Directed by Written by Original air date U.S. viewers ( millions ) 100 `` Mercy '' Greg Nicotero Scott M. Gimple October 22 , 2017 ( 2017 - 10 - 22 ) 11.44 Rick , Maggie , and Ezekiel rally their communities together to take down Negan . Gregory attempts to have the Hilltop residents side with Negan , but they all firmly stand behind Maggie . The group attacks the Sanctuary , taking down its fences and flooding the compound with walkers . With the Sanctuary defaced , everyone leaves except Gabriel , who reluctantly stays to save Gregory , but is left behind when Gregory abandons him . Surrounded by walkers , Gabriel hides in a trailer , where he is trapped inside with Negan . 101 `` The Damned '' Rosemary Rodriguez Matthew Negrete & Channing Powell October 29 , 2017 ( 2017 - 10 - 29 ) 8.92 Rick 's forces split into separate parties to attack several of the Saviors ' outposts , during which many members of the group are killed ; Eric is critically injured and rushed away by Aaron . Jesus stops Tara and Morgan from executing a group of surrendered Saviors . While clearing an outpost with Daryl , Rick is confronted and held at gunpoint by Morales , a survivor he met in the initial Atlanta camp , who is now with the Saviors . 102 `` Monsters '' Greg Nicotero Matthew Negrete & Channing Powell November 5 , 2017 ( 2017 - 11 - 05 ) 8.52 Daryl finds Morales threatening Rick and kills him ; the duo then pursue a group of Saviors who are transporting weapons to another outpost . Gregory returns to Hilltop , and after a heated argument , Maggie ultimately allows him back in the community . Eric dies from his injuries , leaving Aaron distraught . Despite Tara and Morgan 's objections , Jesus leads the group of surrendered Saviors to Hilltop .


key: _node_content
string_value:
{"id_": "cac0f32a-eee5-437a-ae75-67af4029c4a8", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "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": "8ad6517f-8c3a-4060-ba7a-7cdccad68e98", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "30d78a1dbf2b0b4cfaca0f7dffef40e192fa56285d4a39b5bc286b7ba2dcf961", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "3599abbf-0a8e-44e2-9317-b51de29c9d12", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "096c34011f884f1eb5b87cce3b567cee051ec716cd0a87c90c8e05a82457307c", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "066dca2a-3fea-45bd-aae9-fc71f9b24eec", "node_type": "1", "metadata": {}, "hash": "73dbbccf7f3338adc8c15bf29974e78704fc37f172cc4fad6cf91b73ca10884e", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 4834, "end_char_idx": 8053, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
Gregory attempts to have the Hilltop residents side with Negan , but they all firmly stand behind Maggie . The group attacks the Sanctuary , taking down its fences and flooding the compound with walkers . With the Sanctuary defaced , everyone leaves except Gabriel , who reluctantly stays to save Gregory , but is left behind when Gregory abandons him . Surrounded by walkers , Gabriel hides in a trailer , where he is trapped inside with Negan . 101 `` The Damned '' Rosemary Rodriguez Matthew Negrete & Channing Powell October 29 , 2017 ( 2017 - 10 - 29 ) 8.92 Rick 's forces split into separate parties to attack several of the Saviors ' outposts , during which many members of the group are killed ; Eric is critically injured and rushed away by Aaron . Jesus stops Tara and Morgan from executing a group of surrendered Saviors . While clearing an outpost with Daryl , Rick is confronted and held at gunpoint by Morales , a survivor he met in the initial Atlanta camp , who is now with the Saviors . 102 `` Monsters '' Greg Nicotero Matthew Negrete & Channing Powell November 5 , 2017 ( 2017 - 11 - 05 ) 8.52 Daryl finds Morales threatening Rick and kills him ; the duo then pursue a group of Saviors who are transporting weapons to another outpost . Gregory returns to Hilltop , and after a heated argument , Maggie ultimately allows him back in the community . Eric dies from his injuries , leaving Aaron distraught . Despite Tara and Morgan 's objections , Jesus leads the group of surrendered Saviors to Hilltop . Ezekiel 's group attacks another Savior compound , during which several Kingdommers are shot while protecting Ezekiel . 103 `` Some Guy '' Dan Liu David Leslie Johnson November 12 , 2017 ( 2017 - 11 - 12 ) 8.69 Ezekiel 's group is overwhelmed by the Saviors , who kill all of them except for Ezekiel himself and Jerry . Carol clears the inside of the compound , killing all but two Saviors , who almost escape but are eventually caught by Rick and Daryl . En route to the Kingdom , Ezekiel , Jerry , and Carol are surrounded by walkers , but Shiva sacrifices herself to save them . The trio returns to the Kingdom , where Ezekiel 's confidence in himself as a leader has diminished . 104 5 `` The Big Scary U '' Michael E. Satrazemis Story by : Scott M. Gimple & David Leslie Johnson & Angela Kang Teleplay by : David Leslie Johnson & Angela Kang November 19 , 2017 ( 2017 - 11 - 19 ) 7.85 After confessing their sins to each other , Gabriel and Negan manage to escape from the trailer . Simon and the other lieutenants grow suspicious of each other , knowing that Rick 's forces must have inside information . The workers in the Sanctuary become increasingly frustrated with their living conditions , and a riot nearly ensues , until Negan returns and restores order . Gabriel is locked in a cell , where Eugene discovers him sick and suffering . Meanwhile , Rick and Daryl argue over how to take out the Saviors , leading Daryl to abandon Rick . 105 6 `` The King , the Widow , and Rick '' John Polson Angela Kang & Corey Reed November 26 , 2017 ( 2017 - 11 - 26 ) 8.28 Rick visits Jadis in hopes of convincing her to turn against Negan ; Jadis refuses , and locks Rick in a shipping container .


key: _node_content
string_value:
{"id_": "066dca2a-3fea-45bd-aae9-fc71f9b24eec", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "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": "8ad6517f-8c3a-4060-ba7a-7cdccad68e98", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "30d78a1dbf2b0b4cfaca0f7dffef40e192fa56285d4a39b5bc286b7ba2dcf961", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "cac0f32a-eee5-437a-ae75-67af4029c4a8", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "4cde8cf8d63fab5800665978f7be35c9e195227921fc53cf63fdf5aba517271f", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "5c318a64-52e7-4a71-bd22-d5b31ec358fb", "node_type": "1", "metadata": {}, "hash": "cb60107c065b20a505236b4a783526f253147b90402d4c55b8dd05c8edabd56c", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 6476, "end_char_idx": 9569, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
103 `` Some Guy '' Dan Liu David Leslie Johnson November 12 , 2017 ( 2017 - 11 - 12 ) 8.69 Ezekiel 's group is overwhelmed by the Saviors , who kill all of them except for Ezekiel himself and Jerry . Carol clears the inside of the compound , killing all but two Saviors , who almost escape but are eventually caught by Rick and Daryl . En route to the Kingdom , Ezekiel , Jerry , and Carol are surrounded by walkers , but Shiva sacrifices herself to save them . The trio returns to the Kingdom , where Ezekiel 's confidence in himself as a leader has diminished . 104 5 `` The Big Scary U '' Michael E. Satrazemis Story by : Scott M. Gimple & David Leslie Johnson & Angela Kang Teleplay by : David Leslie Johnson & Angela Kang November 19 , 2017 ( 2017 - 11 - 19 ) 7.85 After confessing their sins to each other , Gabriel and Negan manage to escape from the trailer . Simon and the other lieutenants grow suspicious of each other , knowing that Rick 's forces must have inside information . The workers in the Sanctuary become increasingly frustrated with their living conditions , and a riot nearly ensues , until Negan returns and restores order . Gabriel is locked in a cell , where Eugene discovers him sick and suffering . Meanwhile , Rick and Daryl argue over how to take out the Saviors , leading Daryl to abandon Rick . 105 6 `` The King , the Widow , and Rick '' John Polson Angela Kang & Corey Reed November 26 , 2017 ( 2017 - 11 - 26 ) 8.28 Rick visits Jadis in hopes of convincing her to turn against Negan ; Jadis refuses , and locks Rick in a shipping container . Carl encounters Siddiq in the woods and recruits him to Alexandria . Daryl and Tara plot to deviate from Rick 's plans by destroying the Sanctuary . Ezekiel isolates himself at the Kingdom , where Carol tries to encourage him to be the leader his people need . Maggie has the group of captured Saviors placed in a holding area and forces Gregory to join them as punishment for betraying Hilltop . 106 7 `` Time for After '' Larry Teng Matthew Negrete & Corey Reed December 3 , 2017 ( 2017 - 12 - 03 ) 7.47 After learning of Dwight 's association with Rick 's group , Eugene affirms his loyalty to Negan and outlines a plan to get rid of the walkers surrounding the Sanctuary . With help from Morgan and Tara , Daryl drives a truck through the Sanctuary 's walls , flooding its interior with walkers , killing many Saviors . Rick finally convinces Jadis and the Scavengers to align with him , and they plan to force the Saviors to surrender . However , when they arrive at the Sanctuary , Rick is horrified to see the breached walls and no sign of the walker herd . 107 8 `` How It 's Gotta Be '' Michael E. Satrazemis David Leslie Johnson & Angela Kang December 10 , 2017 ( 2017 - 12 - 10 ) 7.89 Eugene 's plan allows the Saviors to escape , and separately , the Saviors waylay the Alexandria , Hilltop , and Kingdom forces . The Scavengers abandon Rick , after which he returns to Alexandria . Ezekiel ensures that the Kingdom residents are able to escape before locking himself in the community with the Saviors .


key: _node_content
string_value:
{"id_": "5c318a64-52e7-4a71-bd22-d5b31ec358fb", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "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": "8ad6517f-8c3a-4060-ba7a-7cdccad68e98", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "30d78a1dbf2b0b4cfaca0f7dffef40e192fa56285d4a39b5bc286b7ba2dcf961", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "066dca2a-3fea-45bd-aae9-fc71f9b24eec", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "dc41270dee3b3ca526d48704b682e7029a0dea7a2965c169a7e3d0f9fd3ea510", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "ee2c039c-fdfb-4e7a-b3d7-4720d5cbdf6c", "node_type": "1", "metadata": {}, "hash": "fb3744ea7d5f5bf0abea2d5e69e6ccfb42772b1baf08b548458e0081534db0d6", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 8054, "end_char_idx": 10845, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
Carl encounters Siddiq in the woods and recruits him to Alexandria . Daryl and Tara plot to deviate from Rick 's plans by destroying the Sanctuary . Ezekiel isolates himself at the Kingdom , where Carol tries to encourage him to be the leader his people need . Maggie has the group of captured Saviors placed in a holding area and forces Gregory to join them as punishment for betraying Hilltop . 106 7 `` Time for After '' Larry Teng Matthew Negrete & Corey Reed December 3 , 2017 ( 2017 - 12 - 03 ) 7.47 After learning of Dwight 's association with Rick 's group , Eugene affirms his loyalty to Negan and outlines a plan to get rid of the walkers surrounding the Sanctuary . With help from Morgan and Tara , Daryl drives a truck through the Sanctuary 's walls , flooding its interior with walkers , killing many Saviors . Rick finally convinces Jadis and the Scavengers to align with him , and they plan to force the Saviors to surrender . However , when they arrive at the Sanctuary , Rick is horrified to see the breached walls and no sign of the walker herd . 107 8 `` How It 's Gotta Be '' Michael E. Satrazemis David Leslie Johnson & Angela Kang December 10 , 2017 ( 2017 - 12 - 10 ) 7.89 Eugene 's plan allows the Saviors to escape , and separately , the Saviors waylay the Alexandria , Hilltop , and Kingdom forces . The Scavengers abandon Rick , after which he returns to Alexandria . Ezekiel ensures that the Kingdom residents are able to escape before locking himself in the community with the Saviors . Eugene aids Gabriel and Doctor Carson in escaping the Sanctuary in order to ease his conscience . Negan attacks Alexandria , but Carl devises a plan to allow the Alexandria residents to escape into the sewers . Carl reveals he was bitten by a walker while escorting Siddiq to Alexandria . 108 9 `` Honor '' Greg Nicotero Matthew Negrete & Channing Powell February 25 , 2018 ( 2018 - 02 - 25 ) 8.28 After the Saviors leave Alexandria , the survivors make for the Hilltop while Rick and Michonne stay behind to say their final goodbyes to a dying Carl , who pleads with Rick to build a better future alongside the Saviors before killing himself . In the Kingdom , Morgan and Carol launch a rescue mission for Ezekiel . Although they are successful and retake the Kingdom , the Saviors ' lieutenant Gavin is killed by Benjamin 's vengeful brother Henry . 109 10 `` The Lost and the Plunderers '' TBA TBA March 4 , 2018 ( 2018 - 03 - 04 ) TBD 110 11 `` Dead or Alive Or '' TBA TBA March 11 , 2018 ( 2018 - 03 - 11 ) TBD 111 12 `` The Key '' TBA TBA March 18 , 2018 ( 2018 - 03 - 18 ) TBD


## Chain definition

Langchain is already integrated with AnalyticDB and performs all the indexing for given list of documents. In our case we are going to store the set of answers we have.


key: _node_content
string_value:
{"id_": "ee2c039c-fdfb-4e7a-b3d7-4720d5cbdf6c", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "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": "8ad6517f-8c3a-4060-ba7a-7cdccad68e98", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "30d78a1dbf2b0b4cfaca0f7dffef40e192fa56285d4a39b5bc286b7ba2dcf961", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "5c318a64-52e7-4a71-bd22-d5b31ec358fb", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "1a1ed099557ce3a3b99eb2602d4cda85059e95f86a70a9563ee232c3db2cacc3", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "53aad4f3-2355-44c4-a070-ce3c776573f2", "node_type": "1", "metadata": {}, "hash": "bf4975ede6e495c31704beb202fac9e8b0f102496b5ad3f3340fa0bfc87ea09f", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 9380, "end_char_idx": 11953, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
The Scavengers abandon Rick , after which he returns to Alexandria . Ezekiel ensures that the Kingdom residents are able to escape before locking himself in the community with the Saviors . Eugene aids Gabriel and Doctor Carson in escaping the Sanctuary in order to ease his conscience . Negan attacks Alexandria , but Carl devises a plan to allow the Alexandria residents to escape into the sewers . Carl reveals he was bitten by a walker while escorting Siddiq to Alexandria . 108 9 `` Honor '' Greg Nicotero Matthew Negrete & Channing Powell February 25 , 2018 ( 2018 - 02 - 25 ) 8.28 After the Saviors leave Alexandria , the survivors make for the Hilltop while Rick and Michonne stay behind to say their final goodbyes to a dying Carl , who pleads with Rick to build a better future alongside the Saviors before killing himself . In the Kingdom , Morgan and Carol launch a rescue mission for Ezekiel . Although they are successful and retake the Kingdom , the Saviors ' lieutenant Gavin is killed by Benjamin 's vengeful brother Henry . 109 10 `` The Lost and the Plunderers '' TBA TBA March 4 , 2018 ( 2018 - 03 - 04 ) TBD 110 11 `` Dead or Alive Or '' TBA TBA March 11 , 2018 ( 2018 - 03 - 11 ) TBD 111 12 `` The Key '' TBA TBA March 18 , 2018 ( 2018 - 03 - 18 ) TBD


## Chain definition

Langchain is already integrated with AnalyticDB and performs all the indexing for given list of documents. In our case we are going to store the set of answers we have.


```python
from langchain.vectorstores import AnalyticDB
from langchain.embeddings import OpenAIEmbeddings
from langchain import VectorDBQA, OpenAI

embeddings = OpenAIEmbeddings()
doc_store = AnalyticDB.from_texts(
    texts=answers, embedding=embeddings, connection_string=CONNECTION_STRING,
    pre_delete_collection=True,
)
```

At this stage all the possible answers are already stored in  AnalyticDB, so we can define the whole QA chain.


```python
from langchain.chains import RetrievalQA

llm = OpenAI()
qa = VectorDBQA.from_chain_type(
    llm=llm,
    chain_type="stuff",
    vectorstore=doc_store,
    return_source_documents=False,
)
```

## Search data

Once the data is put into AnalyticDB we can start asking some questions. A question will be automatically vectorized by OpenAI model, and the created vector will be used to find some possibly matching answers in AnalyticDB. Once retrieved, the most similar answers will be incorporated into the prompt sent to OpenAI Large Language Model.



```python
import random

random.seed(52)
selected_questions = random.choices(questions, k=5)
```


key: _node_content
string_value:
{"id_": "53aad4f3-2355-44c4-a070-ce3c776573f2", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "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": "8ad6517f-8c3a-4060-ba7a-7cdccad68e98", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "30d78a1dbf2b0b4cfaca0f7dffef40e192fa56285d4a39b5bc286b7ba2dcf961", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "ee2c039c-fdfb-4e7a-b3d7-4720d5cbdf6c", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "7f1d7e774cd38772ddee52f909478a0b8e3b881cdd9634dde98eca6340734d41", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "cd0684a2-6d3a-4365-a280-2c91738ccc01", "node_type": "1", "metadata": {}, "hash": "aad65fce997a6de8e7b106fe59e4a38d8f6ef9d2503faa1363469b2d4cbe69fa", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 10422, "end_char_idx": 13073, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
109 10 `` The Lost and the Plunderers '' TBA TBA March 4 , 2018 ( 2018 - 03 - 04 ) TBD 110 11 `` Dead or Alive Or '' TBA TBA March 11 , 2018 ( 2018 - 03 - 11 ) TBD 111 12 `` The Key '' TBA TBA March 18 , 2018 ( 2018 - 03 - 18 ) TBD


## Chain definition

Langchain is already integrated with AnalyticDB and performs all the indexing for given list of documents. In our case we are going to store the set of answers we have.


```python
from langchain.vectorstores import AnalyticDB
from langchain.embeddings import OpenAIEmbeddings
from langchain import VectorDBQA, OpenAI

embeddings = OpenAIEmbeddings()
doc_store = AnalyticDB.from_texts(
    texts=answers, embedding=embeddings, connection_string=CONNECTION_STRING,
    pre_delete_collection=True,
)
```

At this stage all the possible answers are already stored in  AnalyticDB, so we can define the whole QA chain.


```python
from langchain.chains import RetrievalQA

llm = OpenAI()
qa = VectorDBQA.from_chain_type(
    llm=llm,
    chain_type="stuff",
    vectorstore=doc_store,
    return_source_documents=False,
)
```

## Search data

Once the data is put into AnalyticDB we can start asking some questions. A question will be automatically vectorized by OpenAI model, and the created vector will be used to find some possibly matching answers in AnalyticDB. Once retrieved, the most similar answers will be incorporated into the prompt sent to OpenAI Large Language Model.



```python
import random

random.seed(52)
selected_questions = random.choices(questions, k=5)
```


```python
for question in selected_questions:
    print(">", question)
    print(qa.run(question), end="\n\n")
```

    > where do frankenstein and the monster first meet
     Victor retreats into the mountains, and that is where the Creature finds him and pleads for Victor to hear his tale.
    
    > who are the actors in fast and furious
     The main cast of Fast & Furious includes Vin Diesel as Dominic Toretto, Paul Walker as Brian O'Conner, Michelle Rodriguez as Letty Ortiz, Jordana Brewster as Mia Toretto, Tyrese Gibson as Roman Pearce, and Ludacris as Tej Parker.
    
    > properties of red black tree in data structure
     The properties of a red-black tree in data structure are that each node is either red or black, the root is black, all leaves (NIL) are black, and if a node is red, then both its children are black. Additionally, every path from a given node to any of its descendant NIL nodes contains the same number of black nodes.
    
    > who designed the national coat of arms of south africa
     Iaan Bekker
    
    > caravaggio's death of the virgin pamela askew
     I don't know.


key: _node_content
string_value:
{"id_": "cd0684a2-6d3a-4365-a280-2c91738ccc01", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "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": "8ad6517f-8c3a-4060-ba7a-7cdccad68e98", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "30d78a1dbf2b0b4cfaca0f7dffef40e192fa56285d4a39b5bc286b7ba2dcf961", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "53aad4f3-2355-44c4-a070-ce3c776573f2", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "b2856fe41955d344fd5d2463a3c2a31e65f4b48e62ab79511ff09312dfa7e060", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "bbd4890c-fcd9-4ca9-bdf0-30e6cd05e1f1", "node_type": "1", "metadata": {}, "hash": "f767bc0fa77f0557356063d5c75229949db86aaa02dee23adda395d428f974d2", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 11857, "end_char_idx": 14690, "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

random.seed(52)
selected_questions = random.choices(questions, k=5)
```


```python
for question in selected_questions:
    print(">", question)
    print(qa.run(question), end="\n\n")
```

    > where do frankenstein and the monster first meet
     Victor retreats into the mountains, and that is where the Creature finds him and pleads for Victor to hear his tale.
    
    > who are the actors in fast and furious
     The main cast of Fast & Furious includes Vin Diesel as Dominic Toretto, Paul Walker as Brian O'Conner, Michelle Rodriguez as Letty Ortiz, Jordana Brewster as Mia Toretto, Tyrese Gibson as Roman Pearce, and Ludacris as Tej Parker.
    
    > properties of red black tree in data structure
     The properties of a red-black tree in data structure are that each node is either red or black, the root is black, all leaves (NIL) are black, and if a node is red, then both its children are black. Additionally, every path from a given node to any of its descendant NIL nodes contains the same number of black nodes.
    
    > who designed the national coat of arms of south africa
     Iaan Bekker
    
    > caravaggio's death of the virgin pamela askew
     I don't know.
    


### Custom prompt templates

The `stuff` chain type in Langchain uses a specific prompt with question and context documents incorporated. This is what the default prompt looks like:

```text
Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.
{context}
Question: {question}
Helpful Answer:
```

We can, however, provide our prompt template and change the behaviour of the OpenAI LLM, while still using the `stuff` chain type. It is important to keep `{context}` and `{question}` as placeholders.

#### Experimenting with custom prompts

We can try using a different prompt template, so the model:
1. Responds with a single-sentence answer if it knows it.
2. Suggests a random song title if it doesn't know the answer to our question.


```python
from langchain.prompts import PromptTemplate
custom_prompt = """
Use the following pieces of context to answer the question at the end. Please provide
a short single-sentence summary answer only. If you don't know the answer or if it's
not present in given context, don't try to make up an answer, but suggest me a random
unrelated song title I could listen to.
Context: {context}
Question: {question}
Helpful Answer:
"""

custom_prompt_template = PromptTemplate(
    template=custom_prompt, input_variables=["context", "question"]
)
```


```python
custom_qa = VectorDBQA.from_chain_type(
    llm=llm,
    chain_type="stuff",
    vectorstore=doc_store,
    return_source_documents=False,
    chain_type_kwargs={"prompt": custom_prompt_template},
)
```


key: _node_content
string_value:
{"id_": "bbd4890c-fcd9-4ca9-bdf0-30e6cd05e1f1", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "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": "8ad6517f-8c3a-4060-ba7a-7cdccad68e98", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "30d78a1dbf2b0b4cfaca0f7dffef40e192fa56285d4a39b5bc286b7ba2dcf961", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "cd0684a2-6d3a-4365-a280-2c91738ccc01", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/analyticdb/QA_with_Langchain_AnalyticDB_and_OpenAI.ipynb", "title": "Question Answering with Langchain, AnalyticDB and OpenAI", "tokens": 3679, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "a24e12620a29eb92e85c2d7cd63d236fab0af5fdc7c40f95af3debfc3a872363", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 13081, "end_char_idx": 15674, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
### Custom prompt templates

The `stuff` chain type in Langchain uses a specific prompt with question and context documents incorporated. This is what the default prompt looks like:

```text
Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.
{context}
Question: {question}
Helpful Answer:
```

We can, however, provide our prompt template and change the behaviour of the OpenAI LLM, while still using the `stuff` chain type. It is important to keep `{context}` and `{question}` as placeholders.

#### Experimenting with custom prompts

We can try using a different prompt template, so the model:
1. Responds with a single-sentence answer if it knows it.
2. Suggests a random song title if it doesn't know the answer to our question.


```python
from langchain.prompts import PromptTemplate
custom_prompt = """
Use the following pieces of context to answer the question at the end. Please provide
a short single-sentence summary answer only. If you don't know the answer or if it's
not present in given context, don't try to make up an answer, but suggest me a random
unrelated song title I could listen to.
Context: {context}
Question: {question}
Helpful Answer:
"""

custom_prompt_template = PromptTemplate(
    template=custom_prompt, input_variables=["context", "question"]
)
```


```python
custom_qa = VectorDBQA.from_chain_type(
    llm=llm,
    chain_type="stuff",
    vectorstore=doc_store,
    return_source_documents=False,
    chain_type_kwargs={"prompt": custom_prompt_template},
)
```


```python
random.seed(41)
for question in random.choices(questions, k=5):
    print(">", question)
    print(custom_qa.run(question), end="\n\n")
```

    > what was uncle jesse's original last name on full house
    Uncle Jesse's original last name on Full House was Cochran.
    
    > when did the volcano erupt in indonesia 2018
    No information about a volcano erupting in Indonesia in 2018 is present in the given context. Suggested song title: "Volcano" by U2.
    
    > what does a dualist way of thinking mean
    A dualist way of thinking means believing that humans possess a non-physical mind or soul which is distinct from their physical body.
    
    > the first civil service commission in india was set up on the basis of recommendation of
    The first Civil Service Commission in India was not set up on the basis of a recommendation.
    
    > how old do you have to be to get a tattoo in utah
    In Utah, you must be at least 18 years old to get a tattoo.


key: url
string_value:
https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/README.ipynb


key: title
string_value:
**[SingleStoreDB](https://singlestore.com)** has first-class support for vector search through our [Vector Functions](https://docs.singlestore.com/managed-service/en/reference/sql-reference/vector-functions.html). Our vector database subsystem, first made available in 2017 and subsequently enhanced, allows extremely fast nearest-neighbor search to find objects that are semantically similar, easily using SQL.


key: _node_content
string_value:
{"id_": "30403968-f496-4e49-91e0-f80c6a231b4b", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/README.ipynb", "title": "**[SingleStoreDB](https://singlestore.com)** has first-class support for vector search through our [Vector Functions](https://docs.singlestore.com/managed-service/en/reference/sql-reference/vector-functions.html). Our vector database subsystem, first made available in 2017 and subsequently enhanced, allows extremely fast nearest-neighbor search to find objects that are semantically similar, easily using SQL.", "tokens": 321, "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": "aa3d11ca-1b99-4339-a460-a247bb5961c9", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/README.ipynb", "title": "**[SingleStoreDB](https://singlestore.com)** has first-class support for vector search through our [Vector Functions](https://docs.singlestore.com/managed-service/en/reference/sql-reference/vector-functions.html). Our vector database subsystem, first made available in 2017 and subsequently enhanced, allows extremely fast nearest-neighbor search to find objects that are semantically similar, easily using SQL.", "tokens": 321, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "79f82c9947aeab8635b7e2357c5c1e7ee9d5e5b015eb134b3215f4181ddb2aea", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 0, "end_char_idx": 1587, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
**[SingleStoreDB](https://singlestore.com)** has first-class support for vector search through our [Vector Functions](https://docs.singlestore.com/managed-service/en/reference/sql-reference/vector-functions.html). Our vector database subsystem, first made available in 2017 and subsequently enhanced, allows extremely fast nearest-neighbor search to find objects that are semantically similar, easily using SQL.

SingleStoreDB supports vectors and vector similarity search using dot_product (for cosine similarity) and euclidean_distance functions. These functions are used by our customers for applications including face recognition, visual product photo search and text-based semantic search. With the explosion of generative AI technology, these capabilities form a firm foundation for text-based AI chatbots.

But remember, SingleStoreDB is a high-performance, scalable, modern SQL DBMS that supports multiple data models including structured data, semi-structured data based on JSON, time-series, full text, spatial, key-value and of course vector data. Start powering your next intelligent application with SingleStoreDB today!

![SingleStore Open AI](https://user-images.githubusercontent.com/8846480/236985121-48980956-fdc5-49c8-b006-f3a412142676.png)

## Example

This folder contains examples of using SingleStoreDB and OpenAI together. We will keep adding more scenarios so stay tuned!

| Name | Description |
| --- | --- |
| [OpenAI wikipedia semantic search](./OpenAI_wikipedia_semantic_search.ipynb) | Improve ChatGPT accuracy through SingleStoreDB semantic Search in QA |


key: url
string_value:
https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/OpenAI_wikipedia_semantic_search.ipynb


key: _node_content
string_value:
{"id_": "ba5fb8fe-43f1-4f10-8252-938eb2d26a45", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/OpenAI_wikipedia_semantic_search.ipynb", "title": "Intro", "tokens": 2377, "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": "209b9590-ccc6-458b-b899-2b2004d01d8b", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/OpenAI_wikipedia_semantic_search.ipynb", "title": "Intro", "tokens": 2377, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "91d5f416e2bfa709b2a86ac73f620a5ffb0a9f65cf5f978acbe3cb495008fc8c", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "4032282b-0585-484e-8719-fd005f200313", "node_type": "1", "metadata": {}, "hash": "d5c8f8e718c12d68584c4473cf5f11c3ff3ca39839e8477c72ec58b1c2b6663c", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 0, "end_char_idx": 2157, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
# Intro
This notebook is an example on how you can use SingleStoreDB vector storage and functions to build an interactive Q&A application with ChatGPT. If you start a [Trial](https://www.singlestore.com/cloud-trial/) in SingleStoreDB, you can find the same notebook in our sample notebooks with native connection.

## First let's talk directly to ChatGPT and try and get back a response


```python
!pip install openai --quiet

```

    
    [1m[[0m[34;49mnotice[0m[1;39;49m][0m[39;49m A new release of pip is available: [0m[31;49m23.0.1[0m[39;49m -> [0m[32;49m23.1.2[0m
    [1m[[0m[34;49mnotice[0m[1;39;49m][0m[39;49m To update, run: [0m[32;49mpython3.11 -m pip install --upgrade pip[0m



```python
import openai

EMBEDDING_MODEL = "text-embedding-3-small"
GPT_MODEL = "gpt-3.5-turbo"

```

## Let's connect to OpenAI and see the result we get when asking for a date beyond 2021


```python
openai.api_key = 'OPENAI API KEY'

response = openai.ChatCompletion.create(
  model=GPT_MODEL,
  messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Who won the gold medal for curling in Olymics 2022?"},
    ]
)

print(response['choices'][0]['message']['content'])

```

    I'm sorry, I cannot provide information about events that have not occurred yet. The Winter Olympics 2022 will be held in Beijing, China from February 4 to 20, 2022. The curling events will take place during this time and the results will not be known until after the competition has concluded.


# Get the data about Winter Olympics and provide the information to ChatGPT as context

## 1. Setup


```python
!pip install matplotlib plotly.express scikit-learn tabulate tiktoken wget --quiet

```

    
    [1m[[0m[34;49mnotice[0m[1;39;49m][0m[39;49m A new release of pip is available: [0m[31;49m23.0.1[0m[39;49m -> [0m[32;49m23.1.2[0m
    [1m[[0m[34;49mnotice[0m[1;39;49m][0m[39;49m To update, run: [0m[32;49mpython3.11 -m pip install --upgrade pip[0m



```python
import pandas as pd
import os
import wget
import ast

```

## Step 1 - Grab the data from CSV and prepare it


key: _node_content
string_value:
{"id_": "4032282b-0585-484e-8719-fd005f200313", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/OpenAI_wikipedia_semantic_search.ipynb", "title": "Intro", "tokens": 2377, "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": "209b9590-ccc6-458b-b899-2b2004d01d8b", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/OpenAI_wikipedia_semantic_search.ipynb", "title": "Intro", "tokens": 2377, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "91d5f416e2bfa709b2a86ac73f620a5ffb0a9f65cf5f978acbe3cb495008fc8c", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "ba5fb8fe-43f1-4f10-8252-938eb2d26a45", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/OpenAI_wikipedia_semantic_search.ipynb", "title": "Intro", "tokens": 2377, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "ba8e1a39e07c173c4ff88a33c446854e20b542bdcf2a65b87adf502c6bb7c876", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "ffbca25b-f33f-4c12-92af-773aec6e8758", "node_type": "1", "metadata": {}, "hash": "6868e16bae47ee695ce04c5f615a6c3deca324327212a018020fe4a5b5d0be68", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 907, "end_char_idx": 3527, "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_key = 'OPENAI API KEY'

response = openai.ChatCompletion.create(
  model=GPT_MODEL,
  messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Who won the gold medal for curling in Olymics 2022?"},
    ]
)

print(response['choices'][0]['message']['content'])

```

    I'm sorry, I cannot provide information about events that have not occurred yet. The Winter Olympics 2022 will be held in Beijing, China from February 4 to 20, 2022. The curling events will take place during this time and the results will not be known until after the competition has concluded.


# Get the data about Winter Olympics and provide the information to ChatGPT as context

## 1. Setup


```python
!pip install matplotlib plotly.express scikit-learn tabulate tiktoken wget --quiet

```

    
    [1m[[0m[34;49mnotice[0m[1;39;49m][0m[39;49m A new release of pip is available: [0m[31;49m23.0.1[0m[39;49m -> [0m[32;49m23.1.2[0m
    [1m[[0m[34;49mnotice[0m[1;39;49m][0m[39;49m To update, run: [0m[32;49mpython3.11 -m pip install --upgrade pip[0m



```python
import pandas as pd
import os
import wget
import ast

```

## Step 1 - Grab the data from CSV and prepare it


```python
# download pre-chunked text and pre-computed embeddings
# this file is ~200 MB, so may take a minute depending on your connection speed
embeddings_path = "https://cdn.openai.com/API/examples/data/winter_olympics_2022.csv"
file_path = "winter_olympics_2022.csv"

if not os.path.exists(file_path):
    wget.download(embeddings_path, file_path)
    print("File downloaded successfully.")
else:
    print("File already exists in the local file system.")

```

    File downloaded successfully.



```python
df = pd.read_csv(
    "winter_olympics_2022.csv"
)

# convert embeddings from CSV str type back to list type
df['embedding'] = df['embedding'].apply(ast.literal_eval)

```


```python
df

```


```python
df.info(show_counts=True)

```

    <class 'pandas.core.frame.DataFrame'>
    RangeIndex: 6059 entries, 0 to 6058
    Data columns (total 2 columns):
     #   Column     Non-Null Count  Dtype 
    ---  ------     --------------  ----- 
     0   text       6059 non-null   object
     1   embedding  6059 non-null   object
    dtypes: object(2)
    memory usage: 94.8+ KB


## 2. Set up SingleStore DB


```python
import singlestoredb as s2

conn = s2.connect("<user>:<Password>@<host>:3306/")

cur = conn.cursor()

```


```python
# Create database
stmt = """
    CREATE DATABASE IF NOT EXISTS winter_wikipedia2;
"""

cur.execute(stmt)

```




    1


key: _node_content
string_value:
{"id_": "ffbca25b-f33f-4c12-92af-773aec6e8758", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/OpenAI_wikipedia_semantic_search.ipynb", "title": "Intro", "tokens": 2377, "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": "209b9590-ccc6-458b-b899-2b2004d01d8b", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/OpenAI_wikipedia_semantic_search.ipynb", "title": "Intro", "tokens": 2377, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "91d5f416e2bfa709b2a86ac73f620a5ffb0a9f65cf5f978acbe3cb495008fc8c", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "4032282b-0585-484e-8719-fd005f200313", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/OpenAI_wikipedia_semantic_search.ipynb", "title": "Intro", "tokens": 2377, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "ee24ae9192fb5c4ece0a38d0d748495fc89f595aa6d01f0386a9ccf189a02f1e", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "0581d7d1-c887-4751-b0cd-a6f8703470fc", "node_type": "1", "metadata": {}, "hash": "a57a92669caa4650b0c77d82e9aff33c26a3b1a9c4fb0d5218a93768d82e006d", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 2160, "end_char_idx": 4670, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
# download pre-chunked text and pre-computed embeddings
# this file is ~200 MB, so may take a minute depending on your connection speed
embeddings_path = "https://cdn.openai.com/API/examples/data/winter_olympics_2022.csv"
file_path = "winter_olympics_2022.csv"

if not os.path.exists(file_path):
    wget.download(embeddings_path, file_path)
    print("File downloaded successfully.")
else:
    print("File already exists in the local file system.")

```

    File downloaded successfully.



```python
df = pd.read_csv(
    "winter_olympics_2022.csv"
)

# convert embeddings from CSV str type back to list type
df['embedding'] = df['embedding'].apply(ast.literal_eval)

```


```python
df

```


```python
df.info(show_counts=True)

```

    <class 'pandas.core.frame.DataFrame'>
    RangeIndex: 6059 entries, 0 to 6058
    Data columns (total 2 columns):
     #   Column     Non-Null Count  Dtype 
    ---  ------     --------------  ----- 
     0   text       6059 non-null   object
     1   embedding  6059 non-null   object
    dtypes: object(2)
    memory usage: 94.8+ KB


## 2. Set up SingleStore DB


```python
import singlestoredb as s2

conn = s2.connect("<user>:<Password>@<host>:3306/")

cur = conn.cursor()

```


```python
# Create database
stmt = """
    CREATE DATABASE IF NOT EXISTS winter_wikipedia2;
"""

cur.execute(stmt)

```




    1




```python
#create table
stmt = """
CREATE TABLE IF NOT EXISTS winter_wikipedia2.winter_olympics_2022 (
    id INT PRIMARY KEY,
    text TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci,
    embedding BLOB
);"""

cur.execute(stmt)

```




    0



## 3. Populate the Table with our dataframe df and use JSON_ARRAY_PACK to compact it


```python
%%time

# Prepare the statement
stmt = """
    INSERT INTO winter_wikipedia2.winter_olympics_2022 (
        id,
        text,
        embedding
    )
    VALUES (
        %s,
        %s,
        JSON_ARRAY_PACK_F64(%s)
    )
"""

# Convert the DataFrame to a NumPy record array
record_arr = df.to_records(index=True)

# Set the batch size
batch_size = 1000

# Iterate over the rows of the record array in batches
for i in range(0, len(record_arr), batch_size):
    batch = record_arr[i:i+batch_size]
    values = [(row[0], row[1], str(row[2])) for row in batch]
    cur.executemany(stmt, values)

```

    CPU times: user 8.79 s, sys: 4.63 s, total: 13.4 s
    Wall time: 11min 4s


## 4. Do a semantic search with the same question from above and use the response to send to OpenAI again


key: _node_content
string_value:
{"id_": "0581d7d1-c887-4751-b0cd-a6f8703470fc", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/OpenAI_wikipedia_semantic_search.ipynb", "title": "Intro", "tokens": 2377, "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": "209b9590-ccc6-458b-b899-2b2004d01d8b", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/OpenAI_wikipedia_semantic_search.ipynb", "title": "Intro", "tokens": 2377, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "91d5f416e2bfa709b2a86ac73f620a5ffb0a9f65cf5f978acbe3cb495008fc8c", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "ffbca25b-f33f-4c12-92af-773aec6e8758", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/OpenAI_wikipedia_semantic_search.ipynb", "title": "Intro", "tokens": 2377, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "a5128807c8968a20b71a1a97f858b38b8d6071dc0e17940fdc1ec589f0c6c3fb", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "89356ec5-244a-4661-9cf3-0aa77c3585c4", "node_type": "1", "metadata": {}, "hash": "ff28753621ec3841c246af62822f6e3ba40131e52fe48f564d0a96ec8163e6b1", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 3250, "end_char_idx": 6124, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
## 2. Set up SingleStore DB


```python
import singlestoredb as s2

conn = s2.connect("<user>:<Password>@<host>:3306/")

cur = conn.cursor()

```


```python
# Create database
stmt = """
    CREATE DATABASE IF NOT EXISTS winter_wikipedia2;
"""

cur.execute(stmt)

```




    1




```python
#create table
stmt = """
CREATE TABLE IF NOT EXISTS winter_wikipedia2.winter_olympics_2022 (
    id INT PRIMARY KEY,
    text TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci,
    embedding BLOB
);"""

cur.execute(stmt)

```




    0



## 3. Populate the Table with our dataframe df and use JSON_ARRAY_PACK to compact it


```python
%%time

# Prepare the statement
stmt = """
    INSERT INTO winter_wikipedia2.winter_olympics_2022 (
        id,
        text,
        embedding
    )
    VALUES (
        %s,
        %s,
        JSON_ARRAY_PACK_F64(%s)
    )
"""

# Convert the DataFrame to a NumPy record array
record_arr = df.to_records(index=True)

# Set the batch size
batch_size = 1000

# Iterate over the rows of the record array in batches
for i in range(0, len(record_arr), batch_size):
    batch = record_arr[i:i+batch_size]
    values = [(row[0], row[1], str(row[2])) for row in batch]
    cur.executemany(stmt, values)

```

    CPU times: user 8.79 s, sys: 4.63 s, total: 13.4 s
    Wall time: 11min 4s


## 4. Do a semantic search with the same question from above and use the response to send to OpenAI again



```python
from utils.embeddings_utils import get_embedding

def strings_ranked_by_relatedness(
    query: str,
    df: pd.DataFrame,
    relatedness_fn=lambda x, y: 1 - spatial.distance.cosine(x, y),
    top_n: int = 100
) -> tuple:
    """Returns a list of strings and relatednesses, sorted from most related to least."""

    # Get the embedding of the query.
    query_embedding_response = get_embedding(query, EMBEDDING_MODEL)

    # Create the SQL statement.
    stmt = """
        SELECT
            text,
            DOT_PRODUCT_F64(JSON_ARRAY_PACK_F64(%s), embedding) AS score
        FROM winter_wikipedia2.winter_olympics_2022
        ORDER BY score DESC
        LIMIT %s
    """

    # Execute the SQL statement.
    results = cur.execute(stmt, [str(query_embedding_response), top_n])

    # Fetch the results
    results = cur.fetchall()

    strings = []
    relatednesses = []

    for row in results:
        strings.append(row[0])
        relatednesses.append(row[1])

    # Return the results.
    return strings[:top_n], relatednesses[:top_n]

```


```python
from tabulate import tabulate

strings, relatednesses = strings_ranked_by_relatedness(
    "curling gold medal",
    df,
    top_n=5
)

for string, relatedness in zip(strings, relatednesses):
    print(f"{relatedness=:.3f}")
    print(tabulate([[string]], headers=['Result'], tablefmt='fancy_grid'))

```

## 5. Send the right context to ChatGPT for a more accurate answer


key: _node_content
string_value:
{"id_": "89356ec5-244a-4661-9cf3-0aa77c3585c4", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/OpenAI_wikipedia_semantic_search.ipynb", "title": "Intro", "tokens": 2377, "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": "209b9590-ccc6-458b-b899-2b2004d01d8b", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/OpenAI_wikipedia_semantic_search.ipynb", "title": "Intro", "tokens": 2377, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "91d5f416e2bfa709b2a86ac73f620a5ffb0a9f65cf5f978acbe3cb495008fc8c", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "0581d7d1-c887-4751-b0cd-a6f8703470fc", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/OpenAI_wikipedia_semantic_search.ipynb", "title": "Intro", "tokens": 2377, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "345d7c39796942407cd5e32de4f1e59d8aaf4f15913b7cce5f45f6b87f58e17f", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "cbc2e1de-7f02-40f0-b45f-c3827f41e7b4", "node_type": "1", "metadata": {}, "hash": "bed5d2b8ddc037a43392043dd3772f3118e411cc29019b0382c226efe677b2ce", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 4565, "end_char_idx": 7222, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
## 4. Do a semantic search with the same question from above and use the response to send to OpenAI again



```python
from utils.embeddings_utils import get_embedding

def strings_ranked_by_relatedness(
    query: str,
    df: pd.DataFrame,
    relatedness_fn=lambda x, y: 1 - spatial.distance.cosine(x, y),
    top_n: int = 100
) -> tuple:
    """Returns a list of strings and relatednesses, sorted from most related to least."""

    # Get the embedding of the query.
    query_embedding_response = get_embedding(query, EMBEDDING_MODEL)

    # Create the SQL statement.
    stmt = """
        SELECT
            text,
            DOT_PRODUCT_F64(JSON_ARRAY_PACK_F64(%s), embedding) AS score
        FROM winter_wikipedia2.winter_olympics_2022
        ORDER BY score DESC
        LIMIT %s
    """

    # Execute the SQL statement.
    results = cur.execute(stmt, [str(query_embedding_response), top_n])

    # Fetch the results
    results = cur.fetchall()

    strings = []
    relatednesses = []

    for row in results:
        strings.append(row[0])
        relatednesses.append(row[1])

    # Return the results.
    return strings[:top_n], relatednesses[:top_n]

```


```python
from tabulate import tabulate

strings, relatednesses = strings_ranked_by_relatedness(
    "curling gold medal",
    df,
    top_n=5
)

for string, relatedness in zip(strings, relatednesses):
    print(f"{relatedness=:.3f}")
    print(tabulate([[string]], headers=['Result'], tablefmt='fancy_grid'))

```

## 5. Send the right context to ChatGPT for a more accurate answer


```python
import tiktoken

def num_tokens(text: str, model: str = GPT_MODEL) -> int:
    """Return the number of tokens in a string."""
    encoding = tiktoken.encoding_for_model(model)
    return len(encoding.encode(text))


def query_message(
    query: str,
    df: pd.DataFrame,
    model: str,
    token_budget: int
) -> str:
    """Return a message for GPT, with relevant source texts pulled from SingleStoreDB."""
    strings, relatednesses = strings_ranked_by_relatedness(query, df, "winter_olympics_2022")
    introduction = 'Use the below articles on the 2022 Winter Olympics to answer the subsequent question. If the answer cannot be found in the articles, write "I could not find an answer."'
    question = f"\n\nQuestion: {query}"
    message = introduction
    for string in strings:
        next_article = f'\n\nWikipedia article section:\n"""\n{string}\n"""'
        if (
            num_tokens(message + next_article + question, model=model)
            > token_budget
        ):
            break
        else:
            message += next_article
    return message + question


key: _node_content
string_value:
{"id_": "cbc2e1de-7f02-40f0-b45f-c3827f41e7b4", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/OpenAI_wikipedia_semantic_search.ipynb", "title": "Intro", "tokens": 2377, "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": "209b9590-ccc6-458b-b899-2b2004d01d8b", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/OpenAI_wikipedia_semantic_search.ipynb", "title": "Intro", "tokens": 2377, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "91d5f416e2bfa709b2a86ac73f620a5ffb0a9f65cf5f978acbe3cb495008fc8c", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "89356ec5-244a-4661-9cf3-0aa77c3585c4", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/SingleStoreDB/OpenAI_wikipedia_semantic_search.ipynb", "title": "Intro", "tokens": 2377, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "52f83da85de6df9d1b9945915ca882f7a670cae4ac21a6f9f533e4a56094de39", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 5742, "end_char_idx": 8615, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
from tabulate import tabulate

strings, relatednesses = strings_ranked_by_relatedness(
    "curling gold medal",
    df,
    top_n=5
)

for string, relatedness in zip(strings, relatednesses):
    print(f"{relatedness=:.3f}")
    print(tabulate([[string]], headers=['Result'], tablefmt='fancy_grid'))

```

## 5. Send the right context to ChatGPT for a more accurate answer


```python
import tiktoken

def num_tokens(text: str, model: str = GPT_MODEL) -> int:
    """Return the number of tokens in a string."""
    encoding = tiktoken.encoding_for_model(model)
    return len(encoding.encode(text))


def query_message(
    query: str,
    df: pd.DataFrame,
    model: str,
    token_budget: int
) -> str:
    """Return a message for GPT, with relevant source texts pulled from SingleStoreDB."""
    strings, relatednesses = strings_ranked_by_relatedness(query, df, "winter_olympics_2022")
    introduction = 'Use the below articles on the 2022 Winter Olympics to answer the subsequent question. If the answer cannot be found in the articles, write "I could not find an answer."'
    question = f"\n\nQuestion: {query}"
    message = introduction
    for string in strings:
        next_article = f'\n\nWikipedia article section:\n"""\n{string}\n"""'
        if (
            num_tokens(message + next_article + question, model=model)
            > token_budget
        ):
            break
        else:
            message += next_article
    return message + question


def ask(
    query: str,
    df: pd.DataFrame = df,
    model: str = GPT_MODEL,
    token_budget: int = 4096 - 500,
    print_message: bool = False,
) -> str:
    """Answers a query using GPT and a table of relevant texts and embeddings in SingleStoreDB."""
    message = query_message(query, df, model=model, token_budget=token_budget)
    if print_message:
        print(message)
    messages = [
        {"role": "system", "content": "You answer questions about the 2022 Winter Olympics."},
        {"role": "user", "content": message},
    ]
    response = openai.ChatCompletion.create(
        model=model,
        messages=messages,
        temperature=0
    )
    response_message = response["choices"][0]["message"]["content"]
    return response_message

```

## 6. Get an answer from Chat GPT


```python
from pprint import pprint

answer = ask('Who won the gold medal for curling in Olymics 2022?')

pprint(answer)

```

    ("There were three curling events at the 2022 Winter Olympics: men's, women's, "
     'and mixed doubles. The gold medalists for each event are:\n'
     '\n'
     "- Men's: Sweden (Niklas Edin, Oskar Eriksson, Rasmus Wranå, Christoffer "
     'Sundgren, Daniel Magnusson)\n'
     "- Women's: Great Britain (Eve Muirhead, Vicky Wright, Jennifer Dodds, Hailey "
     'Duff, Mili Smith)\n'
     '- Mixed doubles: Italy (Stefania Constantini, Amos Mosaner)')


key: _node_content
string_value:
{"id_": "74a7df17-4f88-4c00-9ed5-046f88bef675", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/whisper.ipynb", "title": "Azure audio whisper (preview) example", "tokens": 982, "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": "da8871ce-bcd5-4091-a56f-7abd8dd173d2", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/whisper.ipynb", "title": "Azure audio whisper (preview) example", "tokens": 982, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "b15583ccbdfc78d4a6f6c05fff52b526446f92b3b76f5a3c8466e2bb73727fbf", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "0523a951-400b-4fb0-b667-8bb936bc5326", "node_type": "1", "metadata": {}, "hash": "57bc55f44a6a25af369c7278bf71eb19b27eaa309b97169e4cb41e7e670ec786", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 0, "end_char_idx": 3342, "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

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


## Setup

First, we install the necessary dependencies and import the libraries we will be using.


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


```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"]

    client = openai.AzureOpenAI(
        azure_endpoint=endpoint,
        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"]

    client = openai.AzureOpenAI(
        azure_endpoint=endpoint,
        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`


## Deployments

In this section we are going to create a deployment using the `whisper-1` model to transcribe audio files.

### Deployments: Create in the Azure OpenAI Studio
Let's deploy a model to use with whisper. Go to https://portal.azure.com, find your Azure OpenAI resource, and then navigate to the Azure OpenAI Studio. Click on the "Deployments" tab and then create a deployment for the model you want to use for whisper. The deployment name that you give the model will be used in the code below.


key: _node_content
string_value:
{"id_": "0523a951-400b-4fb0-b667-8bb936bc5326", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/whisper.ipynb", "title": "Azure audio whisper (preview) example", "tokens": 982, "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": "da8871ce-bcd5-4091-a56f-7abd8dd173d2", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/whisper.ipynb", "title": "Azure audio whisper (preview) example", "tokens": 982, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "b15583ccbdfc78d4a6f6c05fff52b526446f92b3b76f5a3c8466e2bb73727fbf", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "74a7df17-4f88-4c00-9ed5-046f88bef675", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/whisper.ipynb", "title": "Azure audio whisper (preview) example", "tokens": 982, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "a9677e94f9052213815900e6fc774339b43210ee91a336bf614606cdfba02ab9", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 2001, "end_char_idx": 4311, "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"]

    client = openai.AzureOpenAI(
        azure_endpoint=endpoint,
        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`


## Deployments

In this section we are going to create a deployment using the `whisper-1` model to transcribe audio files.

### Deployments: Create in the Azure OpenAI Studio
Let's deploy a model to use with whisper. Go to https://portal.azure.com, find your Azure OpenAI resource, and then navigate to the Azure OpenAI Studio. Click on the "Deployments" tab and then create a deployment for the model you want to use for whisper. The deployment name that you give the model will be used in the code below.


```python
deployment = "whisper-deployment" # Fill in the deployment name from the portal here
```

## 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 = client.audio.transcriptions.create(
    file=open("wikipediaOcelot.wav", "rb"),
    model=deployment,
)
print(transcription.text)
```


key: _node_content
string_value:
{"id_": "b6ce68c8-a066-412b-9f59-05e6cc619d6a", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/chat.ipynb", "title": "Azure chat completions example", "tokens": 1590, "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": "f2e6f485-1bfe-43db-a05d-e4e0833b3b60", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/chat.ipynb", "title": "Azure chat completions example", "tokens": 1590, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "f9d6013cb22f528b0782a20c29e90e8c4b6128538497f1c6d24a1a3cda22c884", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "26792a36-ad48-4625-80be-fcd03250dff9", "node_type": "1", "metadata": {}, "hash": "9509a12086c57c95c51bd6dfd8acc8f2b51ac55339e8e69854136c2f7c8a1dc0", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 0, "end_char_idx": 3344, "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

This example will cover chat completions using the Azure OpenAI service. It also includes information on content filtering.

## Setup

First, we install the necessary dependencies and import the libraries we will be using.


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


```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"]

    client = openai.AzureOpenAI(
        azure_endpoint=endpoint,
        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"]

    client = openai.AzureOpenAI(
        azure_endpoint=endpoint,
        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`


## Deployments

In this section we are going to create a deployment of a GPT model that we can use to create chat completions.

### Deployments: Create in the Azure OpenAI Studio
Let's deploy a model to use with chat completions. Go to https://portal.azure.com, find your Azure OpenAI resource, and then navigate to the Azure OpenAI Studio. Click on the "Deployments" tab and then create a deployment for the model you want to use for chat completions. The deployment name that you give the model will be used in the code below.


key: _node_content
string_value:
{"id_": "26792a36-ad48-4625-80be-fcd03250dff9", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/chat.ipynb", "title": "Azure chat completions example", "tokens": 1590, "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": "f2e6f485-1bfe-43db-a05d-e4e0833b3b60", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/chat.ipynb", "title": "Azure chat completions example", "tokens": 1590, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "f9d6013cb22f528b0782a20c29e90e8c4b6128538497f1c6d24a1a3cda22c884", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "b6ce68c8-a066-412b-9f59-05e6cc619d6a", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/chat.ipynb", "title": "Azure chat completions example", "tokens": 1590, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "fae7154852c903a495df80dea1b1def25aca2da0c3cd9e8e2670bbd531faa133", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "0d467cad-d7d3-43de-af9e-e8f786507d15", "node_type": "1", "metadata": {}, "hash": "540d3b88d334816fb891daa3d65776d1a0a5bc04a1a550a47a3392d845854c05", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 2030, "end_char_idx": 5375, "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"]

    client = openai.AzureOpenAI(
        azure_endpoint=endpoint,
        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`


## Deployments

In this section we are going to create a deployment of a GPT model that we can use to create chat completions.

### Deployments: Create in the Azure OpenAI Studio
Let's deploy a model to use with chat completions. Go to https://portal.azure.com, find your Azure OpenAI resource, and then navigate to the Azure OpenAI Studio. Click on the "Deployments" tab and then create a deployment for the model you want to use for chat completions. The deployment name that you give the model will be used in the code below.


```python
deployment = "" # Fill in the deployment name from the portal here
```

## Create chat completions

Now let's create a chat completion using the client we built.


```python
# For all possible arguments see https://platform.openai.com/docs/api-reference/chat-completions/create
response = client.chat.completions.create(
    model=deployment,
    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}")
```

### Create a streaming chat completion

We can also stream the response.


```python
response = client.chat.completions.create(
    model=deployment,
    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 delta.role:
            print(delta.role + ": ", end="", flush=True)
        if delta.content:
            print(delta.content, end="", flush=True)
```

### Content filtering

Azure OpenAI service includes content filtering of prompts and completion responses. You can learn more about content filtering and how to configure it [here](https://learn.microsoft.com/azure/ai-services/openai/concepts/content-filter).

If the prompt is flagged by the content filter, the library will raise a `BadRequestError` exception with a `content_filter` error code. Otherwise, you can access the `prompt_filter_results` and `content_filter_results` on the response to see the results of the content filtering and what categories were flagged.

#### Prompt flagged by content filter


key: _node_content
string_value:
{"id_": "0d467cad-d7d3-43de-af9e-e8f786507d15", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/chat.ipynb", "title": "Azure chat completions example", "tokens": 1590, "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": "f2e6f485-1bfe-43db-a05d-e4e0833b3b60", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/chat.ipynb", "title": "Azure chat completions example", "tokens": 1590, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "f9d6013cb22f528b0782a20c29e90e8c4b6128538497f1c6d24a1a3cda22c884", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "26792a36-ad48-4625-80be-fcd03250dff9", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/chat.ipynb", "title": "Azure chat completions example", "tokens": 1590, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "3e246abd0e6488f3287c626f9c0876f2202f70c115d26d9c71bee3e8902f9939", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 4138, "end_char_idx": 7123, "text_template": "{metadata_str}\n\n{content}", "metadata_template": "{key}: {value}", "metadata_seperator": "\n", "class_name": "TextNode"}


key: chroma:document
string_value:
```python
response = client.chat.completions.create(
    model=deployment,
    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 delta.role:
            print(delta.role + ": ", end="", flush=True)
        if delta.content:
            print(delta.content, end="", flush=True)
```

### Content filtering

Azure OpenAI service includes content filtering of prompts and completion responses. You can learn more about content filtering and how to configure it [here](https://learn.microsoft.com/azure/ai-services/openai/concepts/content-filter).

If the prompt is flagged by the content filter, the library will raise a `BadRequestError` exception with a `content_filter` error code. Otherwise, you can access the `prompt_filter_results` and `content_filter_results` on the response to see the results of the content filtering and what categories were flagged.

#### Prompt flagged by content filter


```python
import json

messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "<text violating the content policy>"}
]

try:
    completion = client.chat.completions.create(
        messages=messages,
        model=deployment,
    )
except openai.BadRequestError as e:
    err = json.loads(e.response.text)
    if err["error"]["code"] == "content_filter":
        print("Content filter triggered!")
        content_filter_result = err["error"]["innererror"]["content_filter_result"]
        for category, details in content_filter_result.items():
            print(f"{category}:\n filtered={details['filtered']}\n severity={details['severity']}")
```

### Checking the result of the content filter


```python
messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "What's the biggest city in Washington?"}
]

completion = client.chat.completions.create(
    messages=messages,
    model=deployment,
)
print(f"Answer: {completion.choices[0].message.content}")

# prompt content filter result in "model_extra" for azure
prompt_filter_result = completion.model_extra["prompt_filter_results"][0]["content_filter_results"]
print("\nPrompt content filter results:")
for category, details in prompt_filter_result.items():
    print(f"{category}:\n filtered={details['filtered']}\n severity={details['severity']}")

# completion content filter result
print("\nCompletion content filter results:")
completion_filter_result = completion.choices[0].model_extra["content_filter_results"]
for category, details in completion_filter_result.items():
    print(f"{category}:\n filtered={details['filtered']}\n severity={details['severity']}")
```


key: _node_content
string_value:
{"id_": "05613fa3-ca1e-4f34-b532-daaed7125425", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/embeddings.ipynb", "title": "Azure embeddings example", "tokens": 816, "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": "ea076bdf-d2ca-4c6a-acdc-68d6f9198e16", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/embeddings.ipynb", "title": "Azure embeddings example", "tokens": 816, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "6c05bd12f351f64d80b745e6523d74f3ef5d4ad5b3e889ddd6fceff1ac9e6eff", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "9005379c-3e7a-43f7-818c-ca80cdf28404", "node_type": "1", "metadata": {}, "hash": "fbd8f45a5a3061b55914161907a16ca286419e6595edc7dc7554805c507e8925", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 0, "end_char_idx": 3308, "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

This example will cover embeddings using the Azure OpenAI service.

## Setup

First, we install the necessary dependencies and import the libraries we will be using.


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


```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"]

    client = openai.AzureOpenAI(
        azure_endpoint=endpoint,
        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"]

    client = openai.AzureOpenAI(
        azure_endpoint=endpoint,
        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`


## Deployments

In this section we are going to create a deployment of a model that we can use to create embeddings.

### Deployments: Create in the Azure OpenAI Studio
Let's deploy a model to use with embeddings. Go to https://portal.azure.com, find your Azure OpenAI resource, and then navigate to the Azure OpenAI Studio. Click on the "Deployments" tab and then create a deployment for the model you want to use for embeddings. The deployment name that you give the model will be used in the code below.


key: _node_content
string_value:
{"id_": "9005379c-3e7a-43f7-818c-ca80cdf28404", "embedding": null, "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/embeddings.ipynb", "title": "Azure embeddings example", "tokens": 816, "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": "ea076bdf-d2ca-4c6a-acdc-68d6f9198e16", "node_type": "4", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/embeddings.ipynb", "title": "Azure embeddings example", "tokens": 816, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "6c05bd12f351f64d80b745e6523d74f3ef5d4ad5b3e889ddd6fceff1ac9e6eff", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "05613fa3-ca1e-4f34-b532-daaed7125425", "node_type": "1", "metadata": {"url": "https://github.com/openai/openai-cookbook/blob/main/examples/azure/embeddings.ipynb", "title": "Azure embeddings example", "tokens": 816, "retrieve_doc": true, "source": "OpenAI_Cookbooks"}, "hash": "16e014ed44e8bcba31f88ceb9aa1f87a5ae05d63b6cdbd00c27db352e3ab1c93", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 1967, "end_char_idx": 3645, "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"]

    client = openai.AzureOpenAI(
        azure_endpoint=endpoint,
        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`


## Deployments

In this section we are going to create a deployment of a model that we can use to create embeddings.

### Deployments: Create in the Azure OpenAI Studio
Let's deploy a model to use with embeddings. Go to https://portal.azure.com, find your Azure OpenAI resource, and then navigate to the Azure OpenAI Studio. Click on the "Deployments" tab and then create a deployment for the model you want to use for embeddings. The deployment name that you give the model will be used in the code below.


```python
deployment = "" # Fill in the deployment name from the portal here
```

## Embeddings

Now let's create embeddings using the client we built.


```python
embeddings = client.embeddings.create(
    model=deployment,
    input="The food was delicious and the waiter..."
)
                                
print(embeddings)
```


key: _node_content
string_value:
{"id_": "27097d1b-1cd2-46d8-abd8-ec042e7391cb", "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": "19ad7a29-a434-4494-88f5-d86bc804f863", "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": "72b3cbee7b26b1b7611a9155c7b99ef1e0c3456e8821edf500ccca73dc431baa", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "1668c5f5-12f2-48d1-8d11-20f16afd6fa3", "node_type": "1", "metadata": {}, "hash": "7f3fdbc7c63dcbb05852019f307d7636a7ebaf33392c485545571f75670347c4", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 0, "end_char_idx": 3264, "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

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 and import the libraries we will be using.


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


```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"]

    client = openai.AzureOpenAI(
        azure_endpoint=endpoint,
        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"]

    client = openai.AzureOpenAI(
        azure_endpoint=endpoint,
        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_": "1668c5f5-12f2-48d1-8d11-20f16afd6fa3", "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": "19ad7a29-a434-4494-88f5-d86bc804f863", "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": "72b3cbee7b26b1b7611a9155c7b99ef1e0c3456e8821edf500ccca73dc431baa", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "27097d1b-1cd2-46d8-abd8-ec042e7391cb", "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": "df99cd30a74a7aa8d7940e40ff9eecc2f8afbd35fa81cf624cb19cc45e0c9217", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "f6437fcf-25fe-4053-bc95-8fceb45c338d", "node_type": "1", "metadata": {}, "hash": "aca4adeab92c6a47a3a9f517693ef4ccee787d6a4ba39d21fa2aed5f016ddf13", "class_name": "RelatedNodeInfo"}}, "text": "", "mimetype": "text/plain", "start_char_idx": 2432, "end_char_idx": 5725, "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"]

    client = openai.AzureOpenAI(
        azure_endpoint=endpoint,
        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`


## Deployments

In this section we are going to create a deployment of a GPT model that we can use to call functions.

### Deployments: Create in the Azure OpenAI Studio
Let's deploy a model to use with chat completions. Go to https://portal.azure.com, find your Azure OpenAI resource, and then navigate to the Azure OpenAI Studio. Click on the "Deployments" tab and then create a deployment for the model you want to use for chat completions. The deployment name that you give the model will be used in the code below.


```python
deployment = "" # Fill in the deployment name from the portal here
```

## 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 "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.


key: _node_content
string_value:
{"id_": "f6437fcf-25fe-4053-bc95-8fceb45c338d", "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": "19ad7a29-a434-4494-88f5-d86bc804f863", "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": "72b3cbee7b26b1b7611a9155c7b99ef1e0c3456e8821edf500ccca73dc431baa", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "1668c5f5-12f2-48d1-8d11-20f16afd6fa3", "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": "9cc2f9739a269fa23dfab787d58a8b8d5a15611394b1d6865825471b7997ddac", "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_": "1d1730d4-1ca8-41d0-b207-9b7415e57f7d", "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": "a0a08c2d-8d4f-47ff-9382-47f2d2b0492a", "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": "f1e380ff9c9e64f52c1895a67ead7b78edd304c1f71d47513c7bf092668c737c", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "b69aeff1-bc1a-40b7-bb79-d4a128046521", "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_": "b69aeff1-bc1a-40b7-bb79-d4a128046521", "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": "a0a08c2d-8d4f-47ff-9382-47f2d2b0492a", "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": "f1e380ff9c9e64f52c1895a67ead7b78edd304c1f71d47513c7bf092668c737c", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "1d1730d4-1ca8-41d0-b207-9b7415e57f7d", "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": "914c149a0b18fbf8ea6233d4fce158b67597e6fb93637a6bf2c31784689816cc", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "6072f4db-d2a8-4d61-ace1-f09205a7549b", "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_": "6072f4db-d2a8-4d61-ace1-f09205a7549b", "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": "a0a08c2d-8d4f-47ff-9382-47f2d2b0492a", "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": "f1e380ff9c9e64f52c1895a67ead7b78edd304c1f71d47513c7bf092668c737c", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "b69aeff1-bc1a-40b7-bb79-d4a128046521", "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": "a89db9b908dfdf7a0cefee7e88e506f92ea74d974c35f45ab662da63821bc561", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "588c4f3f-ed4d-4ff5-96ad-48f20749a0a3", "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_": "588c4f3f-ed4d-4ff5-96ad-48f20749a0a3", "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": "a0a08c2d-8d4f-47ff-9382-47f2d2b0492a", "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": "f1e380ff9c9e64f52c1895a67ead7b78edd304c1f71d47513c7bf092668c737c", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "6072f4db-d2a8-4d61-ace1-f09205a7549b", "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": "4f07c818b678513167d9b1ffeed235cda1aab0f17ebd75776b1761819251e0eb", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "f305c594-b016-4612-a954-d78c4b1d96a3", "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_": "f305c594-b016-4612-a954-d78c4b1d96a3", "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": "a0a08c2d-8d4f-47ff-9382-47f2d2b0492a", "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": "f1e380ff9c9e64f52c1895a67ead7b78edd304c1f71d47513c7bf092668c737c", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "588c4f3f-ed4d-4ff5-96ad-48f20749a0a3", "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": "292d88fbe881a1d4330bcdf7b47bd1ce0be848de999acff11e33dd9c3d2468f5", "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_": "8062d7ba-a3cf-45f3-bf8c-cf9c2431180a", "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": "61554e7a-cc86-4e2c-859f-6bee14c3e4e0", "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": "37c7f5615acbb8560c3c0710ad3683ae5a6ccf5ca88dc2fc1dd75d26d0a4be9f", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "a3669b02-2a1f-4c34-9e5d-6bf4854fc535", "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_": "a3669b02-2a1f-4c34-9e5d-6bf4854fc535", "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": "61554e7a-cc86-4e2c-859f-6bee14c3e4e0", "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": "37c7f5615acbb8560c3c0710ad3683ae5a6ccf5ca88dc2fc1dd75d26d0a4be9f", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "8062d7ba-a3cf-45f3-bf8c-cf9c2431180a", "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": "dc135cc1a210fecd85c916daecaba3f916c655239d44540e577078fc2d2252c9", "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_": "1d5f393d-3f31-42e2-ac9e-ffa19867ad84", "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": "ab0ff572-cdf3-4d24-bf3c-6a291ea04fcd", "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": "850127d905b48f851b455300cb006c1f4311cf176cd0c7e13053bf99a9dc24e6", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "1fbc3d8f-ffbb-45b9-85c5-3b5de395a143", "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_": "1fbc3d8f-ffbb-45b9-85c5-3b5de395a143", "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": "ab0ff572-cdf3-4d24-bf3c-6a291ea04fcd", "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": "850127d905b48f851b455300cb006c1f4311cf176cd0c7e13053bf99a9dc24e6", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "1d5f393d-3f31-42e2-ac9e-ffa19867ad84", "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": "dfd7ab5c8c174e205092739f4bd28a46eed395b62711cae1b76a51307467965e", "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_": "f3382326-37e8-4fdf-a2a1-94dd6aed18a3", "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": "e97301f5-0e51-48ef-9e9e-7e4a5e00b576", "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": "e1c4991daa4a76678973dfcb004685491ee0d87a9935aa650e037fc8de0e4550", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "7b96def0-7cff-4674-9899-d0e1f3b623ff", "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_": "7b96def0-7cff-4674-9899-d0e1f3b623ff", "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": "e97301f5-0e51-48ef-9e9e-7e4a5e00b576", "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": "e1c4991daa4a76678973dfcb004685491ee0d87a9935aa650e037fc8de0e4550", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "f3382326-37e8-4fdf-a2a1-94dd6aed18a3", "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": "24ad5e467a24f8df6d0bf48c6f9693532690fc3ed801cd61d4403c9212dad211", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "f1a738e7-3c52-4d46-9233-4d90da6b783f", "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_": "f1a738e7-3c52-4d46-9233-4d90da6b783f", "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": "e97301f5-0e51-48ef-9e9e-7e4a5e00b576", "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": "e1c4991daa4a76678973dfcb004685491ee0d87a9935aa650e037fc8de0e4550", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "7b96def0-7cff-4674-9899-d0e1f3b623ff", "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": "c52b0f85151ca97d00a5ee6e5652cbb1ef4e4925c4e932561323003f26665d28", "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_": "47e6de14-6a2b-4d38-8fa8-bbfcb6d33bcb", "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": "11aa4b33-6764-4622-bf26-b929de40c366", "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": "e739b24f94c9ad60d14cbb36ff77fdecf2e9402a54c5f8cc5a478137c12a23fe", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "a8405316-d475-4a16-bb22-dadcdd289225", "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_": "a8405316-d475-4a16-bb22-dadcdd289225", "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": "11aa4b33-6764-4622-bf26-b929de40c366", "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": "e739b24f94c9ad60d14cbb36ff77fdecf2e9402a54c5f8cc5a478137c12a23fe", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "47e6de14-6a2b-4d38-8fa8-bbfcb6d33bcb", "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": "4a9e71c3acfe1df263cda5f52795532cbb9d9770448baa964927b37007cc5758", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "6ee90706-e2ed-43e6-86e4-e0dd03886be3", "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_": "6ee90706-e2ed-43e6-86e4-e0dd03886be3", "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": "11aa4b33-6764-4622-bf26-b929de40c366", "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": "e739b24f94c9ad60d14cbb36ff77fdecf2e9402a54c5f8cc5a478137c12a23fe", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "a8405316-d475-4a16-bb22-dadcdd289225", "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": "a81365422700da137eb69f1d7829e45e09a9dec547c101989b2182d98155fcf3", "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_": "cf1ce43f-0e2b-48f6-a57d-743990129120", "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": "a59f9535-2551-427a-9df6-142a919c1532", "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": "a64d293c5d372bde4fd48ffb5d5bf7c4915d303a930b737b99536ab3e2e7081c", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "2604a7da-7fb2-419d-ac50-45ed5dec5d5f", "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_": "2604a7da-7fb2-419d-ac50-45ed5dec5d5f", "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": "a59f9535-2551-427a-9df6-142a919c1532", "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": "a64d293c5d372bde4fd48ffb5d5bf7c4915d303a930b737b99536ab3e2e7081c", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "cf1ce43f-0e2b-48f6-a57d-743990129120", "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": "a931879270720944775f2a91f7c0098f2f9c2cee2e62b6d2cd064b4cf6cdceec", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "2c8923c9-f897-458b-97e8-ec5c6c124875", "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_": "2c8923c9-f897-458b-97e8-ec5c6c124875", "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": "a59f9535-2551-427a-9df6-142a919c1532", "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": "a64d293c5d372bde4fd48ffb5d5bf7c4915d303a930b737b99536ab3e2e7081c", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "2604a7da-7fb2-419d-ac50-45ed5dec5d5f", "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": "e4824005c79aac6fbeb8cea4ba7fd9dc66e99f6a9f2116ec55d4c35f05d87a51", "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_": "da83c696-787c-4d0c-b42f-5e3bd3fccaed", "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": "68824bc8-c4c1-466d-9de6-534fb461f1c6", "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": "7e7debb5cef7878f989720fdc7817614a73b8c67ed7d3ba8cc7d107d219cbd8e", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "ccc286d9-e635-4869-9e74-f72cb6f8cb42", "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_": "ccc286d9-e635-4869-9e74-f72cb6f8cb42", "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": "68824bc8-c4c1-466d-9de6-534fb461f1c6", "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": "7e7debb5cef7878f989720fdc7817614a73b8c67ed7d3ba8cc7d107d219cbd8e", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "da83c696-787c-4d0c-b42f-5e3bd3fccaed", "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": "5fc9e7d8513e2b302aba6ce81a160142cb417c3c404fb7c50bf7f45011767383", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "fc372180-462d-4fd4-94a6-29e8533eb9c5", "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_": "fc372180-462d-4fd4-94a6-29e8533eb9c5", "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": "68824bc8-c4c1-466d-9de6-534fb461f1c6", "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": "7e7debb5cef7878f989720fdc7817614a73b8c67ed7d3ba8cc7d107d219cbd8e", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "ccc286d9-e635-4869-9e74-f72cb6f8cb42", "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": "0b78f883fe845206c7546f8da8e3e0d60f459a916cec21d19f68e1cca861f56a", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "ae3ac786-3010-4bac-9745-d842dc25175f", "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_": "ae3ac786-3010-4bac-9745-d842dc25175f", "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": "68824bc8-c4c1-466d-9de6-534fb461f1c6", "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": "7e7debb5cef7878f989720fdc7817614a73b8c67ed7d3ba8cc7d107d219cbd8e", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "fc372180-462d-4fd4-94a6-29e8533eb9c5", "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": "f25eaf1490fb4916407364a146c0612760774e5fc610f4f301b97cca278af62e", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "94722239-b54a-4025-b5e3-233276fd55e1", "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_": "94722239-b54a-4025-b5e3-233276fd55e1", "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": "68824bc8-c4c1-466d-9de6-534fb461f1c6", "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": "7e7debb5cef7878f989720fdc7817614a73b8c67ed7d3ba8cc7d107d219cbd8e", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "ae3ac786-3010-4bac-9745-d842dc25175f", "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": "18c0f2dbee318b8ba0f49c3a1b0e9008583a96fb9efc6eba51903b71051be1fb", "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_": "7c58da77-bc6f-466b-a543-160642cb25dd", "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": "8a416323-9970-4bd9-8494-b6e70dc26de3", "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": "45efe248acff036fd32ca8bf274a616f2bb949b916b042f42ce70ce3d9a972f0", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "9fbe4aaa-68c0-482e-bb81-20e24d20e37a", "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_": "9fbe4aaa-68c0-482e-bb81-20e24d20e37a", "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": "8a416323-9970-4bd9-8494-b6e70dc26de3", "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": "45efe248acff036fd32ca8bf274a616f2bb949b916b042f42ce70ce3d9a972f0", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "7c58da77-bc6f-466b-a543-160642cb25dd", "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": "8429895381d96c71aa3f6af88d6eb9888d8e2bd5035adc2fd1b304b41a85a2b9", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "42dd4b17-a227-4d04-9634-7946c56653f6", "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_": "42dd4b17-a227-4d04-9634-7946c56653f6", "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": "8a416323-9970-4bd9-8494-b6e70dc26de3", "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": "45efe248acff036fd32ca8bf274a616f2bb949b916b042f42ce70ce3d9a972f0", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "9fbe4aaa-68c0-482e-bb81-20e24d20e37a", "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": "e3c2bbb6c028a22a002ab3a5db2773bc4b265fc927c545cdc3dd0ebdb0c4333a", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "4a9f89d7-da17-490c-bd97-330e9ecd41e3", "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_": "4a9f89d7-da17-490c-bd97-330e9ecd41e3", "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": "8a416323-9970-4bd9-8494-b6e70dc26de3", "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": "45efe248acff036fd32ca8bf274a616f2bb949b916b042f42ce70ce3d9a972f0", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "42dd4b17-a227-4d04-9634-7946c56653f6", "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": "0042dbb5409952fa1d21b717774eb79db92d41a8364790654248b918fb187cfe", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "5d8c6216-a9a6-4a14-97aa-5b4d5c4109c9", "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_": "5d8c6216-a9a6-4a14-97aa-5b4d5c4109c9", "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": "8a416323-9970-4bd9-8494-b6e70dc26de3", "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": "45efe248acff036fd32ca8bf274a616f2bb949b916b042f42ce70ce3d9a972f0", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "4a9f89d7-da17-490c-bd97-330e9ecd41e3", "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": "baca3b1176fc134b7d58844341a3be7eb3ae19a99784d311671fdc781fc26615", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "33561b03-4f57-47fa-af68-4cc3ae7d74fb", "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_": "33561b03-4f57-47fa-af68-4cc3ae7d74fb", "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": "8a416323-9970-4bd9-8494-b6e70dc26de3", "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": "45efe248acff036fd32ca8bf274a616f2bb949b916b042f42ce70ce3d9a972f0", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "5d8c6216-a9a6-4a14-97aa-5b4d5c4109c9", "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": "a3ce9a46b5405c89e3299a5c7decded79f7bc2e35b8001c7a4fd2dff9c46fca5", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "90e53513-51eb-4eb6-94a1-a76194be1acc", "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_": "90e53513-51eb-4eb6-94a1-a76194be1acc", "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": "8a416323-9970-4bd9-8494-b6e70dc26de3", "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": "45efe248acff036fd32ca8bf274a616f2bb949b916b042f42ce70ce3d9a972f0", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "33561b03-4f57-47fa-af68-4cc3ae7d74fb", "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": "2a32e0d3ef8893401ea306d3a55f7db1c5b8cc11694010ffc9225fdfdc7d0367", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "bf817c34-2b13-47fa-bc70-28cd221ce39a", "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_": "bf817c34-2b13-47fa-bc70-28cd221ce39a", "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": "8a416323-9970-4bd9-8494-b6e70dc26de3", "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": "45efe248acff036fd32ca8bf274a616f2bb949b916b042f42ce70ce3d9a972f0", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "90e53513-51eb-4eb6-94a1-a76194be1acc", "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": "cef0c84e6cf86ec28492405e6792d46154dd7f3c0516c241f24a356059a9f65f", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "aa19e958-70d0-4e7d-be3f-615d17583ddb", "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_": "aa19e958-70d0-4e7d-be3f-615d17583ddb", "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": "8a416323-9970-4bd9-8494-b6e70dc26de3", "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": "45efe248acff036fd32ca8bf274a616f2bb949b916b042f42ce70ce3d9a972f0", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "bf817c34-2b13-47fa-bc70-28cd221ce39a", "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": "4acb737234012803338ef9d747520fb6084c1b8e01a84bd855f1e185a27aa2da", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "196e0763-cbf4-47d7-b991-8a63a7761302", "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_": "196e0763-cbf4-47d7-b991-8a63a7761302", "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": "8a416323-9970-4bd9-8494-b6e70dc26de3", "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": "45efe248acff036fd32ca8bf274a616f2bb949b916b042f42ce70ce3d9a972f0", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "aa19e958-70d0-4e7d-be3f-615d17583ddb", "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": "7014fecb1a5fb758844430468d7b68bf63208cd2be3d2600a0ab7710818095d3", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "679c2369-0b87-4e2a-b410-7ca99833322b", "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_": "679c2369-0b87-4e2a-b410-7ca99833322b", "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": "8a416323-9970-4bd9-8494-b6e70dc26de3", "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": "45efe248acff036fd32ca8bf274a616f2bb949b916b042f42ce70ce3d9a972f0", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "196e0763-cbf4-47d7-b991-8a63a7761302", "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": "00593b6246d27531f59a39580804bc12255c1953fa3d980d43091ad086fe3794", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "c6da88a8-3f6e-4d28-851b-dcd295a349db", "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_": "c6da88a8-3f6e-4d28-851b-dcd295a349db", "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": "8a416323-9970-4bd9-8494-b6e70dc26de3", "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": "45efe248acff036fd32ca8bf274a616f2bb949b916b042f42ce70ce3d9a972f0", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "679c2369-0b87-4e2a-b410-7ca99833322b", "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": "7361747f4660ee85c249d17d742bb7160a04ca2b2f638c4c8b6213077f993f98", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "146a1dd0-9dea-4ec3-846d-f6f767485ccc", "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_": "146a1dd0-9dea-4ec3-846d-f6f767485ccc", "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": "8a416323-9970-4bd9-8494-b6e70dc26de3", "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": "45efe248acff036fd32ca8bf274a616f2bb949b916b042f42ce70ce3d9a972f0", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "c6da88a8-3f6e-4d28-851b-dcd295a349db", "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": "cd5f53380cce4334b1cde236f42cc9d848c3730b286e4d80443af61174d069ff", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "676909cb-6f06-455d-8edb-298195192d22", "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_": "676909cb-6f06-455d-8edb-298195192d22", "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": "8a416323-9970-4bd9-8494-b6e70dc26de3", "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": "45efe248acff036fd32ca8bf274a616f2bb949b916b042f42ce70ce3d9a972f0", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "146a1dd0-9dea-4ec3-846d-f6f767485ccc", "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": "02478c950afad54f966e7ba1c139891f5e109f036dce11caa3db629bb006ce37", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "63eb6346-a1e8-4375-b60b-0698813371eb", "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_": "63eb6346-a1e8-4375-b60b-0698813371eb", "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": "8a416323-9970-4bd9-8494-b6e70dc26de3", "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": "45efe248acff036fd32ca8bf274a616f2bb949b916b042f42ce70ce3d9a972f0", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "676909cb-6f06-455d-8edb-298195192d22", "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": "a9b41c569e32af5df5fa513ec7617c9870a7a696379292ed7018ebe84250b25e", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "22a605c5-9fc8-4e27-a2be-819b37a68d6d", "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_": "22a605c5-9fc8-4e27-a2be-819b37a68d6d", "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": "8a416323-9970-4bd9-8494-b6e70dc26de3", "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": "45efe248acff036fd32ca8bf274a616f2bb949b916b042f42ce70ce3d9a972f0", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "63eb6346-a1e8-4375-b60b-0698813371eb", "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": "b52a71ef19077e3c7603876d21cfaead6f2b0d94b71f34b7d6c9c40f9223ebdd", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "d242ebae-2e78-4e4c-b1cb-dc3536abc668", "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_": "d242ebae-2e78-4e4c-b1cb-dc3536abc668", "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": "8a416323-9970-4bd9-8494-b6e70dc26de3", "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": "45efe248acff036fd32ca8bf274a616f2bb949b916b042f42ce70ce3d9a972f0", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "22a605c5-9fc8-4e27-a2be-819b37a68d6d", "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": "58888cacfcdca0cc08e80f54b672cc35d2daf1b695bc90f3fd3d4ed4b737cd4d", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "adbbfa90-8f5c-48b7-9752-321c88028f25", "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_": "adbbfa90-8f5c-48b7-9752-321c88028f25", "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": "8a416323-9970-4bd9-8494-b6e70dc26de3", "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": "45efe248acff036fd32ca8bf274a616f2bb949b916b042f42ce70ce3d9a972f0", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "d242ebae-2e78-4e4c-b1cb-dc3536abc668", "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": "339af496b7f21702599435ea68dc99f3d56e4804757c442ad131f42aa7c78287", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "3e6a5d90-ca25-4373-a031-38fba6f2674e", "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_": "3e6a5d90-ca25-4373-a031-38fba6f2674e", "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": "8a416323-9970-4bd9-8494-b6e70dc26de3", "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": "45efe248acff036fd32ca8bf274a616f2bb949b916b042f42ce70ce3d9a972f0", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "adbbfa90-8f5c-48b7-9752-321c88028f25", "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": "bd17b314877e9cdab305a11bbe5e5ec75e78cc76d1536986e5b364c79ab21713", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "499c6b84-a905-4699-8422-b3de18989e3c", "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_": "499c6b84-a905-4699-8422-b3de18989e3c", "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": "8a416323-9970-4bd9-8494-b6e70dc26de3", "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": "45efe248acff036fd32ca8bf274a616f2bb949b916b042f42ce70ce3d9a972f0", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "3e6a5d90-ca25-4373-a031-38fba6f2674e", "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": "f7d2955fa26a821454118cf2604fc15c4f5d9f9377372faa864dea98b17b614d", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "9baf1e94-bfc3-4d8e-b4b0-3da1f41493ec", "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_": "9baf1e94-bfc3-4d8e-b4b0-3da1f41493ec", "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": "8a416323-9970-4bd9-8494-b6e70dc26de3", "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": "45efe248acff036fd32ca8bf274a616f2bb949b916b042f42ce70ce3d9a972f0", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "499c6b84-a905-4699-8422-b3de18989e3c", "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": "c9f2645ece21e96e3d47a8d9c9f0ce0861a588f282ba6319e9ccd1eef661890a", "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_": "fc655a19-70b9-4d15-b35a-e83b79d5ad88", "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": "98599bee-0fa7-41f3-8a4a-714499130127", "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": "9c91bf3d2ea53be7cc51dca3df0981f4f1393c30f032ee61d72223235e01dd67", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "87496997-7b1a-4ff3-942d-07efc55394a7", "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_": "87496997-7b1a-4ff3-942d-07efc55394a7", "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": "98599bee-0fa7-41f3-8a4a-714499130127", "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": "9c91bf3d2ea53be7cc51dca3df0981f4f1393c30f032ee61d72223235e01dd67", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "fc655a19-70b9-4d15-b35a-e83b79d5ad88", "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": "54d9fbcaecac05b58e73f6fbcf2759b00b45f0afb7829eb239088bb0879b9d19", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "63c68992-ad9a-4543-8afd-baba6a7f86fd", "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_": "63c68992-ad9a-4543-8afd-baba6a7f86fd", "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": "98599bee-0fa7-41f3-8a4a-714499130127", "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": "9c91bf3d2ea53be7cc51dca3df0981f4f1393c30f032ee61d72223235e01dd67", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "87496997-7b1a-4ff3-942d-07efc55394a7", "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": "2ebae210bb4c865a0010fffab3752dbb3ec7aeb8a61e7a47209f63838c9a8e75", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "3dabe3a7-fe89-4094-ad4b-307b2b12f0b4", "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_": "3dabe3a7-fe89-4094-ad4b-307b2b12f0b4", "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": "98599bee-0fa7-41f3-8a4a-714499130127", "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": "9c91bf3d2ea53be7cc51dca3df0981f4f1393c30f032ee61d72223235e01dd67", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "63c68992-ad9a-4543-8afd-baba6a7f86fd", "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": "6fe5ceb09c3629d441c56b9652330ab692a51f3e3e2662b2220818ebd07992fe", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "7dbb06af-48a3-4347-a5e5-ebb5366ccefb", "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_": "7dbb06af-48a3-4347-a5e5-ebb5366ccefb", "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": "98599bee-0fa7-41f3-8a4a-714499130127", "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": "9c91bf3d2ea53be7cc51dca3df0981f4f1393c30f032ee61d72223235e01dd67", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "3dabe3a7-fe89-4094-ad4b-307b2b12f0b4", "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": "bd72d5f4cf20a46191edb0259a94623cde612b11ca94e49c7648d65181ab4700", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "c29b2d3a-926a-4280-879c-0dc9318a5943", "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_": "c29b2d3a-926a-4280-879c-0dc9318a5943", "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": "98599bee-0fa7-41f3-8a4a-714499130127", "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": "9c91bf3d2ea53be7cc51dca3df0981f4f1393c30f032ee61d72223235e01dd67", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "7dbb06af-48a3-4347-a5e5-ebb5366ccefb", "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": "0cec2a5ccddf4b7631187c1972abc99ff1570142fee847acc670de21a655012a", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "81b3f483-c225-48b1-90c2-2f3ebeb511aa", "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_": "81b3f483-c225-48b1-90c2-2f3ebeb511aa", "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": "98599bee-0fa7-41f3-8a4a-714499130127", "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": "9c91bf3d2ea53be7cc51dca3df0981f4f1393c30f032ee61d72223235e01dd67", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "c29b2d3a-926a-4280-879c-0dc9318a5943", "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": "cee132f99b49de5dc8b9ed78d20304af1d0b8be47582bc38696f196e978db1ab", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "d9df1f0c-2bed-49dd-a16c-a0dd02c25d64", "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_": "d9df1f0c-2bed-49dd-a16c-a0dd02c25d64", "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": "98599bee-0fa7-41f3-8a4a-714499130127", "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": "9c91bf3d2ea53be7cc51dca3df0981f4f1393c30f032ee61d72223235e01dd67", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "81b3f483-c225-48b1-90c2-2f3ebeb511aa", "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": "bd899fa217ec89b779a82e64b72d52aa41c224a7857446dac66a57f7682d2744", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "e4e247c1-22d1-4907-aef1-4732274a2f00", "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_": "e4e247c1-22d1-4907-aef1-4732274a2f00", "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": "98599bee-0fa7-41f3-8a4a-714499130127", "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": "9c91bf3d2ea53be7cc51dca3df0981f4f1393c30f032ee61d72223235e01dd67", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "d9df1f0c-2bed-49dd-a16c-a0dd02c25d64", "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": "ed5029c5289564d9dbbfd7ab33bb5ed3c37eab0be55ff6ecd2589aff521f36b1", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "68be47b5-81e0-446b-a38f-f91c8e7ef831", "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_": "68be47b5-81e0-446b-a38f-f91c8e7ef831", "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": "98599bee-0fa7-41f3-8a4a-714499130127", "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": "9c91bf3d2ea53be7cc51dca3df0981f4f1393c30f032ee61d72223235e01dd67", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "e4e247c1-22d1-4907-aef1-4732274a2f00", "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": "64d43718b56745fca00cffb612de77a684090b7cfa12a430c9ed721ebf86d3a7", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "c1187ecc-7dc8-45b6-a0a8-c88aea7b7344", "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_": "c1187ecc-7dc8-45b6-a0a8-c88aea7b7344", "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": "98599bee-0fa7-41f3-8a4a-714499130127", "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": "9c91bf3d2ea53be7cc51dca3df0981f4f1393c30f032ee61d72223235e01dd67", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "68be47b5-81e0-446b-a38f-f91c8e7ef831", "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": "4736cfe456685e81e7173c6b8c95019b9290f1965b39f052cd9086a64871d2df", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "979fabee-763f-4405-b69e-2f131d24a945", "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_": "979fabee-763f-4405-b69e-2f131d24a945", "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": "98599bee-0fa7-41f3-8a4a-714499130127", "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": "9c91bf3d2ea53be7cc51dca3df0981f4f1393c30f032ee61d72223235e01dd67", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "c1187ecc-7dc8-45b6-a0a8-c88aea7b7344", "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": "f90a7384e9345b6fa49056e2daa43adeae907d5682068ef11faf9b649ef071d6", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "0af41682-cfb6-4fe4-acd7-8a9efc844ca1", "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_": "0af41682-cfb6-4fe4-acd7-8a9efc844ca1", "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": "98599bee-0fa7-41f3-8a4a-714499130127", "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": "9c91bf3d2ea53be7cc51dca3df0981f4f1393c30f032ee61d72223235e01dd67", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "979fabee-763f-4405-b69e-2f131d24a945", "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": "c3bec43d464629304b183030e13a80fb68f611c32c4d5dc7cb9cd717b70d10eb", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "e60deac8-d096-4d52-9524-aa810516483f", "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_": "e60deac8-d096-4d52-9524-aa810516483f", "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": "98599bee-0fa7-41f3-8a4a-714499130127", "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": "9c91bf3d2ea53be7cc51dca3df0981f4f1393c30f032ee61d72223235e01dd67", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "0af41682-cfb6-4fe4-acd7-8a9efc844ca1", "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": "a2ca91a95141c49bad54b8ea1b80e499096b55a8ec26746d0683d97c175d2246", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "b55c7afe-0b9d-41b6-801c-de919ba3cdc0", "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_": "b55c7afe-0b9d-41b6-801c-de919ba3cdc0", "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": "98599bee-0fa7-41f3-8a4a-714499130127", "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": "9c91bf3d2ea53be7cc51dca3df0981f4f1393c30f032ee61d72223235e01dd67", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "e60deac8-d096-4d52-9524-aa810516483f", "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": "e905f09fa55356301648850da1648a6ece0699c64e6933663f7df8ca32bee9c3", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "3f835f47-6546-41d1-b2df-6a871daad1b2", "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_": "3f835f47-6546-41d1-b2df-6a871daad1b2", "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": "98599bee-0fa7-41f3-8a4a-714499130127", "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": "9c91bf3d2ea53be7cc51dca3df0981f4f1393c30f032ee61d72223235e01dd67", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "b55c7afe-0b9d-41b6-801c-de919ba3cdc0", "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": "2dcb093194ffa4e5b90988995094472a58abe80f1fe0b251b06e9f81077665cb", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "2ca06ff4-b345-45c0-bb77-7d89e1be3b07", "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_": "2ca06ff4-b345-45c0-bb77-7d89e1be3b07", "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": "98599bee-0fa7-41f3-8a4a-714499130127", "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": "9c91bf3d2ea53be7cc51dca3df0981f4f1393c30f032ee61d72223235e01dd67", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "3f835f47-6546-41d1-b2df-6a871daad1b2", "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": "e17f6804bfc332c2229d7c8f71b2140e33eab97b22316a83d6d113838d458a8c", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "89bcdacf-dbbe-43f3-b776-8d871057112d", "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_": "89bcdacf-dbbe-43f3-b776-8d871057112d", "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": "98599bee-0fa7-41f3-8a4a-714499130127", "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": "9c91bf3d2ea53be7cc51dca3df0981f4f1393c30f032ee61d72223235e01dd67", "class_name": "RelatedNodeInfo"}, "2": {"node_id": "2ca06ff4-b345-45c0-bb77-7d89e1be3b07", "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": "3cebadde37f19e9ba26b5e51b2ff0bacc5e555a5f81148ce6d27b418c6c64cdd", "class_name": "RelatedNodeInfo"}, "3": {"node_id": "b2ac6c3e-c718-4ec0-8d00-c2f02339f689", "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"}


