ragaai-catalyst 2.1.5b15__tar.gz → 2.1.5b16__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/PKG-INFO +1 -1
- ragaai_catalyst-2.1.5b16/examples/langgraph_examples/agentic_rag.py +318 -0
- ragaai_catalyst-2.1.5b16/examples/langgraph_examples/customer_support.py +551 -0
- ragaai_catalyst-2.1.5b16/examples/langgraph_examples/multi_tool.py +146 -0
- ragaai_catalyst-2.1.5b16/examples/langgraph_examples/planning_agent.py +193 -0
- ragaai_catalyst-2.1.5b16/examples/langgraph_multi_tools.py +148 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/pyproject.toml +1 -1
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/agentic_tracing/tracers/tool_tracer.py +111 -70
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst.egg-info/PKG-INFO +1 -1
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst.egg-info/SOURCES.txt +5 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/.github/ISSUE_TEMPLATE/bug_report.md +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/.github/ISSUE_TEMPLATE/feature_request.md +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/.github/PULL_REQUEST_TEMPLATE.md +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/.gitignore +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/LICENSE +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/README.md +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/docs/dataset_management.md +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/docs/prompt_management.md +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/examples/FinancialAnalysisSystem.ipynb +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/examples/TravelPlanner.ipynb +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/examples/agentic_rag.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/examples/custom_tracer_example.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/examples/customer_support.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/examples/finance.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/examples/planning_agent.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/examples/prompt_management_litellm.ipynb +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/examples/prompt_management_openai.ipynb +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/examples/sync_sample_call.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/examples/travel_agent/agents.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/examples/travel_agent/config.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/examples/travel_agent/main.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/examples/travel_agent/tools.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/__init__.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/_version.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/dataset.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/evaluation.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/experiment.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/guard_executor.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/guardrails_manager.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/internal_api_completion.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/prompt_manager.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/proxy_call.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/ragaai_catalyst.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/synthetic_data_generation.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/__init__.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/agentic_tracing/README.md +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/agentic_tracing/__init__.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/agentic_tracing/data/__init__.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/agentic_tracing/data/data_structure.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/agentic_tracing/tests/FinancialAnalysisSystem.ipynb +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/agentic_tracing/tests/GameActivityEventPlanner.ipynb +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/agentic_tracing/tests/TravelPlanner.ipynb +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/agentic_tracing/tests/__init__.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/agentic_tracing/tests/ai_travel_agent.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/agentic_tracing/tests/unique_decorator_test.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/agentic_tracing/tracers/__init__.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/agentic_tracing/tracers/agent_tracer.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/agentic_tracing/tracers/base.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/agentic_tracing/tracers/custom_tracer.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/agentic_tracing/tracers/langgraph_tracer.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/agentic_tracing/tracers/llm_tracer.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/agentic_tracing/tracers/main_tracer.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/agentic_tracing/tracers/network_tracer.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/agentic_tracing/tracers/user_interaction_tracer.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/agentic_tracing/upload/__init__.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/agentic_tracing/upload/upload_agentic_traces.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/agentic_tracing/upload/upload_code.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/agentic_tracing/upload/upload_trace_metric.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/agentic_tracing/utils/__init__.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/agentic_tracing/utils/api_utils.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/agentic_tracing/utils/create_dataset_schema.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/agentic_tracing/utils/file_name_tracker.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/agentic_tracing/utils/generic.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/agentic_tracing/utils/get_user_trace_metrics.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/agentic_tracing/utils/llm_utils.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/agentic_tracing/utils/model_costs.json +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/agentic_tracing/utils/span_attributes.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/agentic_tracing/utils/supported_llm_provider.toml +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/agentic_tracing/utils/system_monitor.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/agentic_tracing/utils/trace_utils.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/agentic_tracing/utils/unique_decorator.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/agentic_tracing/utils/zip_list_of_unique_files.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/distributed.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/exporters/__init__.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/exporters/file_span_exporter.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/exporters/raga_exporter.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/instrumentators/__init__.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/instrumentators/langchain.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/instrumentators/llamaindex.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/instrumentators/openai.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/langchain_callback.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/llamaindex_callback.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/tracer.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/upload_traces.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/utils/__init__.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/utils/convert_langchain_callbacks_output.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/utils/langchain_tracer_extraction_logic.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/tracers/utils/utils.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst/utils.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst.egg-info/dependency_links.txt +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst.egg-info/requires.txt +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/ragaai_catalyst.egg-info/top_level.txt +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/requirements.txt +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/setup.cfg +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/test/test_catalyst/test_configuration.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/test/test_catalyst/test_dataset.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/test/test_catalyst/test_evaluation.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/test/test_catalyst/test_prompt_manager.py +0 -0
- {ragaai_catalyst-2.1.5b15 → ragaai_catalyst-2.1.5b16}/test/test_catalyst/test_synthetic_data_generation.py +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.2
|
2
2
|
Name: ragaai_catalyst
|
3
|
-
Version: 2.1.
|
3
|
+
Version: 2.1.5b16
|
4
4
|
Summary: RAGA AI CATALYST
|
5
5
|
Author-email: Kiran Scaria <kiran.scaria@raga.ai>, Kedar Gaikwad <kedar.gaikwad@raga.ai>, Dushyant Mahajan <dushyant.mahajan@raga.ai>, Siddhartha Kosti <siddhartha.kosti@raga.ai>, Ritika Goel <ritika.goel@raga.ai>, Vijay Chaurasia <vijay.chaurasia@raga.ai>
|
6
6
|
Requires-Python: <3.13,>=3.9
|
@@ -0,0 +1,318 @@
|
|
1
|
+
from langchain_community.document_loaders import WebBaseLoader
|
2
|
+
from langchain_community.vectorstores import Chroma
|
3
|
+
from langchain_openai import OpenAIEmbeddings
|
4
|
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
5
|
+
from dotenv import load_dotenv
|
6
|
+
import os
|
7
|
+
load_dotenv()
|
8
|
+
from ragaai_catalyst.tracers import Tracer
|
9
|
+
from ragaai_catalyst import RagaAICatalyst, init_tracing
|
10
|
+
from ragaai_catalyst import trace_tool, current_span, trace_agent, trace_llm
|
11
|
+
|
12
|
+
catalyst = RagaAICatalyst(
|
13
|
+
access_key=os.getenv("RAGAAI_CATALYST_ACCESS_KEY"),
|
14
|
+
secret_key=os.getenv("RAGAAI_CATALYST_SECRET_KEY"),
|
15
|
+
base_url=os.getenv("RAGAAI_CATALYST_BASE_URL"),
|
16
|
+
)
|
17
|
+
|
18
|
+
# Initialize tracer
|
19
|
+
tracer = Tracer(
|
20
|
+
project_name="Langgraph_testing",
|
21
|
+
dataset_name="agentic_rag",
|
22
|
+
tracer_type="Agentic",
|
23
|
+
)
|
24
|
+
|
25
|
+
init_tracing(catalyst=catalyst, tracer=tracer)
|
26
|
+
|
27
|
+
urls = [
|
28
|
+
"https://lilianweng.github.io/posts/2023-06-23-agent/",
|
29
|
+
"https://lilianweng.github.io/posts/2023-03-15-prompt-engineering/",
|
30
|
+
"https://lilianweng.github.io/posts/2023-10-25-adv-attack-llm/",
|
31
|
+
]
|
32
|
+
|
33
|
+
docs = [WebBaseLoader(url).load() for url in urls]
|
34
|
+
docs_list = [item for sublist in docs for item in sublist]
|
35
|
+
|
36
|
+
text_splitter = RecursiveCharacterTextSplitter.from_tiktoken_encoder(
|
37
|
+
chunk_size=100, chunk_overlap=50
|
38
|
+
)
|
39
|
+
doc_splits = text_splitter.split_documents(docs_list)
|
40
|
+
|
41
|
+
# Add to vectorDB
|
42
|
+
vectorstore = Chroma.from_documents(
|
43
|
+
documents=doc_splits,
|
44
|
+
collection_name="rag-chroma",
|
45
|
+
embedding=OpenAIEmbeddings(),
|
46
|
+
)
|
47
|
+
retriever = vectorstore.as_retriever()
|
48
|
+
|
49
|
+
from langchain.tools.retriever import create_retriever_tool
|
50
|
+
|
51
|
+
retriever_tool = create_retriever_tool(
|
52
|
+
retriever,
|
53
|
+
"retrieve_blog_posts",
|
54
|
+
"Search and return information about Lilian Weng blog posts on LLM agents, prompt engineering, and adversarial attacks on LLMs.",
|
55
|
+
)
|
56
|
+
|
57
|
+
tools = [retriever_tool]
|
58
|
+
|
59
|
+
from typing import Annotated, Sequence
|
60
|
+
from typing_extensions import TypedDict
|
61
|
+
|
62
|
+
from langchain_core.messages import BaseMessage
|
63
|
+
|
64
|
+
from langgraph.graph.message import add_messages
|
65
|
+
|
66
|
+
|
67
|
+
class AgentState(TypedDict):
|
68
|
+
# The add_messages function defines how an update should be processed
|
69
|
+
# Default is to replace. add_messages says "append"
|
70
|
+
messages: Annotated[Sequence[BaseMessage], add_messages]
|
71
|
+
|
72
|
+
from typing import Annotated, Literal, Sequence
|
73
|
+
from typing_extensions import TypedDict
|
74
|
+
|
75
|
+
from langchain import hub
|
76
|
+
from langchain_core.messages import BaseMessage, HumanMessage
|
77
|
+
from langchain_core.output_parsers import StrOutputParser
|
78
|
+
from langchain_core.prompts import PromptTemplate
|
79
|
+
from langchain_openai import ChatOpenAI
|
80
|
+
|
81
|
+
from pydantic import BaseModel, Field
|
82
|
+
|
83
|
+
|
84
|
+
from langgraph.prebuilt import tools_condition
|
85
|
+
|
86
|
+
### Edges
|
87
|
+
|
88
|
+
@trace_agent("grade_documents")
|
89
|
+
def grade_documents(state) -> Literal["generate", "rewrite"]:
|
90
|
+
"""
|
91
|
+
Determines whether the retrieved documents are relevant to the question.
|
92
|
+
|
93
|
+
Args:
|
94
|
+
state (messages): The current state
|
95
|
+
|
96
|
+
Returns:
|
97
|
+
str: A decision for whether the documents are relevant or not
|
98
|
+
"""
|
99
|
+
|
100
|
+
print("---CHECK RELEVANCE---")
|
101
|
+
|
102
|
+
# Data model
|
103
|
+
class grade(BaseModel):
|
104
|
+
"""Binary score for relevance check."""
|
105
|
+
|
106
|
+
binary_score: str = Field(description="Relevance score 'yes' or 'no'")
|
107
|
+
|
108
|
+
# LLM
|
109
|
+
model = ChatOpenAI(temperature=0, model="gpt-4o", streaming=True)
|
110
|
+
|
111
|
+
# LLM with tool and validation
|
112
|
+
llm_with_tool = model.with_structured_output(grade)
|
113
|
+
|
114
|
+
# Prompt
|
115
|
+
prompt = PromptTemplate(
|
116
|
+
template="""You are a grader assessing relevance of a retrieved document to a user question. \n
|
117
|
+
Here is the retrieved document: \n\n {context} \n\n
|
118
|
+
Here is the user question: {question} \n
|
119
|
+
If the document contains keyword(s) or semantic meaning related to the user question, grade it as relevant. \n
|
120
|
+
Give a binary score 'yes' or 'no' score to indicate whether the document is relevant to the question.""",
|
121
|
+
input_variables=["context", "question"],
|
122
|
+
)
|
123
|
+
|
124
|
+
# Chain
|
125
|
+
chain = prompt | llm_with_tool
|
126
|
+
current_span().add_metrics(
|
127
|
+
name="grade_documents",
|
128
|
+
score=0.9,
|
129
|
+
reasoning="Determine relevance of retrieved documents",
|
130
|
+
cost=0.01,
|
131
|
+
latency=0.5,
|
132
|
+
)
|
133
|
+
|
134
|
+
messages = state["messages"]
|
135
|
+
last_message = messages[-1]
|
136
|
+
|
137
|
+
question = messages[0].content
|
138
|
+
docs = last_message.content
|
139
|
+
|
140
|
+
scored_result = chain.invoke({"question": question, "context": docs})
|
141
|
+
|
142
|
+
score = scored_result.binary_score
|
143
|
+
|
144
|
+
if score == "yes":
|
145
|
+
print("---DECISION: DOCS RELEVANT---")
|
146
|
+
return "generate"
|
147
|
+
|
148
|
+
else:
|
149
|
+
print("---DECISION: DOCS NOT RELEVANT---")
|
150
|
+
print(score)
|
151
|
+
return "rewrite"
|
152
|
+
|
153
|
+
|
154
|
+
### Nodes
|
155
|
+
|
156
|
+
@tracer.trace_agent("agent")
|
157
|
+
def agent(state):
|
158
|
+
"""
|
159
|
+
Invokes the agent model to generate a response based on the current state. Given
|
160
|
+
the question, it will decide to retrieve using the retriever tool, or simply end.
|
161
|
+
|
162
|
+
Args:
|
163
|
+
state (messages): The current state
|
164
|
+
|
165
|
+
Returns:
|
166
|
+
dict: The updated state with the agent response appended to messages
|
167
|
+
"""
|
168
|
+
print("---CALL AGENT---")
|
169
|
+
messages = state["messages"]
|
170
|
+
model = ChatOpenAI(temperature=0, streaming=True, model="gpt-4-turbo")
|
171
|
+
model = model.bind_tools(tools)
|
172
|
+
response = model.invoke(messages)
|
173
|
+
current_span().add_metrics(
|
174
|
+
name="agent",
|
175
|
+
score=0.9,
|
176
|
+
reasoning="Generating response based on current state",
|
177
|
+
cost=0.01,
|
178
|
+
latency=0.5,
|
179
|
+
)
|
180
|
+
# We return a list, because this will get added to the existing list
|
181
|
+
return {"messages": [response]}
|
182
|
+
|
183
|
+
@trace_agent("rewrite")
|
184
|
+
def rewrite(state):
|
185
|
+
"""
|
186
|
+
Transform the query to produce a better question.
|
187
|
+
|
188
|
+
Args:
|
189
|
+
state (messages): The current state
|
190
|
+
|
191
|
+
Returns:
|
192
|
+
dict: The updated state with re-phrased question
|
193
|
+
"""
|
194
|
+
|
195
|
+
print("---TRANSFORM QUERY---")
|
196
|
+
messages = state["messages"]
|
197
|
+
question = messages[0].content
|
198
|
+
|
199
|
+
msg = [
|
200
|
+
HumanMessage(
|
201
|
+
content=f""" \n
|
202
|
+
Look at the input and try to reason about the underlying semantic intent / meaning. \n
|
203
|
+
Here is the initial question:
|
204
|
+
\n ------- \n
|
205
|
+
{question}
|
206
|
+
\n ------- \n
|
207
|
+
Formulate an improved question: """,
|
208
|
+
)
|
209
|
+
]
|
210
|
+
|
211
|
+
# Grader
|
212
|
+
model = ChatOpenAI(temperature=0, model="gpt-4-0125-preview", streaming=True)
|
213
|
+
response = model.invoke(msg)
|
214
|
+
return {"messages": [response]}
|
215
|
+
|
216
|
+
@trace_tool("generate")
|
217
|
+
def generate(state):
|
218
|
+
"""
|
219
|
+
Generate answer
|
220
|
+
|
221
|
+
Args:
|
222
|
+
state (messages): The current state
|
223
|
+
|
224
|
+
Returns:
|
225
|
+
dict: The updated state with re-phrased question
|
226
|
+
"""
|
227
|
+
print("---GENERATE---")
|
228
|
+
messages = state["messages"]
|
229
|
+
question = messages[0].content
|
230
|
+
last_message = messages[-1]
|
231
|
+
|
232
|
+
docs = last_message.content
|
233
|
+
|
234
|
+
# Prompt
|
235
|
+
prompt = hub.pull("rlm/rag-prompt")
|
236
|
+
|
237
|
+
# LLM
|
238
|
+
llm = ChatOpenAI(model_name="gpt-4o-mini", temperature=0, streaming=True)
|
239
|
+
|
240
|
+
# Post-processing
|
241
|
+
def format_docs(docs):
|
242
|
+
return "\n\n".join(doc.page_content for doc in docs)
|
243
|
+
|
244
|
+
# Chain
|
245
|
+
rag_chain = prompt | llm | StrOutputParser()
|
246
|
+
current_span().add_metrics(
|
247
|
+
name="generate",
|
248
|
+
score=0.9,
|
249
|
+
reasoning="Generating response based on current state",
|
250
|
+
cost=0.01,
|
251
|
+
latency=0.5,
|
252
|
+
)
|
253
|
+
|
254
|
+
# Run
|
255
|
+
response = rag_chain.invoke({"context": docs, "question": question})
|
256
|
+
return {"messages": [response]}
|
257
|
+
|
258
|
+
|
259
|
+
print("*" * 20 + "Prompt[rlm/rag-prompt]" + "*" * 20)
|
260
|
+
prompt = hub.pull("rlm/rag-prompt").pretty_print() # Show what the prompt looks like
|
261
|
+
|
262
|
+
from langgraph.graph import END, StateGraph, START
|
263
|
+
from langgraph.prebuilt import ToolNode
|
264
|
+
|
265
|
+
# Define a new graph
|
266
|
+
workflow = StateGraph(AgentState)
|
267
|
+
|
268
|
+
# Define the nodes we will cycle between
|
269
|
+
workflow.add_node("agent", agent) # agent
|
270
|
+
retrieve = ToolNode([retriever_tool])
|
271
|
+
workflow.add_node("retrieve", retrieve) # retrieval
|
272
|
+
workflow.add_node("rewrite", rewrite) # Re-writing the question
|
273
|
+
workflow.add_node(
|
274
|
+
"generate", generate
|
275
|
+
) # Generating a response after we know the documents are relevant
|
276
|
+
# Call agent node to decide to retrieve or not
|
277
|
+
workflow.add_edge(START, "agent")
|
278
|
+
|
279
|
+
# Decide whether to retrieve
|
280
|
+
workflow.add_conditional_edges(
|
281
|
+
"agent",
|
282
|
+
# Assess agent decision
|
283
|
+
tools_condition,
|
284
|
+
{
|
285
|
+
# Translate the condition outputs to nodes in our graph
|
286
|
+
"tools": "retrieve",
|
287
|
+
END: END,
|
288
|
+
},
|
289
|
+
)
|
290
|
+
|
291
|
+
# Edges taken after the `action` node is called.
|
292
|
+
workflow.add_conditional_edges(
|
293
|
+
"retrieve",
|
294
|
+
# Assess agent decision
|
295
|
+
grade_documents,
|
296
|
+
)
|
297
|
+
workflow.add_edge("generate", END)
|
298
|
+
workflow.add_edge("rewrite", "agent")
|
299
|
+
|
300
|
+
# Compile
|
301
|
+
graph = workflow.compile()
|
302
|
+
|
303
|
+
import pprint
|
304
|
+
|
305
|
+
inputs = {
|
306
|
+
"messages": [
|
307
|
+
("user", "What did Lilian Weng write about automatic prompt generation?"),
|
308
|
+
("user", "What did Lilian Weng write about attack on LLMs?"),
|
309
|
+
]
|
310
|
+
}
|
311
|
+
|
312
|
+
with tracer:
|
313
|
+
for output in graph.stream(inputs):
|
314
|
+
for key, value in output.items():
|
315
|
+
pprint.pprint(f"Output from node '{key}':")
|
316
|
+
pprint.pprint("---")
|
317
|
+
pprint.pprint(value, indent=2, width=80, depth=None)
|
318
|
+
pprint.pprint("\n---\n")
|