run-llama / llama_index

LlamaIndex is a data framework for your LLM applications
https://docs.llamaindex.ai
MIT License
35.89k stars 5.09k forks source link

[Documentation]: openai_agent example is incompatible with latest openai library #11579

Closed adieyal closed 3 months ago

adieyal commented 7 months ago

Documentation Issue Description

The openai_agent example and accompanying notebook are not compatible with the latest openai library https://github.com/run-llama/llama_index/blob/main/docs/examples/agent/openai_agent.ipynb https://docs.llamaindex.ai/en/stable/examples/agent/openai_agent.html

def _call_function(self, tool_call: dict) -> ChatMessage:
        id_ = tool_call["id"]
        function_call = tool_call["function"]
        tool = self._tools[function_call["name"]]
        output = tool(**json.loads(function_call["arguments"]))
        return ChatMessage(
            name=function_call["name"],
            content=str(output),
            role="tool",
            additional_kwargs={
                "tool_call_id": id_,
                "name": function_call["name"],
            },
        )

should be replaced with

def _call_function(self, tool_call) -> ChatMessage:
        id_ = tool_call.id
        function_call = tool_call.function
        tool = self._tools[function_call.name]
        output = tool(**json.loads(function_call.arguments))
        return ChatMessage(
            name=function_call.name,
            content=str(output),
            role="tool",
            additional_kwargs={
                "tool_call_id": id_,
                "name": function_call.name,
            },
        )

strictly speaking tool_call is of type openai.types.chat.chat_completion_message_tool_call.ChatCompletionMessageToolCall and no longer dict - I am not sure whether there is an intention to abstract away the OpenAI types

Documentation Link

https://docs.llamaindex.ai/en/stable/examples/agent/openai_agent.html

dosubot[bot] commented 7 months ago

Hey @adieyal, great to see you diving into the depths of LlamaIndex again! 👾

Thank you for bringing this to our attention. It seems like there has been a change in the OpenAI library that is causing incompatibility with the current LlamaIndex codebase.

The change you've suggested, moving from dictionary access to attribute access, is indeed the correct way to handle instances of the ChatCompletionMessageToolCall class in the latest OpenAI library.

However, as you've pointed out, this change would mean that the LlamaIndex codebase is directly dependent on the OpenAI library's types, which might not be the intended design. The LlamaIndex framework is designed to be a general-purpose tool that can work with different libraries, so it's possible that the use of dictionaries was an attempt to keep the codebase library-agnostic.

In the meantime, you can use the version of the OpenAI library that is compatible with the current LlamaIndex codebase. According to the context provided, the compatible version of the OpenAI library is not explicitly mentioned, but it seems to be compatible with the version "^0.1.1" as per the llama-index-integrations/llms/llama-index-llms-azure-openai/pyproject.toml file.

We will take your feedback into account and consider updating the LlamaIndex codebase to be compatible with the latest version of the OpenAI library, or at least provide clear documentation on which version of the OpenAI library the codebase is compatible with.

For future reference, it would be helpful to know the exact version of the OpenAI library you're using when you encounter these issues. This will help us ensure compatibility with the most commonly used versions.

Thank you for your contribution to improving the LlamaIndex project. Your feedback is greatly appreciated. If you have any more questions or issues, feel free to ask.

Link to Dosu's profile

Sources

#### About Dosu This response is meant to be useful and save you time. It is **not meant to be a precise solution**, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot.
logan-markewich commented 7 months ago

To be clear, this is just the portion of the example showing creating your own agent, not our actual agent code in the framework

adieyal commented 7 months ago

yep - but it's worth updating the documentation in case someone following along encounters the error and doesn't know how to proceed.