Skip to content

Openai legacy

ContextRetrieverOpenAIAgent #

Bases: BaseOpenAIAgent

上下文检索器 OpenAI 代理。

该代理在调用 LLM 之前从 BaseRetriever 执行检索。允许它用上下文增强用户消息。

注意:这是一个测试版功能,函数接口可能会发生变化。

Parameters:

Name Type Description Default
tools List[BaseTool]

工具列表。

required
retriever BaseRetriever

一个检索器。

required
qa_prompt Optional[PromptTemplate]

一个问答提示。

required
context_separator str

上下文分隔符。

required
llm Optional[OpenAI]

一个 OpenAI LLM。

required
chat_history Optional[List[ChatMessage]]

一个聊天历史。

required
prefix_messages List[ChatMessage]

List[ChatMessage]: 一个前缀消息列表。

required
verbose bool

是否打印调试语句。

False
max_function_calls int

最大函数调用次数。

DEFAULT_MAX_FUNCTION_CALLS
callback_manager Optional[CallbackManager]

一个回调管理器。

None
Source code in llama_index/agent/openai_legacy/context_retriever_agent.py
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
class ContextRetrieverOpenAIAgent(BaseOpenAIAgent):
    """上下文检索器 OpenAI 代理。

    该代理在调用 LLM 之前从 BaseRetriever 执行检索。允许它用上下文增强用户消息。

    注意:这是一个测试版功能,函数接口可能会发生变化。

    Args:
        tools (List[BaseTool]): 工具列表。
        retriever (BaseRetriever): 一个检索器。
        qa_prompt (Optional[PromptTemplate]): 一个问答提示。
        context_separator (str): 上下文分隔符。
        llm (Optional[OpenAI]): 一个 OpenAI LLM。
        chat_history (Optional[List[ChatMessage]]): 一个聊天历史。
        prefix_messages: List[ChatMessage]: 一个前缀消息列表。
        verbose (bool): 是否打印调试语句。
        max_function_calls (int): 最大函数调用次数。
        callback_manager (Optional[CallbackManager]): 一个回调管理器。"""

    def __init__(
        self,
        tools: List[BaseTool],
        retriever: BaseRetriever,
        qa_prompt: PromptTemplate,
        context_separator: str,
        llm: OpenAI,
        memory: BaseMemory,
        prefix_messages: List[ChatMessage],
        verbose: bool = False,
        max_function_calls: int = DEFAULT_MAX_FUNCTION_CALLS,
        callback_manager: Optional[CallbackManager] = None,
    ) -> None:
        super().__init__(
            llm=llm,
            memory=memory,
            prefix_messages=prefix_messages,
            verbose=verbose,
            max_function_calls=max_function_calls,
            callback_manager=callback_manager,
        )
        self._tools = tools
        self._qa_prompt = qa_prompt
        self._retriever = retriever
        self._context_separator = context_separator

    @classmethod
    def from_tools_and_retriever(
        cls,
        tools: List[BaseTool],
        retriever: BaseRetriever,
        qa_prompt: Optional[PromptTemplate] = None,
        context_separator: str = "\n",
        llm: Optional[LLM] = None,
        chat_history: Optional[List[ChatMessage]] = None,
        memory: Optional[BaseMemory] = None,
        memory_cls: Type[BaseMemory] = ChatMemoryBuffer,
        verbose: bool = False,
        max_function_calls: int = DEFAULT_MAX_FUNCTION_CALLS,
        callback_manager: Optional[CallbackManager] = None,
        system_prompt: Optional[str] = None,
        prefix_messages: Optional[List[ChatMessage]] = None,
    ) -> "ContextRetrieverOpenAIAgent":
        """从检索器创建一个 ContextRetrieverOpenAIAgent。

Args:
    retriever (BaseRetriever): 一个检索器。
    qa_prompt (Optional[PromptTemplate]): 一个问答提示。
    context_separator (str): 上下文分隔符。
    llm (Optional[OpenAI]): 一个 OpenAI LLM。
    chat_history (Optional[ChatMessageHistory]): 一个聊天记录。
    verbose (bool): 是否打印调试语句。
    max_function_calls (int): 最大函数调用次数。
    callback_manager (Optional[CallbackManager]): 一个回调管理器。
"""
        qa_prompt = qa_prompt or DEFAULT_QA_PROMPT
        chat_history = chat_history or []
        llm = llm or Settings.llm
        if not isinstance(llm, OpenAI):
            raise ValueError("llm must be a OpenAI instance")
        if callback_manager is not None:
            llm.callback_manager = callback_manager

        memory = memory or memory_cls.from_defaults(chat_history=chat_history, llm=llm)

        if not is_function_calling_model(llm.model):
            raise ValueError(
                f"Model name {llm.model} does not support function calling API."
            )
        if system_prompt is not None:
            if prefix_messages is not None:
                raise ValueError(
                    "Cannot specify both system_prompt and prefix_messages"
                )
            prefix_messages = [ChatMessage(content=system_prompt, role="system")]

        prefix_messages = prefix_messages or []

        return cls(
            tools=tools,
            retriever=retriever,
            qa_prompt=qa_prompt,
            context_separator=context_separator,
            llm=llm,
            memory=memory,
            prefix_messages=prefix_messages,
            verbose=verbose,
            max_function_calls=max_function_calls,
            callback_manager=callback_manager,
        )

    def _get_tools(self, message: str) -> List[BaseTool]:
        """获取工具。"""
        return self._tools

    def _build_formatted_message(self, message: str) -> str:
        # augment user message
        retrieved_nodes_w_scores: List[NodeWithScore] = self._retriever.retrieve(
            message
        )
        retrieved_nodes = [node.node for node in retrieved_nodes_w_scores]
        retrieved_texts = [node.get_content() for node in retrieved_nodes]

        # format message
        context_str = self._context_separator.join(retrieved_texts)
        return self._qa_prompt.format(context_str=context_str, query_str=message)

    def chat(
        self,
        message: str,
        chat_history: Optional[List[ChatMessage]] = None,
        tool_choice: Union[str, dict] = "auto",
    ) -> AgentChatResponse:
        """聊天。"""
        formatted_message = self._build_formatted_message(message)
        if self._verbose:
            print_text(formatted_message + "\n", color="yellow")

        return super().chat(
            formatted_message, chat_history=chat_history, tool_choice=tool_choice
        )

    async def achat(
        self,
        message: str,
        chat_history: Optional[List[ChatMessage]] = None,
        tool_choice: Union[str, dict] = "auto",
    ) -> AgentChatResponse:
        """聊天。"""
        formatted_message = self._build_formatted_message(message)
        if self._verbose:
            print_text(formatted_message + "\n", color="yellow")

        return await super().achat(
            formatted_message, chat_history=chat_history, tool_choice=tool_choice
        )

    def get_tools(self, message: str) -> List[BaseTool]:
        """获取工具。"""
        return self._get_tools(message)

from_tools_and_retriever classmethod #

from_tools_and_retriever(
    tools: List[BaseTool],
    retriever: BaseRetriever,
    qa_prompt: Optional[PromptTemplate] = None,
    context_separator: str = "\n",
    llm: Optional[LLM] = None,
    chat_history: Optional[List[ChatMessage]] = None,
    memory: Optional[BaseMemory] = None,
    memory_cls: Type[BaseMemory] = ChatMemoryBuffer,
    verbose: bool = False,
    max_function_calls: int = DEFAULT_MAX_FUNCTION_CALLS,
    callback_manager: Optional[CallbackManager] = None,
    system_prompt: Optional[str] = None,
    prefix_messages: Optional[List[ChatMessage]] = None,
) -> ContextRetrieverOpenAIAgent

从检索器创建一个 ContextRetrieverOpenAIAgent。

Parameters:

Name Type Description Default
retriever BaseRetriever

一个检索器。

required
qa_prompt Optional[PromptTemplate]

一个问答提示。

None
context_separator str

上下文分隔符。

'\n'
llm Optional[OpenAI]

一个 OpenAI LLM。

None
chat_history Optional[ChatMessageHistory]

一个聊天记录。

None
verbose bool

是否打印调试语句。

False
max_function_calls int

最大函数调用次数。

DEFAULT_MAX_FUNCTION_CALLS
callback_manager Optional[CallbackManager]

一个回调管理器。

None
Source code in llama_index/agent/openai_legacy/context_retriever_agent.py
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
    @classmethod
    def from_tools_and_retriever(
        cls,
        tools: List[BaseTool],
        retriever: BaseRetriever,
        qa_prompt: Optional[PromptTemplate] = None,
        context_separator: str = "\n",
        llm: Optional[LLM] = None,
        chat_history: Optional[List[ChatMessage]] = None,
        memory: Optional[BaseMemory] = None,
        memory_cls: Type[BaseMemory] = ChatMemoryBuffer,
        verbose: bool = False,
        max_function_calls: int = DEFAULT_MAX_FUNCTION_CALLS,
        callback_manager: Optional[CallbackManager] = None,
        system_prompt: Optional[str] = None,
        prefix_messages: Optional[List[ChatMessage]] = None,
    ) -> "ContextRetrieverOpenAIAgent":
        """从检索器创建一个 ContextRetrieverOpenAIAgent。

Args:
    retriever (BaseRetriever): 一个检索器。
    qa_prompt (Optional[PromptTemplate]): 一个问答提示。
    context_separator (str): 上下文分隔符。
    llm (Optional[OpenAI]): 一个 OpenAI LLM。
    chat_history (Optional[ChatMessageHistory]): 一个聊天记录。
    verbose (bool): 是否打印调试语句。
    max_function_calls (int): 最大函数调用次数。
    callback_manager (Optional[CallbackManager]): 一个回调管理器。
"""
        qa_prompt = qa_prompt or DEFAULT_QA_PROMPT
        chat_history = chat_history or []
        llm = llm or Settings.llm
        if not isinstance(llm, OpenAI):
            raise ValueError("llm must be a OpenAI instance")
        if callback_manager is not None:
            llm.callback_manager = callback_manager

        memory = memory or memory_cls.from_defaults(chat_history=chat_history, llm=llm)

        if not is_function_calling_model(llm.model):
            raise ValueError(
                f"Model name {llm.model} does not support function calling API."
            )
        if system_prompt is not None:
            if prefix_messages is not None:
                raise ValueError(
                    "Cannot specify both system_prompt and prefix_messages"
                )
            prefix_messages = [ChatMessage(content=system_prompt, role="system")]

        prefix_messages = prefix_messages or []

        return cls(
            tools=tools,
            retriever=retriever,
            qa_prompt=qa_prompt,
            context_separator=context_separator,
            llm=llm,
            memory=memory,
            prefix_messages=prefix_messages,
            verbose=verbose,
            max_function_calls=max_function_calls,
            callback_manager=callback_manager,
        )

chat #

chat(
    message: str,
    chat_history: Optional[List[ChatMessage]] = None,
    tool_choice: Union[str, dict] = "auto",
) -> AgentChatResponse

聊天。

Source code in llama_index/agent/openai_legacy/context_retriever_agent.py
163
164
165
166
167
168
169
170
171
172
173
174
175
176
def chat(
    self,
    message: str,
    chat_history: Optional[List[ChatMessage]] = None,
    tool_choice: Union[str, dict] = "auto",
) -> AgentChatResponse:
    """聊天。"""
    formatted_message = self._build_formatted_message(message)
    if self._verbose:
        print_text(formatted_message + "\n", color="yellow")

    return super().chat(
        formatted_message, chat_history=chat_history, tool_choice=tool_choice
    )

achat async #

achat(
    message: str,
    chat_history: Optional[List[ChatMessage]] = None,
    tool_choice: Union[str, dict] = "auto",
) -> AgentChatResponse

聊天。

Source code in llama_index/agent/openai_legacy/context_retriever_agent.py
178
179
180
181
182
183
184
185
186
187
188
189
190
191
async def achat(
    self,
    message: str,
    chat_history: Optional[List[ChatMessage]] = None,
    tool_choice: Union[str, dict] = "auto",
) -> AgentChatResponse:
    """聊天。"""
    formatted_message = self._build_formatted_message(message)
    if self._verbose:
        print_text(formatted_message + "\n", color="yellow")

    return await super().achat(
        formatted_message, chat_history=chat_history, tool_choice=tool_choice
    )

get_tools #

get_tools(message: str) -> List[BaseTool]

获取工具。

Source code in llama_index/agent/openai_legacy/context_retriever_agent.py
193
194
195
def get_tools(self, message: str) -> List[BaseTool]:
    """获取工具。"""
    return self._get_tools(message)

FnRetrieverOpenAIAgent #

Bases: OpenAIAgent

功能:Retriever OpenAI Agent。

使用我们的对象检索器模块来检索openai代理。

注意:这已经被弃用,你可以通过指定以下内容来直接使用基本的OpenAIAgent类:

agent = OpenAIAgent.from_tools(tool_retriever=retriever, ...)

Source code in llama_index/agent/openai_legacy/retriever_openai_agent.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class FnRetrieverOpenAIAgent(OpenAIAgent):
    """功能:Retriever OpenAI Agent。

使用我们的对象检索器模块来检索openai代理。

注意:这已经被弃用,你可以通过指定以下内容来直接使用基本的`OpenAIAgent`类:
```
agent = OpenAIAgent.from_tools(tool_retriever=retriever, ...)
```"""

    @classmethod
    def from_retriever(
        cls, retriever: ObjectRetriever[BaseTool], **kwargs: Any
    ) -> "FnRetrieverOpenAIAgent":
        return cast(
            FnRetrieverOpenAIAgent, cls.from_tools(tool_retriever=retriever, **kwargs)
        )