File size: 3,714 Bytes
4826e54
38812af
 
4826e54
38812af
4826e54
 
 
38812af
4826e54
38812af
4826e54
 
 
 
 
 
 
 
 
 
38812af
 
 
 
4826e54
 
38812af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4826e54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38812af
4826e54
 
 
38812af
 
4826e54
 
 
 
 
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from langchain.tools import Tool
from langchain_community.retrievers import BM25Retriever
from langchain.docstore.document import Document
from langchain_core.messages import HumanMessage
import datasets
from langchain_openai import AzureChatOpenAI
import os
from dotenv import load_dotenv

load_dotenv()

# Create LLM instance once
conversation_llm = AzureChatOpenAI(
    azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
    api_key=os.getenv("AZURE_OPENAI_API_KEY"),
    deployment_name=os.getenv("DEPLOYMENT_NAME"),
    openai_api_version=os.getenv("OPENAI_API_VERSION"),
    temperature=0.75,
    streaming=False,
    verbose=False
)


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

    # Convert dataset entries into Document objects
    docs = [
        Document(
            page_content="\n".join([
                f"Name: {guest['name']}",
                f"Relation: {guest['relation']}",
                f"Description: {guest['description']}",
                f"Email: {guest['email']}"
            ]),
            metadata={"name": guest["name"]}
        )
        for guest in guest_dataset
    ]

    return docs


docs = load_guest_dataset()
bm25_retriever = BM25Retriever.from_documents(docs)


def generate_conversation_starter(description: str) -> str:
    """Generate a conversation starter based on guest description"""
    try:
        generate_prompt = (
            f"Generate a very simple and short conversation starter from the description of the person.\n\n"
            f"For example:\n"
            f"Description: Rear Admiral Grace Hopper was a trailblazer in computer programming and helped invent the first compiler. "
            f"She's passionate about teaching and loves telling stories about debugging.\n\n"
            f"Conversation Starter: Ask her about the time she found a real bug in a computer — she loves that story!\n\n"
            f"Description: {description}\n\n"
            f"Conversation Starter:"
        )

        response = conversation_llm.invoke(
            [HumanMessage(content=generate_prompt)])
        return response.content.strip()
    except Exception:
        return "Ask them about their background and interests!"


def retrieve_info_from_name(query: str) -> str:
    """Retrieves detailed information about gala guests based on their name or relation."""
    results = bm25_retriever.invoke(query)
    if results:
        guest_info_with_starters = []

        for i, doc in enumerate(results[:3], 1):
            guest_info = doc.page_content

            # Extract description from the content
            lines = guest_info.split('\n')
            description = ""
            for line in lines:
                if line.startswith("Description:"):
                    description = line.replace("Description:", "").strip()
                    break

            # Add guest info
            result_text = f"Guest {i}:\n{guest_info}"

            # Add conversation starter if description exists
            if description:
                conversation_starter = generate_conversation_starter(
                    description)
                result_text += f"\n💬 Conversation Starter: {conversation_starter}"

            guest_info_with_starters.append(result_text)

        return "\n\n" + "="*50 + "\n\n".join(guest_info_with_starters)
    else:
        return "No matching guest information found."


guest_info_tool = Tool(
    name="guest_info_retriever",
    func=retrieve_info_from_name,
    description="Retrieves detailed information about gala guests based on their name or relation, including conversation starters."
)