Retrieval Augmented Generation

Retrieval Augmented Generation (RAG) is a technique used to teach a foundation model things it doesn't know.

Knowledge is retrieved from outside a foundation model and augment prompts by adding the relevant retrieved data in context.

For example, if you ask ChatGPT about a current topic, it will most likely fail to provide a good answer. However, if you use RAG, the system will retrieve relevant information from a knowledge source like News and append it to your prompt to inject knowledge use it to generate a better answer.

You can use AI Library to implement RAG with minimal coding.

The first step is to define a knowledge source. You can use a pre-built knowledge source or create a custom one.

Create a Knowledge Base

POST
/v1/knowledgebase
import requests

url = "https://api.ailibrary.ai/v1/knowledgebase"

payload = json.dumps({
  "name": "stable-diffusion-xl-1024-v1-0@stability",
})
headers = {
  'X-Library-Key': '••••••'
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)

The second step is to add data to knowledge source, if needed

First, upload files

Add Files to Knowledge Base

POST
/v1/files
import requests

url = "https://api.ailibrary.ai/v1/files"

files=[
  ('files',('w2-single.png',open('/link/to/local/file.png','rb'),'image/png'))
]
headers = {
  'X-Library-Key': '••••••'
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)

Next, process the files and add them to the knowledge source

The third step is to use the knowledge source to augment your prompt.

Was this page helpful?