joao-vectara commited on
Commit
1ac1349
·
verified ·
1 Parent(s): 8ce19b2

Create agent.py

Browse files
Files changed (1) hide show
  1. agent.py +63 -0
agent.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import requests
3
+ from pydantic import Field, BaseModel
4
+
5
+ from omegaconf import OmegaConf
6
+
7
+ from vectara_agentic.agent import Agent
8
+ from vectara_agentic.tools import ToolsFactory, VectaraToolFactory
9
+
10
+ def create_assistant_tools(cfg):
11
+ class QueryDocsArgs(BaseModel):
12
+ query: str = Field(..., description="The user query, always in the form of a question",
13
+ examples=["Based on uploaded documents, what are the top four challenges of the Fintech sector in Saudi Arabia? list them in bullet points."])
14
+
15
+ vec_factory = VectaraToolFactory(vectara_api_key=cfg.api_key,
16
+ vectara_corpus_key=cfg.corpus_key)
17
+ summarizer = 'mockingbird-1.0-2024-07-16'
18
+ ask_docs = vec_factory.create_rag_tool(
19
+ tool_name = "ask_docs",
20
+ tool_description = """
21
+ Responds to an user question about a particular analysis, based on the documentation provide.
22
+ """,
23
+ tool_args_schema = QueryDocsArgs,
24
+ reranker = "chain", rerank_k = 100,
25
+ rerank_chain = [
26
+ {
27
+ "type": "multilingual_reranker_v1",
28
+ # "cutoff": 0.2
29
+ },
30
+ {
31
+ "type": "mmr",
32
+ "diversity_bias": 0.2,
33
+ "limit": 50
34
+ }
35
+ ],
36
+ n_sentences_before = 2, n_sentences_after = 2, lambda_val = 0.005,
37
+ summary_num_results = 15,
38
+ vectara_summarizer = summarizer,
39
+ include_citations = True,
40
+ #vectara_prompt_text=prompt,
41
+ save_history = True,
42
+ verbose=False
43
+ )
44
+ tools_factory = ToolsFactory()
45
+ return (
46
+ tools_factory.standard_tools() +
47
+ [ask_docs]
48
+ )
49
+
50
+ def initialize_agent(_cfg, agent_progress_callback=None):
51
+ stc_bank_bot_instructions = """
52
+ - Call the the ask_docs tool to retrieve the information to answer the user query.
53
+ - Always summarize the response.
54
+ """
55
+
56
+ agent = Agent(
57
+ tools=create_assistant_tools(_cfg),
58
+ topic="STC Bank questions",
59
+ custom_instructions=stc_bank_bot_instructions,
60
+ agent_progress_callback=agent_progress_callback,
61
+ )
62
+ agent.report()
63
+ return agent