Home / Function/ ainvoke() — langchain Function Reference

ainvoke() — langchain Function Reference

Architecture documentation for the ainvoke() function in base.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  5912fbae_35d0_fc72_46fb_b0f27a511df7["ainvoke()"]
  40171661_732e_8178_c8ae_92254ace13fe["OpenAIAssistantRunnable"]
  5912fbae_35d0_fc72_46fb_b0f27a511df7 -->|defined in| 40171661_732e_8178_c8ae_92254ace13fe
  633db7c5_84a0_dcf2_9949_8d11da115c78["_aparse_intermediate_steps()"]
  5912fbae_35d0_fc72_46fb_b0f27a511df7 -->|calls| 633db7c5_84a0_dcf2_9949_8d11da115c78
  9a8d9eab_65ef_1fd7_ee85_d9451c12e0a0["_acreate_thread_and_run()"]
  5912fbae_35d0_fc72_46fb_b0f27a511df7 -->|calls| 9a8d9eab_65ef_1fd7_ee85_d9451c12e0a0
  05f5267c_cc58_5dc4_e414_e966f46d6c91["_acreate_run()"]
  5912fbae_35d0_fc72_46fb_b0f27a511df7 -->|calls| 05f5267c_cc58_5dc4_e414_e966f46d6c91
  b2a730ee_719c_3ee4_1644_9a171f0d59ee["_await_for_run()"]
  5912fbae_35d0_fc72_46fb_b0f27a511df7 -->|calls| b2a730ee_719c_3ee4_1644_9a171f0d59ee
  e5873e73_5275_0690_9701_42a206967e08["_aget_response()"]
  5912fbae_35d0_fc72_46fb_b0f27a511df7 -->|calls| e5873e73_5275_0690_9701_42a206967e08
  style 5912fbae_35d0_fc72_46fb_b0f27a511df7 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain/langchain_classic/agents/openai_assistant/base.py lines 421–518

    async def ainvoke(
        self,
        input: dict,
        config: RunnableConfig | None = None,
        **kwargs: Any,
    ) -> OutputType:
        """Async invoke assistant.

        Args:
            input: Runnable input dict that can have:
                content: User message when starting a new run.
                thread_id: Existing thread to use.
                run_id: Existing run to use. Should only be supplied when providing
                    the tool output for a required action after an initial invocation.
                message_metadata: Metadata to associate with a new message.
                thread_metadata: Metadata to associate with new thread. Only relevant
                    when a new thread is created.
                instructions: Overrides the instructions of the assistant.
                additional_instructions: Appends additional instructions.
                model: Override Assistant model for this run.
                tools: Override Assistant tools for this run.
                parallel_tool_calls: Allow Assistant to set parallel_tool_calls
                    for this run.
                top_p: Override Assistant top_p for this run.
                temperature: Override Assistant temperature for this run.
                max_completion_tokens: Allow setting max_completion_tokens for this run.
                max_prompt_tokens: Allow setting max_prompt_tokens for this run.
                run_metadata: Metadata to associate with new run.
            config: Runnable config.
            kwargs: Additional arguments.

        Returns:
            If self.as_agent, will return
                Union[List[OpenAIAssistantAction], OpenAIAssistantFinish].
                Otherwise, will return OpenAI types
                Union[List[ThreadMessage], List[RequiredActionFunctionToolCall]].
        """
        config = config or {}
        callback_manager = CallbackManager.configure(
            inheritable_callbacks=config.get("callbacks"),
            inheritable_tags=config.get("tags"),
            inheritable_metadata=config.get("metadata"),
        )
        run_manager = callback_manager.on_chain_start(
            dumpd(self),
            input,
            name=config.get("run_name") or self.get_name(),
        )
        try:
            # Being run within AgentExecutor and there are tool outputs to submit.
            if self.as_agent and input.get("intermediate_steps"):
                tool_outputs = await self._aparse_intermediate_steps(
                    input["intermediate_steps"],
                )
                run = await self.async_client.beta.threads.runs.submit_tool_outputs(
                    **tool_outputs,
                )
            # Starting a new thread and a new run.
            elif "thread_id" not in input:
                thread = {
                    "messages": [
                        {
                            "role": "user",
                            "content": input["content"],
                            "metadata": input.get("message_metadata"),
                        },
                    ],
                    "metadata": input.get("thread_metadata"),
                }
                run = await self._acreate_thread_and_run(input, thread)
            # Starting a new run in an existing thread.
            elif "run_id" not in input:
                _ = await self.async_client.beta.threads.messages.create(
                    input["thread_id"],
                    content=input["content"],
                    role="user",
                    metadata=input.get("message_metadata"),
                )
                run = await self._acreate_run(input)
            # Submitting tool outputs to an existing run, outside the AgentExecutor
            # framework.

Subdomains

Frequently Asked Questions

What does ainvoke() do?
ainvoke() is a function in the langchain codebase, defined in libs/langchain/langchain_classic/agents/openai_assistant/base.py.
Where is ainvoke() defined?
ainvoke() is defined in libs/langchain/langchain_classic/agents/openai_assistant/base.py at line 421.
What does ainvoke() call?
ainvoke() calls 5 function(s): _acreate_run, _acreate_thread_and_run, _aget_response, _aparse_intermediate_steps, _await_for_run.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free