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

Support to Custom Chains #35

Closed
rogalvil opened this issue May 11, 2023 · 3 comments · Fixed by #48
Closed

Support to Custom Chains #35

rogalvil opened this issue May 11, 2023 · 3 comments · Fixed by #48
Labels
question Further information is requested

Comments

@rogalvil
Copy link
Contributor

I have a custom chain for Conversational Retrieval With Source Chain because currently not exist support on langchian

class ConversationalRetrievalWithSourcesChain(ConversationalRetrievalChain):
    """Chain for chatting with sources over documents."""

    sources_output_key: str = "sources"  #: :meta private:

    @property
    def output_keys(self) -> List[str]:
        """Return the output keys.

        :meta private:
        """
        _output_keys = [self.output_key]
        _output_keys.append(self.sources_output_key)
        if self.return_source_documents:
            _output_keys = _output_keys + ["source_documents"]
        return _output_keys

    def _call(
        self,
        inputs: Dict[str, Any],
        run_manager: Optional[CallbackManagerForChainRun] = None,
    ) -> Dict[str, Any]:
        result = super()._call(
            inputs=inputs,
            run_manager=run_manager,
        )
        answer = result[self.output_key]
        if re.search(r"SOURCES:\s", answer):
            answer, sources = re.split(r"SOURCES:\s", answer)
        else:
            sources = ""
        result[self.output_key] = answer
        result[self.sources_output_key] = sources
        return result

    async def _acall(
        self,
        inputs: Dict[str, Any],
        run_manager: Optional[AsyncCallbackManagerForChainRun] = None,
    ) -> Dict[str, Any]:
        result = await super()._acall(
            inputs=inputs,
            run_manager=run_manager,
        )
        answer = result[self.output_key]
        if re.search(r"SOURCES:\s", answer):
            answer, sources = re.split(r"SOURCES:\s", answer)
        else:
            sources = ""
        result[self.output_key] = answer
        result[self.sources_output_key] = sources
        return result

I love a new feat: auto-detect callback

But now I receive a this kind of errors
Captura de pantalla 2023-05-11 a la(s) 1 46 34

Any idea to include my custom chain on auto-detect callback

@rogalvil rogalvil added the feature New feature or request label May 11, 2023
@ajndkr
Copy link
Owner

ajndkr commented May 11, 2023

glad you like the new feature!

We really need a docs page for this project 😅

I see 2 approaches here:

Approach 1:

You define a new callback handler called AsyncConversationalRetrievalWithSourcesChainStreamingCallback and register it as follows:

@register_streaming_callback("ConversationalRetrievalWithSourcesChain")
class AsyncConversationalRetrievalWithSourcesChainStreamingCallback(
    AsyncConversationalRetrievalChainStreamingCallback
):
    """AsyncStreamingResponseCallback handler for ConversationalRetrievalWithSourcesChain."""

    pass

Approach 2:

We update fastapi_async_langchain to auto-detect using chain class name (as we do now) but if the chain type is not supported, we search for the inherit chain class name. This could work for your use-case but I sense there might be bugs down this road as some custom chains will simply inherit from Chain and it might break the application.

I think approach 1 is a better solution. Try it out and let me know how it goes!

@ajndkr ajndkr added question Further information is requested and removed feature New feature or request labels May 11, 2023
@ajndkr
Copy link
Owner

ajndkr commented May 18, 2023

@rogalvil fyi: https://lanarky.readthedocs.io/en/latest/advanced/custom_callbacks.html

feel free to make suggestions via pr.

@rogalvil
Copy link
Contributor Author

@ajndkr Cool, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants