-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'microsoft:main' into main
- Loading branch information
Showing
44 changed files
with
3,279 additions
and
1,204 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
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
Empty file.
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,24 @@ | ||
from dataclasses import dataclass | ||
from enum import Enum, auto | ||
from typing import Optional | ||
|
||
|
||
class DocumentType(Enum): | ||
""" | ||
Enum for supporting document type. | ||
""" | ||
|
||
TEXT = auto() | ||
HTML = auto() | ||
PDF = auto() | ||
|
||
|
||
@dataclass | ||
class Document: | ||
""" | ||
A wrapper of graph store query results. | ||
""" | ||
|
||
doctype: DocumentType | ||
data: Optional[object] = None | ||
path_or_url: Optional[str] = "" |
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,51 @@ | ||
from dataclasses import dataclass, field | ||
from typing import List, Optional, Protocol | ||
|
||
from .document import Document | ||
|
||
|
||
@dataclass | ||
class GraphStoreQueryResult: | ||
""" | ||
A wrapper of graph store query results. | ||
answer: human readable answer to question/query. | ||
results: intermediate results to question/query, e.g. node entities. | ||
""" | ||
|
||
answer: Optional[str] = None | ||
results: list = field(default_factory=list) | ||
|
||
|
||
class GraphQueryEngine(Protocol): | ||
"""An abstract base class that represents a graph query engine on top of a underlying graph database. | ||
This interface defines the basic methods for graph rag. | ||
""" | ||
|
||
def init_db(self, input_doc: List[Document] | None = None): | ||
""" | ||
This method initializes graph database with the input documents or records. | ||
Usually, it takes the following steps, | ||
1. connecting to a graph database. | ||
2. extract graph nodes, edges based on input data, graph schema and etc. | ||
3. build indexes etc. | ||
Args: | ||
input_doc: a list of input documents that are used to build the graph in database. | ||
Returns: GraphStore | ||
""" | ||
pass | ||
|
||
def add_records(self, new_records: List) -> bool: | ||
""" | ||
Add new records to the underlying database and add to the graph if required. | ||
""" | ||
pass | ||
|
||
def query(self, question: str, n_results: int = 1, **kwargs) -> GraphStoreQueryResult: | ||
""" | ||
This method transform a string format question into database query and return the result. | ||
""" | ||
pass |
56 changes: 56 additions & 0 deletions
56
autogen/agentchat/contrib/graph_rag/graph_rag_capability.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,56 @@ | ||
from autogen.agentchat.contrib.capabilities.agent_capability import AgentCapability | ||
from autogen.agentchat.conversable_agent import ConversableAgent | ||
|
||
from .graph_query_engine import GraphQueryEngine | ||
|
||
|
||
class GraphRagCapability(AgentCapability): | ||
""" | ||
A graph rag capability uses a graph query engine to give a conversable agent the graph rag ability. | ||
An agent class with graph rag capability could | ||
1. create a graph in the underlying database with input documents. | ||
2. retrieved relevant information based on messages received by the agent. | ||
3. generate answers from retrieved information and send messages back. | ||
For example, | ||
graph_query_engine = GraphQueryEngine(...) | ||
graph_query_engine.init_db([Document(doc1), Document(doc2), ...]) | ||
graph_rag_agent = ConversableAgent( | ||
name="graph_rag_agent", | ||
max_consecutive_auto_reply=3, | ||
... | ||
) | ||
graph_rag_capability = GraphRagCapbility(graph_query_engine) | ||
graph_rag_capability.add_to_agent(graph_rag_agent) | ||
user_proxy = UserProxyAgent( | ||
name="user_proxy", | ||
code_execution_config=False, | ||
is_termination_msg=lambda msg: "TERMINATE" in msg["content"], | ||
human_input_mode="ALWAYS", | ||
) | ||
user_proxy.initiate_chat(graph_rag_agent, message="Name a few actors who've played in 'The Matrix'") | ||
# ChatResult( | ||
# chat_id=None, | ||
# chat_history=[ | ||
# {'content': 'Name a few actors who've played in \'The Matrix\'', 'role': 'graph_rag_agent'}, | ||
# {'content': 'A few actors who have played in The Matrix are: | ||
# - Keanu Reeves | ||
# - Laurence Fishburne | ||
# - Carrie-Anne Moss | ||
# - Hugo Weaving', | ||
# 'role': 'user_proxy'}, | ||
# ...) | ||
""" | ||
|
||
def __init__(self, query_engine: GraphQueryEngine): | ||
""" | ||
initialize graph rag capability with a graph query engine | ||
""" | ||
... | ||
|
||
def add_to_agent(self, agent: ConversableAgent): ... |
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
37 changes: 37 additions & 0 deletions
37
dotnet/sample/AutoGen.OpenAI.Sample/Connect_To_OpenAI_o1_preview.cs
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,37 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Connect_To_OpenAI_o1_preview.cs | ||
|
||
using AutoGen.Core; | ||
using OpenAI; | ||
|
||
namespace AutoGen.OpenAI.Sample; | ||
|
||
public class Connect_To_OpenAI_o1_preview | ||
{ | ||
public static async Task RunAsync() | ||
{ | ||
#region create_agent | ||
var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new InvalidOperationException("Please set environment variable OPENAI_API_KEY"); | ||
var openAIClient = new OpenAIClient(apiKey); | ||
|
||
// until 2024/09/12 | ||
// openai o1-preview doesn't support systemMessage, temperature, maxTokens, streaming output | ||
// so in order to use OpenAIChatAgent with o1-preview, you need to set those parameters to null | ||
var agent = new OpenAIChatAgent( | ||
chatClient: openAIClient.GetChatClient("o1-preview"), | ||
name: "assistant", | ||
systemMessage: null, | ||
temperature: null, | ||
maxTokens: null, | ||
seed: 0) | ||
// by using RegisterMiddleware instead of RegisterStreamingMiddleware | ||
// it turns an IStreamingAgent into an IAgent and disables streaming | ||
.RegisterMiddleware(new OpenAIChatRequestMessageConnector()) | ||
.RegisterPrintMessage(); | ||
#endregion create_agent | ||
|
||
#region send_message | ||
await agent.SendAsync("Can you write a piece of C# code to calculate 100th of fibonacci?"); | ||
#endregion send_message | ||
} | ||
} |
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
Oops, something went wrong.