-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
81 changed files
with
3,412 additions
and
523 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,91 @@ | ||
# Source code | ||
*.bash text eol=lf | ||
*.bat text eol=crlf | ||
*.cmd text eol=crlf | ||
*.coffee text | ||
*.css text diff=css eol=lf | ||
*.htm text diff=html eol=lf | ||
*.html text diff=html eol=lf | ||
*.inc text | ||
*.ini text | ||
*.js text | ||
*.json text eol=lf | ||
*.jsx text | ||
*.less text | ||
*.ls text | ||
*.map text -diff | ||
*.od text | ||
*.onlydata text | ||
*.php text diff=php | ||
*.pl text | ||
*.ps1 text eol=crlf | ||
*.py text diff=python eol=lf | ||
*.rb text diff=ruby eol=lf | ||
*.sass text | ||
*.scm text | ||
*.scss text diff=css | ||
*.sh text eol=lf | ||
.husky/* text eol=lf | ||
*.sql text | ||
*.styl text | ||
*.tag text | ||
*.ts text | ||
*.tsx text | ||
*.xml text | ||
*.xhtml text diff=html | ||
|
||
# Docker | ||
Dockerfile text eol=lf | ||
|
||
# Documentation | ||
*.ipynb text | ||
*.markdown text diff=markdown eol=lf | ||
*.md text diff=markdown eol=lf | ||
*.mdwn text diff=markdown eol=lf | ||
*.mdown text diff=markdown eol=lf | ||
*.mkd text diff=markdown eol=lf | ||
*.mkdn text diff=markdown eol=lf | ||
*.mdtxt text eol=lf | ||
*.mdtext text eol=lf | ||
*.txt text eol=lf | ||
AUTHORS text eol=lf | ||
CHANGELOG text eol=lf | ||
CHANGES text eol=lf | ||
CONTRIBUTING text eol=lf | ||
COPYING text eol=lf | ||
copyright text eol=lf | ||
*COPYRIGHT* text eol=lf | ||
INSTALL text eol=lf | ||
license text eol=lf | ||
LICENSE text eol=lf | ||
NEWS text eol=lf | ||
readme text eol=lf | ||
*README* text eol=lf | ||
TODO text | ||
|
||
# Configs | ||
*.cnf text eol=lf | ||
*.conf text eol=lf | ||
*.config text eol=lf | ||
.editorconfig text | ||
.env text eol=lf | ||
.gitattributes text eol=lf | ||
.gitconfig text eol=lf | ||
.htaccess text | ||
*.lock text -diff | ||
package.json text eol=lf | ||
package-lock.json text eol=lf -diff | ||
pnpm-lock.yaml text eol=lf -diff | ||
.prettierrc text | ||
yarn.lock text -diff | ||
*.toml text eol=lf | ||
*.yaml text eol=lf | ||
*.yml text eol=lf | ||
browserslist text | ||
Makefile text eol=lf | ||
makefile text eol=lf | ||
|
||
# Images | ||
*.png filter=lfs diff=lfs merge=lfs -text | ||
*.jpg filter=lfs diff=lfs merge=lfs -text | ||
*.jpeg filter=lfs diff=lfs merge=lfs -text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
autogen/agentchat/contrib/llamaindex_conversable_agent.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
from typing import Dict, List, Optional, Tuple, Union | ||
|
||
from autogen import OpenAIWrapper | ||
from autogen.agentchat import Agent, ConversableAgent | ||
from autogen.agentchat.contrib.vectordb.utils import get_logger | ||
|
||
logger = get_logger(__name__) | ||
|
||
try: | ||
from llama_index.core.agent.runner.base import AgentRunner | ||
from llama_index.core.chat_engine.types import AgentChatResponse | ||
from llama_index_client import ChatMessage | ||
except ImportError as e: | ||
logger.fatal("Failed to import llama-index. Try running 'pip install llama-index'") | ||
raise e | ||
|
||
|
||
class LLamaIndexConversableAgent(ConversableAgent): | ||
|
||
def __init__( | ||
self, | ||
name: str, | ||
llama_index_agent: AgentRunner, | ||
description: Optional[str] = None, | ||
**kwargs, | ||
): | ||
""" | ||
Args: | ||
name (str): agent name. | ||
llama_index_agent (AgentRunner): llama index agent. | ||
Please override this attribute if you want to reprogram the agent. | ||
description (str): a short description of the agent. This description is used by other agents | ||
(e.g. the GroupChatManager) to decide when to call upon this agent. | ||
**kwargs (dict): Please refer to other kwargs in | ||
[ConversableAgent](../conversable_agent#__init__). | ||
""" | ||
|
||
if llama_index_agent is None: | ||
raise ValueError("llama_index_agent must be provided") | ||
|
||
if description is None or description.isspace(): | ||
raise ValueError("description must be provided") | ||
|
||
super().__init__( | ||
name, | ||
description=description, | ||
**kwargs, | ||
) | ||
|
||
self._llama_index_agent = llama_index_agent | ||
|
||
# Override the `generate_oai_reply` | ||
self.replace_reply_func(ConversableAgent.generate_oai_reply, LLamaIndexConversableAgent._generate_oai_reply) | ||
|
||
self.replace_reply_func(ConversableAgent.a_generate_oai_reply, LLamaIndexConversableAgent._a_generate_oai_reply) | ||
|
||
def _generate_oai_reply( | ||
self, | ||
messages: Optional[List[Dict]] = None, | ||
sender: Optional[Agent] = None, | ||
config: Optional[OpenAIWrapper] = None, | ||
) -> Tuple[bool, Union[str, Dict, None]]: | ||
"""Generate a reply using autogen.oai.""" | ||
user_message, history = self._extract_message_and_history(messages=messages, sender=sender) | ||
|
||
chatResponse: AgentChatResponse = self._llama_index_agent.chat(message=user_message, chat_history=history) | ||
|
||
extracted_response = chatResponse.response | ||
|
||
return (True, extracted_response) | ||
|
||
async def _a_generate_oai_reply( | ||
self, | ||
messages: Optional[List[Dict]] = None, | ||
sender: Optional[Agent] = None, | ||
config: Optional[OpenAIWrapper] = None, | ||
) -> Tuple[bool, Union[str, Dict, None]]: | ||
"""Generate a reply using autogen.oai.""" | ||
user_message, history = self._extract_message_and_history(messages=messages, sender=sender) | ||
|
||
chatResponse: AgentChatResponse = await self._llama_index_agent.achat( | ||
message=user_message, chat_history=history | ||
) | ||
|
||
extracted_response = chatResponse.response | ||
|
||
return (True, extracted_response) | ||
|
||
def _extract_message_and_history( | ||
self, messages: Optional[List[Dict]] = None, sender: Optional[Agent] = None | ||
) -> Tuple[str, List[ChatMessage]]: | ||
"""Extract the message and history from the messages.""" | ||
if not messages: | ||
messages = self._oai_messages[sender] | ||
|
||
if not messages: | ||
return "", [] | ||
|
||
message = messages[-1].get("content", "") | ||
|
||
history = messages[:-1] | ||
history_messages: List[ChatMessage] = [] | ||
for history_message in history: | ||
content = history_message.get("content", "") | ||
role = history_message.get("role", "user") | ||
if role: | ||
if role == "user" or role == "assistant": | ||
history_messages.append(ChatMessage(content=content, role=role, additional_kwargs={})) | ||
return message, history_messages |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.