-
Notifications
You must be signed in to change notification settings - Fork 15.8k
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
core: allow artifact in create_retriever_tool #28903
base: master
Are you sure you want to change the base?
Changes from all commits
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,7 @@ | ||
from __future__ import annotations | ||
|
||
from functools import partial | ||
from typing import Optional | ||
from typing import Any, Literal, Optional, Union | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
|
@@ -28,11 +28,16 @@ def _get_relevant_documents( | |
document_prompt: BasePromptTemplate, | ||
document_separator: str, | ||
callbacks: Callbacks = None, | ||
) -> str: | ||
response_format: Literal["content", "content_and_artifact"] = "content", | ||
) -> Union[str, tuple[str, list[dict[str, Any]]]]: | ||
docs = retriever.invoke(query, config={"callbacks": callbacks}) | ||
return document_separator.join( | ||
content = document_separator.join( | ||
format_document(doc, document_prompt) for doc in docs | ||
) | ||
if response_format == "content_and_artifact": | ||
return (content, [doc.model_dump() for doc in docs]) | ||
|
||
return content | ||
|
||
|
||
async def _aget_relevant_documents( | ||
|
@@ -41,12 +46,18 @@ async def _aget_relevant_documents( | |
document_prompt: BasePromptTemplate, | ||
document_separator: str, | ||
callbacks: Callbacks = None, | ||
) -> str: | ||
response_format: Literal["content", "content_and_artifact"] = "content", | ||
) -> Union[str, tuple[str, list[dict[str, Any]]]]: | ||
docs = await retriever.ainvoke(query, config={"callbacks": callbacks}) | ||
return document_separator.join( | ||
content = document_separator.join( | ||
[await aformat_document(doc, document_prompt) for doc in docs] | ||
) | ||
|
||
if response_format == "content_and_artifact": | ||
return (content, [doc.model_dump() for doc in docs]) | ||
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. might make sense to just return 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. The reason is to always have the same type after deserialization. As So, to be consistent and always have a dict I opted to dump. |
||
|
||
return content | ||
|
||
|
||
def create_retriever_tool( | ||
retriever: BaseRetriever, | ||
|
@@ -55,6 +66,7 @@ def create_retriever_tool( | |
*, | ||
document_prompt: Optional[BasePromptTemplate] = None, | ||
document_separator: str = "\n\n", | ||
response_format: Literal["content", "content_and_artifact"] = "content", | ||
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. needs to be added to docstring |
||
) -> Tool: | ||
"""Create a tool to do retrieval of documents. | ||
|
||
|
@@ -76,17 +88,20 @@ def create_retriever_tool( | |
retriever=retriever, | ||
document_prompt=document_prompt, | ||
document_separator=document_separator, | ||
response_format=response_format, | ||
) | ||
afunc = partial( | ||
_aget_relevant_documents, | ||
retriever=retriever, | ||
document_prompt=document_prompt, | ||
document_separator=document_separator, | ||
response_format=response_format, | ||
) | ||
return Tool( | ||
name=name, | ||
description=description, | ||
func=func, | ||
coroutine=afunc, | ||
args_schema=RetrieverInput, | ||
response_format=response_format, | ||
) |
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.
same q as on async one
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.
Same reasoning as above