File size: 1,612 Bytes
ad2b3b2
 
38812af
ad2b3b2
38812af
 
 
 
 
 
 
 
 
 
 
 
 
ad2b3b2
7fd0db3
ad2b3b2
38812af
 
ad2b3b2
 
 
 
 
 
 
 
38812af
 
ad2b3b2
38812af
8507438
38812af
 
ad2b3b2
 
 
 
 
 
 
 
 
 
 
 
38812af
 
ad2b3b2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from uuid import uuid4

import datasets
from smolagents import Tool


class GuestInfoRetrieverTool(Tool):
    name = "guest_info_retriever"
    description = "Retrieves detailed information about gala guests based on their name or relation."
    inputs = {
        "query": {
            "type": "string",
            "description": "The name or relation of the guest you want information about."
        }
    }
    output_type = "string"

    def __init__(self, vector_store):
        self.is_initialized = False
        self.vector_store = vector_store

    def forward(self, query: str):
        result = self.vector_store.query(
            query_texts=[query],
            n_results=3
        )

        distances = [distance for distance in result['distances'][0] if distance < 1.3]
        docs = result['documents'][0]
        return "\n\n".join([docs[idx] for idx in range(0, len(distances))])


def load_guest_dataset(vector_store):
    # Load the dataset
    guest_dataset = datasets.load_dataset("agents-course/unit3-invitees", split="train")

    # Convert dataset entries into Document objects
    for guest in guest_dataset:
        vector_store.add(
            documents=[
                "\n".join([
                    f"Name: {guest['name']}",
                    f"Relation: {guest['relation']}",
                    f"Description: {guest['description']}",
                    f"Email: {guest['email']}"
                ])
            ],
            metadatas=[{"name": guest["name"]}],
            ids=[str(uuid4())])

    # Return the tool
    return GuestInfoRetrieverTool(vector_store)