generated from aniketmaurya/python-project-template
-
Notifications
You must be signed in to change notification settings - Fork 5
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
1 parent
bacefc6
commit 720fd6a
Showing
4 changed files
with
43 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
from .llm import create_tool_use_llm | ||
from ._openai import OpenAIChatCompletion | ||
from ._cohere import CohereChatCompletion | ||
|
||
__all__ = ["create_tool_use_llm"] | ||
__all__ = ["create_tool_use_llm", "CohereChatCompletion", "OpenAIChatCompletion"] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from typing import List, Optional, Any, Dict | ||
|
||
import logging | ||
from agents.specs import ChatCompletion | ||
from agents.tool_executor import ToolRegistry | ||
from langchain_core.tools import StructuredTool | ||
from llama_cpp import ChatCompletionRequestMessage | ||
from openai import OpenAI | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class OpenAIChatCompletion: | ||
def __init__(self, model: str = "gpt-4o"): | ||
self.model = model | ||
self.client = OpenAI() | ||
self.tool_registry = ToolRegistry() | ||
|
||
def bind_tools(self, tools: Optional[List[StructuredTool]] = None): | ||
for tool in tools: | ||
self.tool_registry.register_tool(tool) | ||
|
||
def chat_completion( | ||
self, messages: List[ChatCompletionRequestMessage], **kwargs | ||
) -> ChatCompletion: | ||
tools = self.tool_registry.openai_tools | ||
output = self.client.chat.completions.create( | ||
model=self.model, messages=messages, tools=tools | ||
) | ||
logger.debug(output) | ||
return output | ||
|
||
def run_tools(self, chat_completion: ChatCompletion) -> List[Dict[str, Any]]: | ||
return self.tool_registry.call_tools(chat_completion) |