Skip to content

Workshop 3.5

Plai edited this page Aug 12, 2024 · 4 revisions

Read and dump knowledge into embedding API

Make sure to finish workshop 1, 2, and 3 first, or you can switch to branch pre-workshop-3.5 to start from the check point.

Step 1: Start dependencies

Now back to the empty vector database as we are dumping the knowledge into the database oueselves this time.

We've also prepared files for the tool that we are going to build in this workshop in the folder demo/2-products. Each file is a YAML that contains information about a product (name, description, prices, discount, reviewes, etc.)

Run the following command to start all the components

docker compose up -d

You should have all components running as followings

  • Embedding API at localhost:8001
  • Search UI at localhost:8002
  • Chatbot UI at localhost:8003
  • The vector database

Step 2: Create the tool

Create a new folder called retriever and a new file inside the folder, let's call it index.py and add this code into the file.

import os
import requests

from common.config import config

DATA_DIR = 'demo/2-products'

def send_files():
    for filename in os.listdir(DATA_DIR):
        if filename.endswith('.yaml'):
            file_path = os.path.join(DATA_DIR, filename)
            with open(file_path, 'r') as file:
                content = file.read()
                response = requests.post(f"http://{config.embedding_api_host}/text", json={"source": filename, "text": content})
                print("filename:", filename, "status:", response.status_code)


if __name__ == "__main__":
    send_files()

Step 3: Dump the knowledge

Let's run

PYTHONPATH=$(pwd) python retriever/index.py

PYTHONPATH env is required as the code provided above utilizes code from other module

You should see the ingestion results

filename: 1.yaml status: 200
filename: 2.yaml status: 200
...

Now, let's try search and chatbot to find a product.

recommend me a fragrance

or

pet's food

Closing

Congratulations on completing this immersive workshop! You've gained valuable skills in leveraging Large Language Models (LLMs), Retrieval-Augmented Generation (RAG), and embeddings to build powerful search engines and conversational AI assistants.

You now understand how to save and retrieve embeddings, create internal search pages utilizing these embeddings, and develop chatbots that can understand prompts, retrieve relevant knowledge, and provide contextual responses through chat completion.

It's important to note that the example code provided in this workshop is not optimal and may have flaws. One obvious issue is that the system message changes every time the chat receives a new user message, which could lead to inconsistent responses and loss of original context.

I encourage you to experiment with the code, modify the order of messages, adjust context placement, and observe the results. This hands-on exploration will deepen your understanding of these technologies and their nuances.

Remember, the true power lies in applying these concepts creatively to your unique use cases. Continuous learning and experimentation will keep you ahead in the rapidly evolving AI landscape.

Thank you for your active participation. We wish you success in creating innovative solutions with the knowledge you've acquired here.

Clone this wiki locally