Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ianchi
Copy link
Contributor

@ianchi ianchi commented Dec 24, 2024

Add option to return content and artifacts, to also be able to access the full info of the retrieved documents.

They are returned as a list of dicts in the artifacts property if parameter response_format is set to "content_and_artifact".

Defaults to "content" to keep current behavior.

@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Dec 24, 2024
Copy link

vercel bot commented Dec 24, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
langchain ⬜️ Ignored (Inspect) Visit Preview Dec 24, 2024 4:00pm

@dosubot dosubot bot added Ɑ: core Related to langchain-core Ɑ: retriever Related to retriever module labels Dec 24, 2024
Copy link
Member

@efriis efriis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 questions/suggestions, and will need some unit tests to be mergable

[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])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might make sense to just return docs as the artifact - any reason to dump here in particular?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason is to always have the same type after deserialization.

As artifact has type Any when deserializing a ToolMessage from json or any other source (api call, DB, etc), Pydantic won't be able to do any "magic" to regenerate the artifact as a list[Document] but it will be a plain list[dict].

So, to be consistent and always have a dict I opted to dump.
Otherwise you need to add custom logic when deserializing to have consistent types.

format_document(doc, document_prompt) for doc in docs
)
if response_format == "content_and_artifact":
return (content, [doc.model_dump() for doc in docs])
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same reasoning as above

@@ -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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs to be added to docstring

@efriis efriis self-assigned this Dec 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ɑ: core Related to langchain-core Ɑ: retriever Related to retriever module size:S This PR changes 10-29 lines, ignoring generated files.
Projects
Status: In review
Development

Successfully merging this pull request may close these issues.

2 participants