Quick Start - Retrieval

Updated as of 2023-Jun-24

In this example, we will upload documents to a vector database so our AI chatbot can use the information. We will be uploading information from the NCBI StatPearls article on Acute Bronchitis.

1. Add your OpenAI API key

  • Go to the Dashboard and submit your OpenAI API Key.

2. Get an API Key

3. Create a chat

  • If you have already created a chat in Quick Start - Memory, skip this step.

  • Chats are used to organize conversational memory.

import requests

YOUR_DOPPLERAI_API_KEY = 'sk-...' # Replace with your API Key
headers = {'Authorization': f'Bearer {YOUR_DOPPLERAI_API_KEY}'}

# Create chat
def create_chat(headers, chat_data):
    chats_url = f'https://api.dopplerai.com/v1/chats'
    response = requests.post(chats_url, headers=headers, json=chat_data)
    return response.json()

# The reference_id and name are optional.
# If you don't need them, pass in an empty hash: chat_data = {}
chat_data = {
  "": "ac52d6a60a3b489683fd6cfda6e4abcd",
  "": "Example Labs Ltd"
}

response = create_chat(headers=headers, chat_data=chat_data)

print(response)
# {'reference_id': 'ac52d6a60a3b489683fd6cfda6e4abcd', 'name': 'Example Labs Ltd', 'uuid': 'ad289875-c446-4799-a017-14cda80d18c1', 'created_at': '2023-06-24T22:24:00.447256'}

4. Create a collection

  • Information uploaded to the vector database are organized into collections.

import requests

YOUR_DOPPLERAI_API_KEY = 'sk-...'
headers = {'Authorization': f'Bearer {YOUR_DOPPLERAI_API_KEY}'}

def create_collection(headers, collection_data):
    collections_url = 'https://api.dopplerai.com/v1/collections'
    response = requests.post(collections_url, headers=headers, json=collection_data)
    return response.json()

collection_data = {
    "reference_id": "fb700444788d430bb39b148447af7521",
    "name": "reports"
}

response = create_collection(headers=headers, collection_data=collection_data)

print(response)
# {'name': 'reports', 'reference_id': 'fb700444788d430bb39b148447af7521', 'uuid': 'e3aa752c-ede3-4c45-8088-67f0671bfc19', 'created_at': '2023-06-25T00:51:31.526000'}

5. Upload an article by URL

  • DopplerAI provides 2 API endpoints for uploading information to the vector database.

    • /uploads/text lets you upload a string.

    • /uploads/url lets you upload the contents from an URL.

      • DopplerAI will automatically detect whether the URL is an image, PDF or a regular webpage.

      • If it's an image, the text from the image will be extracted and uploaded to the vector database. Max file size is 1MB.

      • If it's a PDF, the text from the PDF will be extracted. Max file size is 1MB.

      • If it's a webpage, the webpage's HTML will be extracted.

  • This endpoint uses text-embedding-ada-002 for embeddings. If you want to a different model, or your own fine-tuned model, please just let us know on Discord or via an email to [email protected].

import requests

YOUR_DOPPLERAI_API_KEY = 'sk-...'
headers = {'Authorization': f'Bearer {YOUR_DOPPLERAI_API_KEY}'}

def upload_url(headers, upload_data):
    uploads_url = 'https://api.dopplerai.com/v1/uploads/url'
    response = requests.post(uploads_url, headers=headers, json=upload_data)
    return response.json()

upload_data = {
    "name": "Acute Bronchitis",
    "path": 'https://www.ncbi.nlm.nih.gov/books/NBK448067',
    "collection_uuid": "e3aa752c-ede3-4c45-8088-67f0671bfc19"
}

response = upload_url(headers=headers, upload_data=upload_data)

print(response)
# {'name': 'Acute Bronchitis', 'uuid': '237cc0e4-f7c5-4ab1-bc07-8fc849c0d5c3', 'created_at': '2023-06-25T02:13:53.120084', 'collection_uuid': 'e3aa752c-ede3-4c45-8088-67f0671bfc19', 'content': 'Warning: The NCBI web site requires JavaScript to function. more... ...', 'info': 'Added upload to vector database'}

6. Upload an article by text

  • Upload a string to the vector database.

import requests

YOUR_DOPPLERAI_API_KEY = 'sk-...'
headers = {'Authorization': f'Bearer {YOUR_DOPPLERAI_API_KEY}'}

def upload_text(headers, upload_data):
    uploads_url = 'https://api.dopplerai.com/v1/uploads/text'
    response = requests.post(uploads_url, headers=headers, json=upload_data)
    return response.json()

content = """
Introduction
Acoustic neuroma is also called vestibular schwannoma (VS), acoustic neurinoma, vestibular neuroma or acoustic neurofibroma. These are tumors that evolve from the Schwann cell sheath and can be either intracranial or extra-axial. They usually occur adjacent to the cochlear and vestibular nerves and most often arise from the inferior division of the latter. Anatomically, acoustic neuroma tends to occupy the cerebellopontine angle. About 5-10% of cerebellopontine angle (CPA) tumors are meningiomas and may occur elsewhere in the brain. Bilateral acoustic neuromas tend to be exclusively found in individuals with type 2 neurofibromatosis.[1][2][3]

Etiology
Bilateral acoustic neuromas may be a part of neurofibromatosis type 2 occurring due to a defect on chromosome 22q12.2 at the location of the neurofibromin 2 gene which encodes merlin protein. Studies have shown that acoustic neuroma had a causative predisposition mutation. Radiation exposure may predispose a patient to the development of that condition as well.[4] Even though mobile phone radiation has been of concern, several studies have failed to prove its causative effect on vestibular schwannomas.[5]

Epidemiology
Approximately 8% of all intracranial tumors which manifest clinically are attributed to schwannomas. The majority of acoustic neuromas are unilateral and sporadic. Bilateral acoustic neuromas are in fact genetic and constitute a bit less than 5% of all schwannomas. Acoustic neuromas in general, tend to present between the fourth to sixth decades of life. Acoustic neuromas developing in individuals with neurofibromatosis type 2 (NF II) are likely to present earlier, with a peak incidence around the third decade of life. Although rare, acoustic schwannomas can occur in children. There is a small female preponderance with an aggravation of problems during pregnancy. The hereditary acoustic neuroma occurs in NF II much more often compared to neurofibromatosis type 1 (NF I), although the latter is much more common. Unilateral acoustic neuroma has been reported exclusively in 24% of cases with NF I, while bilateral acoustic schwannoma is a hallmark of NF II. Both these conditions are autosomal dominant. The defective genetic locus has been localized to chromosome 17 in NF I and chromosome 22 in NF II.[6][7]
"""

upload_data = {
    "name": "Acoustic Neuroma",
    "content": content,
    "collection_uuid": "e3aa752c-ede3-4c45-8088-67f0671bfc19"
}

response = upload_text(headers=headers, upload_data=upload_data)

print(response)
# {'name': 'Acoustic Neuroma', 'uuid': '1d8fd24f-26f6-4e77-a7fd-84465cef55be', 'created_at': '2023-06-25T02:27:47.877907', 'collection_uuid': 'e3aa752c-ede3-4c45-8088-67f0671bfc19', 'content': 'Introduction ...', 'info': 'Added upload to vector database'}

7. Send a message with retrieval enabled

  • Send a message, allowing the LLM to access information from the uploaded articles.

import requests

YOUR_DOPPLERAI_API_KEY = 'sk-...'
headers = {'Authorization': f'Bearer {YOUR_DOPPLERAI_API_KEY}'}

# Send message
def send_message(headers, message_data):
    messages_url = f'https://api.dopplerai.com/v1/messages'
    response = requests.post(messages_url, headers=headers, json=message_data)
    return response.json()

chat_uuid = "ad289875-c446-4799-a017-14cda80d18c1"

message_data = {
  "message": {
    "prompt": "Answer the following question:",
    "content": "What is the association bewteen acoustic neuroma and neurofibromatosis?",
    "chat_uuid": chat_uuid
  },
  "large_language_model": {
    "model_name": "gpt-3.5-turbo",
    "temperature": 0,
    "frequency_penalty": 0.7
  },
  "memory": {
    "memorize": False,
    "remember": False
  },
  "vector_database": {
    "collection_uuid": "e3aa752c-ede3-4c45-8088-67f0671bfc19",
    "": {},
    "": 3,
    "": "Use the following information to answer the question. If you don't know the answer, just say that you don't know, don't try to make up an answer."
    }
}

response = send_message(headers=headers, message_data=message_data)

print(response)
# { "completion":{ "id":"chatcmpl-7VA001CYpUMMrhGdSD6oyAIGcEHby", "object":"chat.completion", "created":1687660960, "model":"gpt-3.5-turbo-0301", "choices":[ { "index":0, "message":{ "role":"assistant", "content":"Acoustic neuromas may be a part of neurofibromatosis type 2 occurring due to a defect on chromosome 22q12.2 at the location of the neurofibromin 2 gene which encodes merlin protein. The hereditary acoustic neuroma occurs in NF II much more often compared to neurofibromatosis type 1 (NF I), although the latter is much more common. Unilateral acoustic neuroma has been reported exclusively in 24% of cases with NF I, while bilateral acoustic schwannoma is a hallmark of NF II. Both these conditions are autosomal dominant." }, "finish_reason":"stop" } ], "usage":{ "prompt_tokens":332, "completion_tokens":125, "total_tokens":457 } }, "uuid":"None", "created_at":"None" }

print(response['completion']['choices'][0]['message']['content'])
# Acoustic neuromas may be a part of neurofibromatosis type 2 occurring due to a defect on chromosome 22q12.2 at the location of the neurofibromin 2 gene which encodes merlin protein. The hereditary acoustic neuroma occurs in NF II much more often compared to neurofibromatosis type 1 (NF I), although the latter is much more common. Unilateral acoustic neuroma has been reported exclusively in 24% of cases with NF I, while bilateral acoustic schwannoma is a hallmark of NF II. Both these conditions are autosomal dominant.

Last updated

Was this helpful?