Spaces:
Sleeping
Sleeping
| import os | |
| from omegaconf import OmegaConf | |
| from dotenv import load_dotenv | |
| load_dotenv(override=True) | |
| from pydantic import Field, BaseModel | |
| from vectara_agentic.agent import Agent | |
| from vectara_agentic.agent_config import AgentConfig | |
| from vectara_agentic.tools import ToolsFactory, VectaraToolFactory | |
| from vectara_agentic.tools_catalog import ToolsCatalog | |
| from vectara_agentic.types import ModelProvider, AgentType | |
| teaching_styles = ['Inquiry-based', 'Socratic', 'traditional'] | |
| languages = {'English': 'en', 'Spanish': 'es', 'French': 'fr', 'German': 'de', 'Arabic': 'ar', 'Chinese': 'zh-cn', | |
| 'Hebrew': 'he', 'Hindi': 'hi', 'Italian': 'it', 'Japanese': 'ja', 'Korean': 'ko', 'Portuguese': 'pt'} | |
| initial_prompt = "How can I help you today?" | |
| class AgentTools: | |
| def __init__(self, _cfg, agent_config): | |
| self.tools_factory = ToolsFactory() | |
| self.agent_config = agent_config | |
| self.cfg = _cfg | |
| self.vec_factory = VectaraToolFactory( | |
| vectara_api_key=_cfg.api_key, | |
| vectara_corpus_key=_cfg.corpus_key | |
| ) | |
| def adjust_response_to_student( | |
| self, | |
| text: str = Field(description='the text to adjust. may include citations in markdown format.'), | |
| age: int = Field(description='the age of the student. An integer'), | |
| style: str = Field(description='teaching style'), | |
| language: str = Field(description='the language') | |
| ) -> str: | |
| """ | |
| Rephrase the provided text to match the student's age, desired teaching style and language. | |
| """ | |
| instructions = f''' | |
| The following is some text, which may include citations in markdown format. | |
| Adjust the response to match the student's age of {age}, the {style} teaching style. | |
| Where possible, maintain the citations in context of the response (instead of listing them at the end). | |
| When showing citations, use markdown in the following format: [[N](URL)], where N is the citation sequential number. | |
| You can renumber citations if needed. | |
| For example, in the inquiry-based teaching style, choose to ask questions that encourage the student to think critically instead of repsonding directly with the answer. | |
| Or in the socratic teaching style, choose to ask questions that lead the student to the answer. | |
| Always respond in the {language} language.''' \ | |
| .replace("{style}", self.cfg.style) \ | |
| .replace("{language}", self.cfg.language) \ | |
| .replace("{student_age}", str(self.cfg.student_age)) | |
| rephrase_text = ToolsCatalog(self.agent_config).rephrase_text | |
| return rephrase_text(text, instructions) | |
| def get_tools(self): | |
| vec_factory = VectaraToolFactory(vectara_api_key=self.cfg.api_key,vectara_corpus_key=self.cfg.corpus_key) | |
| summarizer = 'vectara-summary-table-md-query-ext-jan-2025-gpt-4o' | |
| query_tool = vec_factory.create_rag_tool( | |
| tool_name = "ask_about_justice_harvard", | |
| tool_description = """ | |
| Answer questions about the justice, morality, politics and related topics, | |
| based on transcripts of recordings from the Justice Harvard class that includes a lot of content on these topics. | |
| """, | |
| reranker = "multilingual_reranker_v1", rerank_k = 100, rerank_cutoff = 0.2, | |
| n_sentences_before = 2, n_sentences_after = 2, lambda_val = 0.005, | |
| summary_num_results = 15, | |
| max_tokens = 4096, max_response_chars = 8192, | |
| vectara_summarizer = summarizer, | |
| include_citations = True, | |
| verbose = False, | |
| save_history = True, | |
| ) | |
| tools_factory = ToolsFactory() | |
| return ( | |
| [tools_factory.create_tool(tool) for tool in | |
| [ | |
| self.adjust_response_to_student, | |
| ] | |
| ] + | |
| tools_factory.standard_tools() + | |
| tools_factory.guardrail_tools() + | |
| [query_tool] | |
| ) | |
| def initialize_agent(_cfg, agent_progress_callback=None): | |
| bot_instructions = f""" | |
| - You are a helpful teacher assistant, with expertise in education in various teaching styles. | |
| - Obtain information using tools to answer the user's query. | |
| - If the tool cannot provide information relevant to the user's query, try calling the tool again with a rephrased query. | |
| If it fails 3 times, then tell the user that you are unable to provide an answer. | |
| - If the tool can provide relevant information, use the adjust_response_to_student tool | |
| to rephrase the text (including citations if any) to ensure it fits the student's age of {_cfg.student_age}, | |
| the {_cfg.style} teaching style and the {_cfg.language} language. | |
| - When including links to the Justice Harvard videos, the url should include the start time (from the metadata). | |
| For example: https://youtube.com/watch?v=0O2Rq4HJBxw&t=1234 represents a link to a youtube video starting at 1234 seconds. | |
| - When showing citations, renumber them if needed and use markdown in the following format: [[N](URL)], where N is the citation sequential number. | |
| - Response in a concise and clear manner, and provide the most relevant information to the student. | |
| - Never discuss politics, and always respond politely. | |
| """ | |
| agent_config = AgentConfig( | |
| agent_type = os.getenv("VECTARA_AGENTIC_AGENT_TYPE", AgentType.OPENAI.value), | |
| main_llm_provider = os.getenv("VECTARA_AGENTIC_MAIN_LLM_PROVIDER", ModelProvider.OPENAI.value), | |
| main_llm_model_name = os.getenv("VECTARA_AGENTIC_MAIN_MODEL_NAME", ""), | |
| tool_llm_provider = os.getenv("VECTARA_AGENTIC_TOOL_LLM_PROVIDER", ModelProvider.OPENAI.value), | |
| tool_llm_model_name = os.getenv("VECTARA_AGENTIC_TOOL_MODEL_NAME", ""), | |
| observer = os.getenv("VECTARA_AGENTIC_OBSERVER_TYPE", "NO_OBSERVER") | |
| ) | |
| fallback_agent_config = AgentConfig( | |
| agent_type = os.getenv("VECTARA_AGENTIC_FALLBACK_AGENT_TYPE", AgentType.OPENAI.value), | |
| main_llm_provider = os.getenv("VECTARA_AGENTIC_FALLBACK_MAIN_LLM_PROVIDER", ModelProvider.OPENAI.value), | |
| main_llm_model_name = os.getenv("VECTARA_AGENTIC_FALLBACK_MAIN_MODEL_NAME", ""), | |
| tool_llm_provider = os.getenv("VECTARA_AGENTIC_FALLBACK_TOOL_LLM_PROVIDER", ModelProvider.OPENAI.value), | |
| tool_llm_model_name = os.getenv("VECTARA_AGENTIC_FALLBACK_TOOL_MODEL_NAME", ""), | |
| observer = os.getenv("VECTARA_AGENTIC_OBSERVER_TYPE", "NO_OBSERVER") | |
| ) | |
| agent = Agent( | |
| agent_config=agent_config, | |
| fallback_agent_config=fallback_agent_config, | |
| tools=AgentTools(_cfg, agent_config).get_tools(), | |
| topic="justice, morality, politics, and philosophy", | |
| custom_instructions=bot_instructions, | |
| agent_progress_callback=agent_progress_callback | |
| ) | |
| 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': "Justice-Harvard", | |
| 'demo_welcome': "Welcome to the Justice Harvard e-learning assistant demo.", | |
| 'demo_description': "Ask questions about the Justice Harvard class and get answers from an expert teaching assistant.", | |
| 'style': teaching_styles[0], | |
| 'language': 'English', | |
| 'student_age': 18 | |
| }) | |
| return cfg | |