-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add litellm and llama-cpp (with openai style response now)
- Loading branch information
1 parent
82d5eb9
commit c1f0e2e
Showing
16 changed files
with
135 additions
and
122 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
File renamed without changes.
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 was deleted.
Oops, something went wrong.
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,35 +1,47 @@ | ||
|
||
from r2ai import LOGGER | ||
from r2ai.r2clippy.ais.ai_openai import chat as openai_chat | ||
from r2ai.models import new_get_hf_llm | ||
from r2ai.r2clippy.functions import get_ai_tools | ||
from r2ai.r2clippy.models import parse_model_str | ||
from r2ai.r2clippy.processors import process_streaming_response | ||
from r2ai.r2clippy.decorators import system_message, context | ||
from litellm import completion as litellm_completion | ||
from r2ai.interpreter_base import BaseInterpreter | ||
from r2ai.r2clippy.constants import LITELMM_PROVIDERS | ||
|
||
def auto_chat(interpreter): | ||
model = parse_model_str(interpreter.model) | ||
fn = None | ||
if model.id in auto_chat_handlers.get(model.platform): | ||
if model.uri: | ||
interpreter.api_base = model.uri | ||
fn = auto_chat_handlers[model.platform][model.id] | ||
elif "default" in auto_chat_handlers.get(model.platform, {}): | ||
if model.uri: | ||
interpreter.api_base = model.uri | ||
fn = auto_chat_handlers[model.platform]["default"] | ||
if not fn: | ||
LOGGER.error("Model %s:%s is not currently supported in auto mode") | ||
return | ||
return fn(interpreter) | ||
|
||
|
||
def chat_open_ai(): | ||
pass | ||
from r2ai.r2clippy.utils.split_string import split_string_with_limit | ||
from llama_cpp import Llama | ||
|
||
def auto_chat(interpreter: BaseInterpreter): | ||
model = parse_model_str(interpreter.model) | ||
_auto_chat(interpreter, model) | ||
|
||
auto_chat_handlers = { | ||
"openai": { | ||
"default": openai_chat | ||
}, | ||
"openapi": { | ||
"default": openai_chat | ||
} | ||
} | ||
@system_message | ||
@context | ||
def _auto_chat(interpreter: BaseInterpreter, model): | ||
call = True | ||
while call: | ||
extra_args = {} | ||
completion = None | ||
if model.platform not in LITELMM_PROVIDERS: | ||
if not interpreter.llama_instance: | ||
interpreter.llama_instance = new_get_hf_llm(interpreter, f"{model.platform}/{model.id}", (LOGGER.level / 10) == 1, int(interpreter.env["llm.window"])) | ||
completion = interpreter.llama_instance.create_chat_completion_openai_v1 | ||
extra_args = {} | ||
else: | ||
completion = litellm_completion | ||
extra_args = {"num_retries": 3, | ||
"base_url": model.uri} | ||
response = completion( | ||
model=f"{model.platform}/{model.id}", | ||
max_tokens=4000, #int(interpreter.env["llm.maxtokens"]), | ||
tools=get_ai_tools(), | ||
messages=interpreter.messages, | ||
tool_choice="auto", | ||
stream=True, | ||
temperature=float(interpreter.env["llm.temperature"], | ||
**extra_args) | ||
) | ||
call = process_streaming_response( | ||
interpreter, response) | ||
return response |
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,27 @@ | ||
from queue import Queue | ||
from typing import Dict, Tuple, Union | ||
|
||
from r2ai.r2clippy.utils import split_string_with_limit | ||
|
||
_chunks: Queue = Queue() | ||
|
||
|
||
def add_chunk(text: str, max_tokens = 2000): | ||
global _chunks | ||
if _chunks.qsize() > 0: | ||
_chunks = Queue() | ||
if text.strip() == "": | ||
return 0 | ||
for i in split_string_with_limit(text, max_tokens, "cl100k_base"): | ||
_chunks.put(i) | ||
return _chunks.qsize() | ||
|
||
def get_chunk(): | ||
global _chunks | ||
if _chunks.qsize() == 0: | ||
return str() | ||
return _chunks.get() | ||
|
||
def size(): | ||
global _chunks | ||
return _chunks.qsize() |
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
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,9 @@ | ||
# from openai.types.chat import ChatCompletion | ||
|
||
|
||
# class LlamaChatCompletion(ChatCompletion): | ||
# @classmethod | ||
# def from_llama_response(llamaResponse: dict) -> ChatCompletion: | ||
# choice = llamaResponse["choices"][0] | ||
# if "message" in choice: | ||
# if "content" in choice["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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from r2ai.r2clippy.utils.context import context_from_msg | ||
from r2ai.r2clippy.utils.split_string import split_string_with_limit | ||
|
||
|
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.