enable config through .env

This commit is contained in:
ian 2024-03-27 19:04:48 +07:00
parent c6db7f5d01
commit c1b1371a68
6 changed files with 140 additions and 63 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=

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"]