Skip to content

Commit

Permalink
add openai
Browse files Browse the repository at this point in the history
  • Loading branch information
aniketmaurya committed Jun 2, 2024
1 parent bacefc6 commit 720fd6a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 27 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ pip install git+https://github.com/aniketmaurya/agents.git@main
pip install -e .
```

## Supported LLMs

✅ OpenAI
✅ Cohere Command R and Command R+
✅ LlamaCPP

## Usage/Examples

### Simple tool use with a local or cloud LLM
Expand Down
4 changes: 3 additions & 1 deletion src/agents/llms/__init__.py
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"]
26 changes: 0 additions & 26 deletions src/agents/llms/_cohere.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,32 +51,6 @@ def _format_cohere_to_openai(self, output: AIMessage):
}
return ChatCompletion(**response)

# def _format_cohere_to_openai(self, output: NonStreamedChatResponse):
# _tool_calls = output.tool_calls
# tool_calls = []
# for tool in _tool_calls:
# tool = ToolCall(id=tool["id"], type=tool["type"], function=tool["function"])
# tool_calls.append(tool)
#
# message = Message(
# role="assistant", content=output.text, tool_calls=tool_calls
# )
# choices = Choice(index=0, logprobs=None, message=message, finish_reason=output.finish_reason)
# usage = Usage(
# prompt_tokens=output.meta.tokens.input_tokens,
# completion_tokens=output.meta.tokens.output_tokens,
# total_tokens=(output.meta.tokens.input_tokens + output.meta.tokens.output_tokens),
# )
# response = {
# "id": output.generation_id,
# "object": "",
# "created": int(time.time()),
# "model": "Cohere",
# "choices": [choices],
# "usage": usage,
# }
# return ChatCompletion(**response)

def chat_completion(
self, messages: List[ChatCompletionRequestMessage], **kwargs
) -> ChatCompletion:
Expand Down
34 changes: 34 additions & 0 deletions src/agents/llms/_openai.py
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)

0 comments on commit 720fd6a

Please sign in to comment.