Spaces:
Running
Running
updated
Browse files- Dockerfile +3 -0
- agent.py +54 -21
- app.py +3 -0
- requirements.txt +2 -3
- st_app.py +2 -3
Dockerfile
CHANGED
|
@@ -14,6 +14,9 @@ RUN useradd -m -u 1000 user
|
|
| 14 |
USER user
|
| 15 |
ENV HOME /home/user
|
| 16 |
ENV PATH $HOME/.local/bin:$PATH
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
WORKDIR $HOME
|
| 19 |
RUN mkdir app
|
|
|
|
| 14 |
USER user
|
| 15 |
ENV HOME /home/user
|
| 16 |
ENV PATH $HOME/.local/bin:$PATH
|
| 17 |
+
ENV TIKTOKEN_CACHE_DIR $HOME/.cache/tiktoken
|
| 18 |
+
|
| 19 |
+
RUN mkdir -p $HOME/.cache/tiktoken
|
| 20 |
|
| 21 |
WORKDIR $HOME
|
| 22 |
RUN mkdir app
|
agent.py
CHANGED
|
@@ -2,7 +2,7 @@ import os
|
|
| 2 |
import re
|
| 3 |
import requests
|
| 4 |
import json
|
| 5 |
-
from typing import Tuple, List
|
| 6 |
|
| 7 |
from omegaconf import OmegaConf
|
| 8 |
|
|
@@ -12,6 +12,7 @@ from vectara_agentic.agent import Agent
|
|
| 12 |
from vectara_agentic.agent_config import AgentConfig
|
| 13 |
from vectara_agentic.tools import ToolsFactory, VectaraToolFactory
|
| 14 |
from vectara_agentic.tools_catalog import ToolsCatalog
|
|
|
|
| 15 |
|
| 16 |
from dotenv import load_dotenv
|
| 17 |
load_dotenv(override=True)
|
|
@@ -39,9 +40,10 @@ class AgentTools:
|
|
| 39 |
self.tools_factory = ToolsFactory()
|
| 40 |
self.agent_config = agent_config
|
| 41 |
self.cfg = _cfg
|
| 42 |
-
self.vec_factory = VectaraToolFactory(
|
| 43 |
-
|
| 44 |
-
|
|
|
|
| 45 |
|
| 46 |
def get_opinion_text(
|
| 47 |
self,
|
|
@@ -212,15 +214,13 @@ class AgentTools:
|
|
| 212 |
|
| 213 |
def get_tools(self):
|
| 214 |
class QueryCaselawArgs(BaseModel):
|
| 215 |
-
|
| 216 |
|
| 217 |
-
|
| 218 |
-
vectara_corpus_key=self.cfg.corpus_key)
|
| 219 |
-
summarizer = 'vectara-experimental-summary-ext-2023-12-11-med-omni'
|
| 220 |
|
| 221 |
-
ask_caselaw = vec_factory.create_rag_tool(
|
| 222 |
tool_name = "ask_caselaw",
|
| 223 |
-
tool_description = "A tool for asking questions about case law in Alaska.
|
| 224 |
tool_args_schema = QueryCaselawArgs,
|
| 225 |
reranker = "chain", rerank_k = 100,
|
| 226 |
rerank_chain = [
|
|
@@ -240,11 +240,32 @@ class AgentTools:
|
|
| 240 |
n_sentences_before = 2, n_sentences_after = 2, lambda_val = 0.005,
|
| 241 |
summary_num_results = 15,
|
| 242 |
vectara_summarizer = summarizer,
|
|
|
|
|
|
|
| 243 |
include_citations = True,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
)
|
| 245 |
|
| 246 |
return (
|
| 247 |
-
[ask_caselaw] +
|
| 248 |
[self.tools_factory.create_tool(tool) for tool in [
|
| 249 |
self.get_opinion_text,
|
| 250 |
self.get_case_document_pdf,
|
|
@@ -270,35 +291,47 @@ def initialize_agent(_cfg, agent_progress_callback=None):
|
|
| 270 |
|
| 271 |
legal_assistant_instructions = """
|
| 272 |
- You are a helpful legal assistant, with case law expertise in the state of Alaska.
|
| 273 |
-
-
|
| 274 |
-
- If the 'ask_caselaw' tool responds that it does not have enough information to answer the question,
|
| 275 |
-
try to rephrase the query, or break the original query down into multiple sub-questions, and use the 'ask_caselaw' tool again.
|
| 276 |
- The references returned by the 'ask_caselaw' tool include metadata relevant to its response, such as case citations, dates, or names.
|
|
|
|
| 277 |
- When using a case citation in your response, try to include a valid URL along with it:
|
| 278 |
* Call the 'get_case_document_pdf' for a case citation to obtain a valid web URL to a pdf of the case record.
|
| 279 |
* If this doesn't work, call the 'get_case_document_page' for a case citation to obtain a valid web URL to a page with information about the case.
|
| 280 |
- When including a URL for a citation in your response, use the citation as anchor text, and the URL as the link.
|
| 281 |
-
- Never use your internal knowledge to guess a case citation. Only use citation information provided by a tool or the user.
|
| 282 |
- A Case Citation includes 3 components: volume number, reporter, and first page.
|
| 283 |
Here are some examples: '253 P.2d 136', '10 Alaska 11', '6 C.M.A. 3'
|
| 284 |
- If two cases have conflicting rulings, assume that the case with the more current ruling date is correct.
|
| 285 |
- If the response is based on cases that are older than 5 years, make sure to inform the user that the information may be outdated,
|
| 286 |
since some case opinions may no longer apply in law.
|
| 287 |
-
- To summarize the case, use the 'get_opinion_text' with summarize=True.
|
| 288 |
-
- Use 'get_opinion_text' with summarize=False only when full text is needed. Consider summarizing the text when possible to make things run faster.
|
| 289 |
- If a user wants to test their argument, use the 'ask_caselaw' tool to gather information about cases related to their argument
|
| 290 |
and the 'critique_as_judge' tool to determine whether their argument is sound or has issues that must be corrected.
|
| 291 |
- Never discuss politics, and always respond politely.
|
| 292 |
-
- Your response should not include markdown.
|
| 293 |
"""
|
| 294 |
-
agent_config = AgentConfig(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 295 |
|
| 296 |
agent = Agent(
|
| 297 |
tools=AgentTools(_cfg, agent_config).get_tools(),
|
| 298 |
topic="Case law in Alaska",
|
| 299 |
custom_instructions=legal_assistant_instructions,
|
| 300 |
agent_progress_callback=agent_progress_callback,
|
| 301 |
-
agent_config=agent_config
|
|
|
|
| 302 |
)
|
| 303 |
-
agent.report()
|
| 304 |
return agent
|
|
|
|
| 2 |
import re
|
| 3 |
import requests
|
| 4 |
import json
|
| 5 |
+
from typing import Tuple, List, Optional
|
| 6 |
|
| 7 |
from omegaconf import OmegaConf
|
| 8 |
|
|
|
|
| 12 |
from vectara_agentic.agent_config import AgentConfig
|
| 13 |
from vectara_agentic.tools import ToolsFactory, VectaraToolFactory
|
| 14 |
from vectara_agentic.tools_catalog import ToolsCatalog
|
| 15 |
+
from vectara_agentic.types import ModelProvider, AgentType
|
| 16 |
|
| 17 |
from dotenv import load_dotenv
|
| 18 |
load_dotenv(override=True)
|
|
|
|
| 40 |
self.tools_factory = ToolsFactory()
|
| 41 |
self.agent_config = agent_config
|
| 42 |
self.cfg = _cfg
|
| 43 |
+
self.vec_factory = VectaraToolFactory(
|
| 44 |
+
vectara_api_key=_cfg.api_key,
|
| 45 |
+
vectara_corpus_key=_cfg.corpus_key,
|
| 46 |
+
)
|
| 47 |
|
| 48 |
def get_opinion_text(
|
| 49 |
self,
|
|
|
|
| 214 |
|
| 215 |
def get_tools(self):
|
| 216 |
class QueryCaselawArgs(BaseModel):
|
| 217 |
+
citations: Optional[str] = Field(description = citation_description, default=None)
|
| 218 |
|
| 219 |
+
summarizer = 'vectara-summary-table-md-query-ext-jan-2025-gpt-4o'
|
|
|
|
|
|
|
| 220 |
|
| 221 |
+
ask_caselaw = self.vec_factory.create_rag_tool(
|
| 222 |
tool_name = "ask_caselaw",
|
| 223 |
+
tool_description = "A tool for asking questions about case law, and any legal issue in the state of Alaska.",
|
| 224 |
tool_args_schema = QueryCaselawArgs,
|
| 225 |
reranker = "chain", rerank_k = 100,
|
| 226 |
rerank_chain = [
|
|
|
|
| 240 |
n_sentences_before = 2, n_sentences_after = 2, lambda_val = 0.005,
|
| 241 |
summary_num_results = 15,
|
| 242 |
vectara_summarizer = summarizer,
|
| 243 |
+
max_tokens = 4096,
|
| 244 |
+
max_response_chars = 8192,
|
| 245 |
include_citations = True,
|
| 246 |
+
save_history = True,
|
| 247 |
+
)
|
| 248 |
+
|
| 249 |
+
search_caselaw = self.vec_factory.create_search_tool(
|
| 250 |
+
tool_name = "search_caselaw",
|
| 251 |
+
tool_description = "A tool for retrieving a list of relevant documents about case law in Alaska.",
|
| 252 |
+
tool_args_schema = QueryCaselawArgs,
|
| 253 |
+
reranker = "chain", rerank_k = 100,
|
| 254 |
+
rerank_chain = [
|
| 255 |
+
{
|
| 256 |
+
"type": "slingshot",
|
| 257 |
+
"cutoff": 0.2
|
| 258 |
+
},
|
| 259 |
+
{
|
| 260 |
+
"type": "mmr",
|
| 261 |
+
"diversity_bias": 0.1
|
| 262 |
+
},
|
| 263 |
+
],
|
| 264 |
+
n_sentences_before = 2, n_sentences_after = 2, lambda_val = 0.005,
|
| 265 |
)
|
| 266 |
|
| 267 |
return (
|
| 268 |
+
[ask_caselaw, search_caselaw] +
|
| 269 |
[self.tools_factory.create_tool(tool) for tool in [
|
| 270 |
self.get_opinion_text,
|
| 271 |
self.get_case_document_pdf,
|
|
|
|
| 291 |
|
| 292 |
legal_assistant_instructions = """
|
| 293 |
- You are a helpful legal assistant, with case law expertise in the state of Alaska.
|
| 294 |
+
- Always use the 'ask_caselaw' tool first, as your primary tool for answering questions. Never use your own knowledge.
|
|
|
|
|
|
|
| 295 |
- The references returned by the 'ask_caselaw' tool include metadata relevant to its response, such as case citations, dates, or names.
|
| 296 |
+
- Use the 'search_caselaw' tool to search for documents related to case law in Alaska, and set summarize=True to get a summary of those documents.
|
| 297 |
- When using a case citation in your response, try to include a valid URL along with it:
|
| 298 |
* Call the 'get_case_document_pdf' for a case citation to obtain a valid web URL to a pdf of the case record.
|
| 299 |
* If this doesn't work, call the 'get_case_document_page' for a case citation to obtain a valid web URL to a page with information about the case.
|
| 300 |
- When including a URL for a citation in your response, use the citation as anchor text, and the URL as the link.
|
| 301 |
+
- Never use your internal knowledge to guess a case citation. Only use citation information provided by a tool or by the user.
|
| 302 |
- A Case Citation includes 3 components: volume number, reporter, and first page.
|
| 303 |
Here are some examples: '253 P.2d 136', '10 Alaska 11', '6 C.M.A. 3'
|
| 304 |
- If two cases have conflicting rulings, assume that the case with the more current ruling date is correct.
|
| 305 |
- If the response is based on cases that are older than 5 years, make sure to inform the user that the information may be outdated,
|
| 306 |
since some case opinions may no longer apply in law.
|
|
|
|
|
|
|
| 307 |
- If a user wants to test their argument, use the 'ask_caselaw' tool to gather information about cases related to their argument
|
| 308 |
and the 'critique_as_judge' tool to determine whether their argument is sound or has issues that must be corrected.
|
| 309 |
- Never discuss politics, and always respond politely.
|
|
|
|
| 310 |
"""
|
| 311 |
+
agent_config = AgentConfig(
|
| 312 |
+
agent_type = os.getenv("VECTARA_AGENTIC_AGENT_TYPE", AgentType.OPENAI.value),
|
| 313 |
+
main_llm_provider = os.getenv("VECTARA_AGENTIC_MAIN_LLM_PROVIDER", ModelProvider.OPENAI.value),
|
| 314 |
+
main_llm_model_name = os.getenv("VECTARA_AGENTIC_MAIN_MODEL_NAME", ""),
|
| 315 |
+
tool_llm_provider = os.getenv("VECTARA_AGENTIC_TOOL_LLM_PROVIDER", ModelProvider.OPENAI.value),
|
| 316 |
+
tool_llm_model_name = os.getenv("VECTARA_AGENTIC_TOOL_MODEL_NAME", ""),
|
| 317 |
+
observer = os.getenv("VECTARA_AGENTIC_OBSERVER_TYPE", "NO_OBSERVER")
|
| 318 |
+
)
|
| 319 |
+
fallback_agent_config = AgentConfig(
|
| 320 |
+
agent_type = os.getenv("VECTARA_AGENTIC_FALLBACK_AGENT_TYPE", AgentType.OPENAI.value),
|
| 321 |
+
main_llm_provider = os.getenv("VECTARA_AGENTIC_FALLBACK_MAIN_LLM_PROVIDER", ModelProvider.OPENAI.value),
|
| 322 |
+
main_llm_model_name = os.getenv("VECTARA_AGENTIC_FALLBACK_MAIN_MODEL_NAME", ""),
|
| 323 |
+
tool_llm_provider = os.getenv("VECTARA_AGENTIC_FALLBACK_TOOL_LLM_PROVIDER", ModelProvider.OPENAI.value),
|
| 324 |
+
tool_llm_model_name = os.getenv("VECTARA_AGENTIC_FALLBACK_TOOL_MODEL_NAME", ""),
|
| 325 |
+
observer = os.getenv("VECTARA_AGENTIC_OBSERVER_TYPE", "NO_OBSERVER")
|
| 326 |
+
)
|
| 327 |
|
| 328 |
agent = Agent(
|
| 329 |
tools=AgentTools(_cfg, agent_config).get_tools(),
|
| 330 |
topic="Case law in Alaska",
|
| 331 |
custom_instructions=legal_assistant_instructions,
|
| 332 |
agent_progress_callback=agent_progress_callback,
|
| 333 |
+
agent_config=agent_config,
|
| 334 |
+
fallback_agent_config=fallback_agent_config,
|
| 335 |
)
|
| 336 |
+
agent.report(detailed=False)
|
| 337 |
return agent
|
app.py
CHANGED
|
@@ -1,10 +1,13 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
from st_app import launch_bot
|
| 3 |
import uuid
|
| 4 |
|
| 5 |
import nest_asyncio
|
| 6 |
import asyncio
|
| 7 |
|
|
|
|
|
|
|
| 8 |
# Setup for HTTP API Calls to Amplitude Analytics
|
| 9 |
if 'device_id' not in st.session_state:
|
| 10 |
st.session_state.device_id = str(uuid.uuid4())
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
from st_app import launch_bot
|
| 4 |
import uuid
|
| 5 |
|
| 6 |
import nest_asyncio
|
| 7 |
import asyncio
|
| 8 |
|
| 9 |
+
torch.classes.__path__ = []
|
| 10 |
+
|
| 11 |
# Setup for HTTP API Calls to Amplitude Analytics
|
| 12 |
if 'device_id' not in st.session_state:
|
| 13 |
st.session_state.device_id = str(uuid.uuid4())
|
requirements.txt
CHANGED
|
@@ -1,9 +1,8 @@
|
|
| 1 |
omegaconf==2.3.0
|
| 2 |
python-dotenv==1.0.1
|
| 3 |
-
streamlit==1.
|
| 4 |
-
streamlit_pills==0.3.0
|
| 5 |
streamlit-feedback==0.1.3
|
| 6 |
uuid==1.30
|
| 7 |
langdetect==1.0.9
|
| 8 |
langcodes==3.4.0
|
| 9 |
-
vectara-agentic==0.2.
|
|
|
|
| 1 |
omegaconf==2.3.0
|
| 2 |
python-dotenv==1.0.1
|
| 3 |
+
streamlit==1.43.2
|
|
|
|
| 4 |
streamlit-feedback==0.1.3
|
| 5 |
uuid==1.30
|
| 6 |
langdetect==1.0.9
|
| 7 |
langcodes==3.4.0
|
| 8 |
+
vectara-agentic==0.2.15
|
st_app.py
CHANGED
|
@@ -3,7 +3,6 @@ import sys
|
|
| 3 |
import re
|
| 4 |
|
| 5 |
import streamlit as st
|
| 6 |
-
from streamlit_pills import pills
|
| 7 |
from streamlit_feedback import streamlit_feedback
|
| 8 |
|
| 9 |
from utils import thumbs_feedback, escape_dollars_outside_latex, send_amplitude_data
|
|
@@ -46,7 +45,7 @@ def agent_progress_callback(status_type: AgentStatusType, msg: str):
|
|
| 46 |
|
| 47 |
def show_example_questions():
|
| 48 |
if len(st.session_state.example_messages) > 0 and st.session_state.first_turn:
|
| 49 |
-
selected_example = pills("Queries to Try:", st.session_state.example_messages,
|
| 50 |
if selected_example:
|
| 51 |
st.session_state.ex_prompt = selected_example
|
| 52 |
st.session_state.first_turn = False
|
|
@@ -139,7 +138,7 @@ async def launch_bot():
|
|
| 139 |
if st.session_state.prompt:
|
| 140 |
with st.chat_message("assistant", avatar='🤖'):
|
| 141 |
st.session_state.status = st.status('Processing...', expanded=False)
|
| 142 |
-
response = st.session_state.agent.
|
| 143 |
res = escape_dollars_outside_latex(response.response)
|
| 144 |
message = {"role": "assistant", "content": res, "avatar": '🤖'}
|
| 145 |
st.session_state.messages.append(message)
|
|
|
|
| 3 |
import re
|
| 4 |
|
| 5 |
import streamlit as st
|
|
|
|
| 6 |
from streamlit_feedback import streamlit_feedback
|
| 7 |
|
| 8 |
from utils import thumbs_feedback, escape_dollars_outside_latex, send_amplitude_data
|
|
|
|
| 45 |
|
| 46 |
def show_example_questions():
|
| 47 |
if len(st.session_state.example_messages) > 0 and st.session_state.first_turn:
|
| 48 |
+
selected_example = st.pills("Queries to Try:", st.session_state.example_messages, default=None)
|
| 49 |
if selected_example:
|
| 50 |
st.session_state.ex_prompt = selected_example
|
| 51 |
st.session_state.first_turn = False
|
|
|
|
| 138 |
if st.session_state.prompt:
|
| 139 |
with st.chat_message("assistant", avatar='🤖'):
|
| 140 |
st.session_state.status = st.status('Processing...', expanded=False)
|
| 141 |
+
response = await st.session_state.agent.achat(st.session_state.prompt)
|
| 142 |
res = escape_dollars_outside_latex(response.response)
|
| 143 |
message = {"role": "assistant", "content": res, "avatar": '🤖'}
|
| 144 |
st.session_state.messages.append(message)
|