core
Browse files- core/__init__.py +2 -0
- core/app/__init__.py +2 -0
- core/cli/__init__.py +2 -0
- core/llms/__init__.py +7 -0
- core/llms/base_llm.py +22 -0
- core/llms/litellm_llm.py +29 -0
- core/tools/__init__.py +2 -0
core/__init__.py
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
|
2 |
+
|
core/app/__init__.py
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
|
2 |
+
|
core/cli/__init__.py
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
|
2 |
+
|
core/llms/__init__.py
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from .base_llm import BaseLLM
|
2 |
+
from .litellm_llm import LLM
|
3 |
+
|
4 |
+
__all__ = [
|
5 |
+
"BaseLLM",
|
6 |
+
"LLM",
|
7 |
+
]
|
core/llms/base_llm.py
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from abc import ABC, abstractmethod
|
2 |
+
|
3 |
+
class BaseLLM(ABC):
|
4 |
+
|
5 |
+
def __init__(self, api_key, model, tools):
|
6 |
+
self.api_key = api_key
|
7 |
+
self.model = model
|
8 |
+
self.tools = tools
|
9 |
+
|
10 |
+
@abstractmethod
|
11 |
+
def _chat(self, messages, **kargs):
|
12 |
+
pass
|
13 |
+
|
14 |
+
def chat(self, messages, **kargs):
|
15 |
+
return self._chat(messages, **kargs)
|
16 |
+
|
17 |
+
def get_attr(self,attr:str, kwargs:dict):
|
18 |
+
attribute = kwargs.get(attr, None)
|
19 |
+
if attribute is None:
|
20 |
+
if hasattr(self, attr):
|
21 |
+
attribute = getattr(self, attr)
|
22 |
+
return attribute
|
core/llms/litellm_llm.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from openai.types.chat.chat_completion_chunk import Choice
|
2 |
+
from .base_llm import BaseLLM
|
3 |
+
from litellm import completion
|
4 |
+
from litellm.utils import ModelResponse
|
5 |
+
from openai import OpenAI
|
6 |
+
|
7 |
+
class LLM(BaseLLM):
|
8 |
+
def __init__(self, api_key, model, tools:list=None):
|
9 |
+
super().__init__(api_key=api_key, model=model, tools=tools)
|
10 |
+
|
11 |
+
|
12 |
+
def _chat(self, messages, **kwargs):
|
13 |
+
return completion(messages=messages, model=self.model, api_key=self.api_key, **kwargs)
|
14 |
+
|
15 |
+
def _handle_tool_calls(self, messages:list[ModelResponse], **kwargs):
|
16 |
+
|
17 |
+
message = messages[-1]
|
18 |
+
|
19 |
+
if self._tools is None:
|
20 |
+
return messages
|
21 |
+
|
22 |
+
if message.choices[0].finish_reason in ('stop', 'length'):
|
23 |
+
return messages
|
24 |
+
elif message.choices[0].finish_reason == 'tool_calls':
|
25 |
+
tools_to_call = message.choices[0].message.tool_calls
|
26 |
+
for tool in tools_to_call:
|
27 |
+
print(tool)
|
28 |
+
|
29 |
+
|
core/tools/__init__.py
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
|
2 |
+
|