-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from difizen/brokun_dev
refactor(API): core agent
- Loading branch information
Showing
15 changed files
with
277 additions
and
259 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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'''Agent''' | ||
from typing import AsyncIterator, List | ||
from abc import ABC, abstractmethod | ||
from langchain.schema.messages import BaseMessage, BaseMessageChunk | ||
from pydantic import BaseModel | ||
|
||
from models.chat import MessageModel | ||
|
||
from .base import ConfigMeta | ||
|
||
|
||
class Agent(BaseModel, ABC): | ||
meta: ConfigMeta | ||
|
||
'''Abstract agent''' | ||
@abstractmethod | ||
def invoke( | ||
self, | ||
config: ConfigMeta, | ||
chat_id: int, | ||
history: List[MessageModel], | ||
message: MessageModel, | ||
) -> BaseMessage | None: | ||
"""Chat with agent""" | ||
|
||
@abstractmethod | ||
def invoke_astream(self, | ||
config: ConfigMeta, | ||
chat_id: int, | ||
history: List[MessageModel], | ||
message: MessageModel,) -> AsyncIterator[BaseMessageChunk] | None: | ||
"""Chat with agent and get answer via stream""" |
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,40 @@ | ||
'''Agent manager | ||
''' | ||
|
||
|
||
from typing import List | ||
from pydantic import BaseModel | ||
|
||
from .agent_provider import AgentProvider | ||
from .base import ConfigMeta | ||
|
||
|
||
class AgentManager(BaseModel): | ||
'''Agent Manager | ||
get or create agent from provider by manager | ||
''' | ||
|
||
providers: List[AgentProvider] = [] | ||
|
||
def registe_provider(self, provider: AgentProvider): | ||
'''add provider''' | ||
self.providers.append(provider) | ||
|
||
def get_provider(self, config: ConfigMeta) -> AgentProvider: | ||
'''retrieve the best matching provider''' | ||
selected_provider = None | ||
highest_priority = -1 | ||
|
||
for provider in self.providers: | ||
priority = provider.can_handle(config) | ||
if priority > highest_priority: | ||
highest_priority = priority | ||
selected_provider = provider | ||
|
||
if selected_provider is None: | ||
raise Exception("No suitable provider found") | ||
|
||
return selected_provider | ||
|
||
|
||
agent_manager = AgentManager() |
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,20 @@ | ||
'''Agent Provider''' | ||
from abc import ABC, abstractmethod | ||
from pydantic import BaseModel | ||
|
||
from .agent import Agent | ||
|
||
from .base import ConfigMeta | ||
|
||
|
||
class AgentProvider(BaseModel, ABC): | ||
'''Agent Provider''' | ||
name: str = "custom" | ||
|
||
def can_handle(self, config: ConfigMeta) -> int: | ||
'''Priority of provider for handling agent config''' | ||
return -1 | ||
|
||
@abstractmethod | ||
def provide(self, config: ConfigMeta, chat_id: int) -> Agent: | ||
"""List chat executors.""" |
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,15 @@ | ||
'''Base model''' | ||
from typing import Any, List | ||
from pydantic import BaseModel | ||
|
||
|
||
class ModelMeta(BaseModel): | ||
'''LLM meta model ''' | ||
key: str | ||
|
||
|
||
class ConfigMeta(BaseModel): | ||
'''agent meta model''' | ||
persona: str | ||
model: ModelMeta | ||
tools: List[Any] = [] |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.