Merge pull request #1 from Cinnamon/misc/config-model-from-file

Misc/config model from file
This commit is contained in:
ian_Cin 2024-03-28 16:29:26 +07:00 committed by GitHub
commit b2089245f2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
18 changed files with 146 additions and 74 deletions

18
.env Normal file
View File

@ -0,0 +1,18 @@
# settings for OpenAI
OPENAI_API_BASE=https://api.openai.com/v1
OPENAI_API_KEY=
OPENAI_CHAT_MODEL=gpt-3.5-turbo
OPENAI_EMBEDDINGS_MODEL=text-embedding-ada-002
# settings for Azure OpenAI
AZURE_OPENAI_ENDPOINT=
AZURE_OPENAI_API_KEY=
OPENAI_API_VERSION=2024-02-15-preview
AZURE_OPENAI_CHAT_DEPLOYMENT=gpt-35-turbo
AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT=text-embedding-ada-002
# settings for Cohere
COHERE_API_KEY=
# settings for local models
LOCAL_MODEL=

Binary file not shown.

4
.gitignore vendored
View File

@ -332,7 +332,6 @@ celerybeat.pid
*.sage.py *.sage.py
# Environments # Environments
.env
.venv .venv
env/ env/
venv/ venv/
@ -457,7 +456,6 @@ $RECYCLE.BIN/
logs/ logs/
.gitsecret/keys/random_seed .gitsecret/keys/random_seed
!*.secret !*.secret
.env
.envrc .envrc
S.gpg-agent* S.gpg-agent*
@ -467,4 +465,4 @@ storage/*
# Conda and env storages # Conda and env storages
*install_dir/ *install_dir/
doc_env doc_env/

Binary file not shown.

Binary file not shown.

View File

@ -1 +0,0 @@
.env:555d804179d7207ad6784a84afb88d2ec44f90ea3b7a061d0e38f9dd53fe7211

View File

@ -137,14 +137,14 @@ class AzureOpenAIEmbeddings(LCEmbeddingMixin, BaseEmbeddings):
azure_endpoint: Optional[str] = None, azure_endpoint: Optional[str] = None,
deployment: Optional[str] = None, deployment: Optional[str] = None,
openai_api_key: Optional[str] = None, openai_api_key: Optional[str] = None,
openai_api_version: Optional[str] = None, api_version: Optional[str] = None,
request_timeout: Optional[float] = None, request_timeout: Optional[float] = None,
**params, **params,
): ):
super().__init__( super().__init__(
azure_endpoint=azure_endpoint, azure_endpoint=azure_endpoint,
deployment=deployment, deployment=deployment,
openai_api_version=openai_api_version, api_version=api_version,
openai_api_key=openai_api_key, openai_api_key=openai_api_key,
request_timeout=request_timeout, request_timeout=request_timeout,
**params, **params,

View File

@ -2,7 +2,7 @@ from kotaemon.base.schema import AIMessage, BaseMessage, HumanMessage, SystemMes
from .base import BaseLLM from .base import BaseLLM
from .branching import GatedBranchingPipeline, SimpleBranchingPipeline from .branching import GatedBranchingPipeline, SimpleBranchingPipeline
from .chats import AzureChatOpenAI, ChatLLM, EndpointChatLLM, LlamaCppChat from .chats import AzureChatOpenAI, ChatLLM, ChatOpenAI, EndpointChatLLM, LlamaCppChat
from .completions import LLM, AzureOpenAI, LlamaCpp, OpenAI from .completions import LLM, AzureOpenAI, LlamaCpp, OpenAI
from .cot import ManualSequentialChainOfThought, Thought from .cot import ManualSequentialChainOfThought, Thought
from .linear import GatedLinearPipeline, SimpleLinearPipeline from .linear import GatedLinearPipeline, SimpleLinearPipeline
@ -17,6 +17,7 @@ __all__ = [
"HumanMessage", "HumanMessage",
"AIMessage", "AIMessage",
"SystemMessage", "SystemMessage",
"ChatOpenAI",
"AzureChatOpenAI", "AzureChatOpenAI",
"LlamaCppChat", "LlamaCppChat",
# completion-specific components # completion-specific components

View File

@ -1,11 +1,12 @@
from .base import ChatLLM from .base import ChatLLM
from .endpoint_based import EndpointChatLLM from .endpoint_based import EndpointChatLLM
from .langchain_based import AzureChatOpenAI, LCChatMixin from .langchain_based import AzureChatOpenAI, ChatOpenAI, LCChatMixin
from .llamacpp import LlamaCppChat from .llamacpp import LlamaCppChat
__all__ = [ __all__ = [
"ChatLLM", "ChatLLM",
"EndpointChatLLM", "EndpointChatLLM",
"ChatOpenAI",
"AzureChatOpenAI", "AzureChatOpenAI",
"LCChatMixin", "LCChatMixin",
"LlamaCppChat", "LlamaCppChat",

View File

@ -165,7 +165,36 @@ class LCChatMixin:
raise ValueError(f"Invalid param {path}") raise ValueError(f"Invalid param {path}")
class ChatOpenAI(LCChatMixin, ChatLLM): # type: ignore
def __init__(
self,
openai_api_base: str | None = None,
openai_api_key: str | None = None,
model: str | None = None,
temperature: float = 0.7,
request_timeout: float | None = None,
**params,
):
super().__init__(
openai_api_base=openai_api_base,
openai_api_key=openai_api_key,
model=model,
temperature=temperature,
request_timeout=request_timeout,
**params,
)
def _get_lc_class(self):
try:
from langchain_openai import ChatOpenAI
except ImportError:
from langchain.chat_models import ChatOpenAI
return ChatOpenAI
class AzureChatOpenAI(LCChatMixin, ChatLLM): # type: ignore class AzureChatOpenAI(LCChatMixin, ChatLLM): # type: ignore
def __init__( def __init__(
self, self,
azure_endpoint: str | None = None, azure_endpoint: str | None = None,

View File

@ -31,70 +31,98 @@ KH_VECTORSTORE = {
"__type__": "kotaemon.storages.ChromaVectorStore", "__type__": "kotaemon.storages.ChromaVectorStore",
"path": str(user_cache_dir / "vectorstore"), "path": str(user_cache_dir / "vectorstore"),
} }
KH_LLMS = { KH_LLMS = {}
# example for using Azure OpenAI, the config variables can set as environment KH_EMBEDDINGS = {}
# variables or in the .env file
# "gpt4": { # populate options from config
# "def": { if config("AZURE_OPENAI_API_KEY", default="") and config(
# "__type__": "kotaemon.llms.AzureChatOpenAI", "AZURE_OPENAI_ENDPOINT", default=""
# "temperature": 0, ):
# "azure_endpoint": config("AZURE_OPENAI_ENDPOINT", default=""), if config("AZURE_OPENAI_CHAT_DEPLOYMENT", default=""):
# "openai_api_key": config("AZURE_OPENAI_API_KEY", default=""), KH_LLMS["azure"] = {
# "openai_api_version": config("OPENAI_API_VERSION", default=""), "def": {
# "deployment_name": "<your deployment name>", "__type__": "kotaemon.llms.AzureChatOpenAI",
# "stream": True, "temperature": 0,
# }, "azure_endpoint": config("AZURE_OPENAI_ENDPOINT", default=""),
# "accuracy": 10, "openai_api_key": config("AZURE_OPENAI_API_KEY", default=""),
# "cost": 10, "api_version": config("OPENAI_API_VERSION", default="")
# "default": False, or "2024-02-15-preview",
# }, "deployment_name": config("AZURE_OPENAI_CHAT_DEPLOYMENT", default=""),
# "gpt35": { "request_timeout": 10,
# "def": { "stream": False,
# "__type__": "kotaemon.llms.AzureChatOpenAI", },
# "temperature": 0, "default": False,
# "azure_endpoint": config("AZURE_OPENAI_ENDPOINT", default=""), "accuracy": 5,
# "openai_api_key": config("AZURE_OPENAI_API_KEY", default=""), "cost": 5,
# "openai_api_version": config("OPENAI_API_VERSION", default=""), }
# "deployment_name": "<your deployment name>", if config("AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT", default=""):
# "request_timeout": 10, KH_EMBEDDINGS["azure"] = {
# "stream": False, "def": {
# }, "__type__": "kotaemon.embeddings.AzureOpenAIEmbeddings",
# "accuracy": 5, "azure_endpoint": config("AZURE_OPENAI_ENDPOINT", default=""),
# "cost": 5, "openai_api_key": config("AZURE_OPENAI_API_KEY", default=""),
# "default": False, "api_version": config("OPENAI_API_VERSION", default="")
# }, or "2024-02-15-preview",
"local": { "deployment": config("AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT", default=""),
"request_timeout": 10,
"chunk_size": 16,
},
"default": False,
"accuracy": 5,
"cost": 5,
}
if config("OPENAI_API_KEY", default=""):
KH_LLMS["openai"] = {
"def": {
"__type__": "kotaemon.llms.ChatOpenAI",
"temperature": 0,
"openai_api_base": config("OPENAI_API_BASE", default="")
or "https://api.openai.com/v1",
"openai_api_key": config("OPENAI_API_KEY", default=""),
"model": config("OPENAI_CHAT_MODEL", default="") or "gpt-3.5-turbo",
"request_timeout": 10,
"stream": False,
},
"default": False,
}
if len(KH_EMBEDDINGS) < 1:
KH_EMBEDDINGS["openai"] = {
"def": {
"__type__": "kotaemon.embeddings.OpenAIEmbeddings",
"openai_api_base": config("OPENAI_API_BASE", default="")
or "https://api.openai.com/v1",
"openai_api_key": config("OPENAI_API_KEY", default=""),
"model": config(
"OPENAI_EMBEDDINGS_MODEL", default="text-embedding-ada-002"
)
or "text-embedding-ada-002",
"request_timeout": 10,
"chunk_size": 16,
},
"default": False,
}
if config("LOCAL_MODEL", default=""):
KH_LLMS["local"] = {
"def": { "def": {
"__type__": "kotaemon.llms.EndpointChatLLM", "__type__": "kotaemon.llms.EndpointChatLLM",
"endpoint_url": "http://localhost:31415/v1/chat/completions", "endpoint_url": "http://localhost:31415/v1/chat/completions",
}, },
"default": False, "default": False,
}, "cost": 0,
} }
KH_EMBEDDINGS = { if len(KH_EMBEDDINGS) < 1:
# example for using Azure OpenAI, the config variables can set as environment KH_EMBEDDINGS["local"] = {
# variables or in the .env file
# "ada": {
# "def": {
# "__type__": "kotaemon.embeddings.AzureOpenAIEmbeddings",
# "model": "text-embedding-ada-002",
# "azure_endpoint": config("AZURE_OPENAI_ENDPOINT", default=""),
# "openai_api_key": config("AZURE_OPENAI_API_KEY", default=""),
# "deployment": "<your deployment name>",
# "chunk_size": 16,
# },
# "accuracy": 5,
# "cost": 5,
# "default": True,
# },
"local": {
"def": { "def": {
"__type__": "kotaemon.embeddings.EndpointEmbeddings", "__type__": "kotaemon.embeddings.EndpointEmbeddings",
"endpoint_url": "http://localhost:31415/v1/embeddings", "endpoint_url": "http://localhost:31415/v1/embeddings",
}, },
"default": False, "default": False,
}, "cost": 0,
} }
KH_REASONINGS = ["ktem.reasoning.simple.FullQAPipeline"] KH_REASONINGS = ["ktem.reasoning.simple.FullQAPipeline"]

View File

@ -3,9 +3,7 @@ import subprocess
from inspect import currentframe, getframeinfo from inspect import currentframe, getframeinfo
from pathlib import Path from pathlib import Path
import dotenv from decouple import config
configs = dotenv.dotenv_values(".env")
system_name = platform.system() system_name = platform.system()
@ -53,7 +51,7 @@ def serve_llamacpp_python(local_model_file: Path, **kwargs):
def main(): def main():
local_model_file = configs.get("LOCAL_MODEL", "") local_model_file = config("LOCAL_MODEL", default="")
if not local_model_file: if not local_model_file:
print("LOCAL_MODEL not set in the `.env` file.") print("LOCAL_MODEL not set in the `.env` file.")

View File

@ -87,7 +87,7 @@ activate_environment
# install dependencies # install dependencies
# ver 0.2.56 produces segment error for /embeddings on MacOS # ver 0.2.56 produces segment error for /embeddings on MacOS
python -m pip install llama-cpp-python[server]!=0.2.56 python -m pip install llama-cpp-python[server]==0.2.55
# start the server with passed params # start the server with passed params
python -m llama_cpp.server $@ python -m llama_cpp.server $@

View File

@ -88,7 +88,7 @@ activate_environment
# install dependencies # install dependencies
# ver 0.2.56 produces segment error for /embeddings on MacOS # ver 0.2.56 produces segment error for /embeddings on MacOS
python -m pip install llama-cpp-python[server]!=0.2.56 python -m pip install llama-cpp-python[server]==0.2.55
# start the server with passed params # start the server with passed params
python -m llama_cpp.server $@ python -m llama_cpp.server $@

View File

@ -28,7 +28,7 @@ call :activate_environment
@rem install dependencies @rem install dependencies
@rem ver 0.2.56 produces segment error for /embeddings on MacOS @rem ver 0.2.56 produces segment error for /embeddings on MacOS
call python -m pip install llama-cpp-python[server]!=0.2.56 call python -m pip install llama-cpp-python[server]==0.2.55
@REM @rem start the server with passed params @REM @rem start the server with passed params
call python -m llama_cpp.server %* call python -m llama_cpp.server %*