-
Notifications
You must be signed in to change notification settings - Fork 2
[WIP]LangSmithを使用している箇所をWeaveに変更 #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
23458ff
a6b6170
3b992bd
8ed90bb
d64e822
f709f24
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,5 @@ | ||
| OPENAI_API_KEY= | ||
| LANGCHAIN_TRACING_V2=false | ||
| LANGCHAIN_ENDPOINT=https://api.smith.langchain.com | ||
| LANGCHAIN_API_KEY= | ||
| LANGCHAIN_PROJECT=training-llm-app | ||
| WANDB_API_KEY= | ||
| WEAVE_PROJECT_NAME= | ||
| COHERE_API_KEY= | ||
| TAVILY_API_KEY= |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,42 +1,22 @@ | ||
| from typing import Generator | ||
|
|
||
| import weave | ||
| from langchain.embeddings import init_embeddings | ||
| from langchain_chroma import Chroma | ||
| from langchain_core.documents import Document | ||
| from langchain_core.language_models import BaseChatModel | ||
| from langchain_core.load import dumps, loads | ||
| from langsmith import traceable | ||
| from pydantic import BaseModel, Field | ||
|
|
||
| from app.advanced_rag.chains.base import AnswerToken, BaseRAGChain, Context, reduce_fn | ||
| from app.advanced_rag.chains.base import AnswerToken, BaseRAGChain, Context | ||
| from app.prompts import generate_answer_prompt, query_generation_prompt | ||
|
|
||
|
|
||
| class QueryGenerationOutput(BaseModel): | ||
| queries: list[str] = Field(..., description="検索クエリのリスト") | ||
|
|
||
|
|
||
| _query_generation_prompt_template = """\ | ||
| 質問に対してベクターデータベースから関連文書を検索するために、 | ||
| 3つの異なる検索クエリを生成してください。 | ||
| 距離ベースの類似性検索の限界を克服するために、 | ||
| ユーザーの質問に対して複数の視点を提供することが目標です。 | ||
|
|
||
| 質問: {question} | ||
| """ | ||
|
|
||
|
|
||
| _generate_answer_prompt_template = ''' | ||
| 以下の文脈だけを踏まえて質問に回答してください。 | ||
|
|
||
| 文脈: """ | ||
| {context} | ||
| """ | ||
|
|
||
| 質問: {question} | ||
| ''' | ||
|
|
||
|
|
||
| @traceable | ||
| @weave.op | ||
| def _reciprocal_rank_fusion( | ||
| retriever_outputs: list[list[Document]], | ||
| k: int = 60, | ||
|
|
@@ -75,15 +55,15 @@ def __init__(self, model: BaseChatModel): | |
| ) | ||
| self.retriever = vector_store.as_retriever(search_kwargs={"k": 5}) | ||
|
|
||
| @traceable(name="rag_fusion", reduce_fn=reduce_fn) | ||
| @weave.op(name="rag_fusion") | ||
| def stream(self, question: str) -> Generator[Context | AnswerToken, None, None]: | ||
| # 検索クエリを生成する | ||
|
Comment on lines
+58
to
60
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: # First, let's find and examine the base.py file
find . -name "base.py" -path "*/advanced_rag/chains/*" | head -5Repository: GenerativeAgents/training-llm-application-development Length of output: 130 🏁 Script executed: # Check the structure of advanced_rag/chains directory
ls -la app/advanced_rag/chains/ 2>/dev/null | head -20Repository: GenerativeAgents/training-llm-application-development Length of output: 744 🏁 Script executed: # Get the BaseRAGChain class definition and stream method
rg -A 10 "class BaseRAGChain" app/advanced_rag/chains/base.pyRepository: GenerativeAgents/training-llm-application-development Length of output: 390 🏁 Script executed: # Check what WeaveCallId is and where it's defined
rg "WeaveCallId" app/advanced_rag/chains/Repository: GenerativeAgents/training-llm-application-development Length of output: 1836 🏁 Script executed: # Check the current rag_fusion.py implementation
cat -n app/advanced_rag/chains/rag_fusion.pyRepository: GenerativeAgents/training-llm-application-development Length of output: 4046 🏁 Script executed: # Check if WeaveCallId is imported in rag_fusion.py
rg "import.*WeaveCallId|from.*WeaveCallId" app/advanced_rag/chains/rag_fusion.pyRepository: GenerativeAgents/training-llm-application-development Length of output: 79 🏁 Script executed: # Check the stream method in rag_fusion.py around line 78-80
sed -n '70,100p' app/advanced_rag/chains/rag_fusion.py | cat -nRepository: GenerativeAgents/training-llm-application-development Length of output: 1585
🤖 Prompt for AI Agents |
||
| query_generation_prompt = _query_generation_prompt_template.format( | ||
| query_generation_prompt_text = query_generation_prompt.format( | ||
| question=question | ||
| ) | ||
| model_with_structure = self.model.with_structured_output(QueryGenerationOutput) | ||
| query_generation_output: QueryGenerationOutput = model_with_structure.invoke( | ||
| query_generation_prompt | ||
| query_generation_prompt_text | ||
| ) # type: ignore[assignment] | ||
|
|
||
| # 検索する | ||
|
|
@@ -95,11 +75,11 @@ def stream(self, question: str) -> Generator[Context | AnswerToken, None, None]: | |
| yield Context(documents=documents) | ||
|
|
||
| # 回答を生成して徐々に応答を返す | ||
| generate_answer_prompt = _generate_answer_prompt_template.format( | ||
| generate_answer_prompt_text = generate_answer_prompt.format( | ||
| context=documents, | ||
| question=question, | ||
| ) | ||
| for chunk in self.model.stream(generate_answer_prompt): | ||
| for chunk in self.model.stream(generate_answer_prompt_text): | ||
| yield AnswerToken(token=chunk.content) | ||
|
|
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stream()の型拡張に合わせてreduce_fn()もWeaveCallIdを受けられるようにしておくと安全ですBaseRAGChain.stream()がWeaveCallIdをyieldする前提になったので、reduce_fn()が同じストリーム(or 収集済みchunks)を扱う経路があると型/実装がズレます(Line 24-26 vs Line 30)。WeaveCallIdは集約では無視でOKだと思うので、明示的にスキップすると事故が減ります。修正案(WeaveCallIdを無視して集約)
Also applies to: 30-44
🤖 Prompt for AI Agents