File size: 3,544 Bytes
b6fadc7
 
 
 
 
8bdf672
 
b6fadc7
 
 
 
8bdf672
b6fadc7
8bdf672
b6fadc7
8bdf672
b6fadc7
8bdf672
 
 
 
 
a1da257
8bdf672
a1da257
 
8bdf672
 
8dcd782
8bdf672
 
8dcd782
8bdf672
8dcd782
8bdf672
 
8dcd782
b6fadc7
e3e32d4
8bdf672
a1da257
e3e32d4
b6fadc7
6323a30
 
8bdf672
 
b6fadc7
 
8bdf672
b6fadc7
8bdf672
fbdf17d
 
 
 
 
 
 
 
8bdf672
 
fbdf17d
 
8d13ddc
b6fadc7
8bdf672
3108590
a1da257
b6fadc7
8bdf672
b6fadc7
7c81dd2
8bdf672
 
 
6323a30
8bdf672
 
b6fadc7
 
 
 
8bdf672
 
 
b6fadc7
 
 
 
 
 
 
a1da257
8bdf672
b6fadc7
0aef952
 
 
b6fadc7
 
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
import os
from typing import Optional
from pydantic import Field, BaseModel
from omegaconf import OmegaConf

from vectara_agentic.agent import Agent
from vectara_agentic.tools import VectaraToolFactory

from dotenv import load_dotenv
load_dotenv(override=True)

initial_prompt = "How can I help you today?"

def create_assistant_tools(cfg):

    class QueryHMC(BaseModel):
        query: str = Field(description="The user query.")
        ticker: Optional[str] = Field(
            default=None,
            description="The company ticker.",
            examples=['GOOG', 'META']
        )
        year: int | str = Field(
            default=None,
            description="The year of the report, or a string specifying a condition on the year",
            examples=[2020, '>2021', '<2023', '>=2021', '<=2023', '[2021, 2023]', '[2021, 2023)']
        )
        quarter: Optional[int] = Field(
            default=None,
            description="The quarter of the report.",
            examples=[1, 2, 3, 4]
        )
        filing_type: Optional[str] = Field(
            default=None,
            description="The type of filing.",
            examples=['10K', '10Q']
        )

    vec_factory = VectaraToolFactory(
        vectara_api_key=cfg.api_key,
        vectara_corpus_key=cfg.corpus_key
    )
    
    summarizer = 'vectara-experimental-summary-ext-2023-12-11-med-omni'
    #summarizer = 'vectara-summary-ext-24-05-med-omni'
    ask_hmc = vec_factory.create_rag_tool(
        tool_name = "ask_hmc",
        tool_description = """
        Given a user query, 
        returns a response to a user question about fund management companies.
        """,
        tool_args_schema = QueryHMC,
        reranker = "chain", rerank_k = 100, 
        rerank_chain = [
            {
                "type": "slingshot",
                "cutoff": 0.2
            },
            {
                "type": "mmr",
                "diversity_bias": 0.05,
                "limit": 20
            }
        ],
        n_sentences_before = 2, n_sentences_after = 2, lambda_val = 0.005,
        vectara_summarizer = summarizer,
        summary_num_results = 10,
        include_citations = True,
        verbose=False,
    )
    return [ask_hmc]

def initialize_agent(_cfg, agent_progress_callback=None):
    bot_instructions = """
    - You are a helpful assistant, with expertise in management of public company stock portfolios.
    - Use the 'ask_hmc' tool to answer questions about public company performance, risks, and other financial metrics.
      If the tool responds with "I don't have enough information to answer", try rephrasing the question.
    - Use the year, quarter, filing_type and ticker arguments to the 'ask_hmc' tool to get more specific answers.
    - Note that 10Q reports exist for quarters 1, 2, 3 and for the 4th quarter there is a 10K report.
    """

    agent = Agent(
        tools=create_assistant_tools(_cfg),
        topic="Endowment fund management",
        custom_instructions=bot_instructions,
        agent_progress_callback=agent_progress_callback,
    )
    agent.report()
    return agent


def get_agent_config() -> OmegaConf:
    cfg = OmegaConf.create({
        'corpus_key': str(os.environ['VECTARA_CORPUS_KEY']),
        'api_key': str(os.environ['VECTARA_API_KEY']),
        'examples': os.environ.get('QUERY_EXAMPLES', None),
        'demo_name': "Harvard Management Company",
        'demo_welcome': "Harvard Management Company.",
        'demo_description': "AI Assistant.",
    })
    return cfg