Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed LLMCustom setup #418

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@
<br />
<div align="center">
<h2>Cheshire-Cat (Stregatto)</h2>
<br/>
<a href="https://github.com/cheshire-cat-ai/core">
<img alt="GitHub Repo stars" src="https://img.shields.io/github/stars/cheshire-cat-ai/core?style=social">
</a>
<a href="https://discord.gg/bHX5sNFCYU">
<img src="https://img.shields.io/discord/1092359754917089350?logo=discord"
alt="chat on Discord"></a>
<a href="https://github.com/cheshire-cat-ai/core/issues">
<img alt="GitHub issues" src="https://img.shields.io/github/issues/cheshire-cat-ai/core">
</a>
<a href="https://github.com/cheshire-cat-ai/core/tags">
<img alt="GitHub tag (with filter)" src="https://img.shields.io/github/v/tag/cheshire-cat-ai/core">
</a>
<img alt="GitHub top language" src="https://img.shields.io/github/languages/top/cheshire-cat-ai/core">

<br/>
<img src="./readme/cheshire_cat_generated_mj.jpeg" alt="Logo" width="600" height="auto" alt="Image generated by Midjourney, prompted by Edgars Romanovskis">
<h4>
Customizable AI architecture
Expand Down
25 changes: 15 additions & 10 deletions core/cat/factory/custom_llm.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Optional, List, Any, Mapping, Dict
import requests
from langchain.llms.base import LLM
from cat.log import log


class LLMDefault(LLM):
Expand All @@ -10,13 +11,12 @@ def _llm_type(self):

def _call(self, prompt, stop=None):
return "AI: You did not configure a Language Model. " \
"Do it in the settings!"
"Do it in the settings!"


# elaborated from
# https://python.langchain.com/en/latest/modules/models/llms/examples/custom_llm.html
class LLMCustom(LLM):

# endpoint where custom LLM service accepts requests
url: str

Expand All @@ -31,11 +31,11 @@ def _llm_type(self) -> str:
return "custom"

def _call(
self,
prompt: str,
stop: Optional[List[str]] = None,
# run_manager: Optional[CallbackManagerForLLMRun] = None,
run_manager: Optional[Any] = None,
self,
prompt: str,
stop: Optional[List[str]] = None,
# run_manager: Optional[CallbackManagerForLLMRun] = None,
run_manager: Optional[Any] = None,
) -> str:

request_body = {
Expand All @@ -44,11 +44,16 @@ def _call(
"options": self.options
}

headers = {
'accept': 'application/json',
'Content-Type': 'application/json'
}

try:
response_json = requests.post(self.url, json=request_body).json()
except Exception:
raise Exception("Custom LLM endpoint error "
"during http POST request")
except Exception as exc:
raise ValueError("Custom LLM endpoint error "
"during http POST request") from exc

generated_text = response_json["text"]

Expand Down
15 changes: 10 additions & 5 deletions core/cat/factory/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class Config:


class LLMCustomConfig(LLMSettings):

url: str
auth_key: str = "optional_auth_key"
options: str = "{}"
Expand All @@ -45,16 +44,20 @@ class LLMCustomConfig(LLMSettings):
# instantiate Custom LLM from configuration
@classmethod
def get_llm_from_config(cls, config):
options = config["options"]
# options are inserted as a string in the admin
if type(config["options"]) == str:
config["options"] = json.loads(config["options"])
if isinstance(options, str):
if options != "":
config["options"] = json.loads(options)
else:
config["options"] = {}

return cls._pyclass(**config)

class Config:
schema_extra = {
"humanReadableName": "Custom LLM",
"description":
"description":
"LLM on a custom endpoint. "
"See docs for examples.",
}
Expand All @@ -80,7 +83,7 @@ class LLMOpenAIConfig(LLMSettings):
class Config:
schema_extra = {
"humanReadableName": "OpenAI GPT-3",
"description":
"description":
"OpenAI GPT-3. More expensive but "
"also more flexible than ChatGPT.",
}
Expand Down Expand Up @@ -138,6 +141,7 @@ class Config:
"description": "Configuration for Cohere language model",
}


# https://python.langchain.com/en/latest/modules/models/llms/integrations/huggingface_textgen_inference.html
class LLMHuggingFaceTextGenInferenceConfig(LLMSettings):
inference_server_url: str
Expand All @@ -155,6 +159,7 @@ class Config:
"description": "Configuration for HuggingFace TextGen Inference",
}


class LLMHuggingFaceHubConfig(LLMSettings):
# model_kwargs = {
# "generation_config": {
Expand Down
43 changes: 20 additions & 23 deletions readme/ROADMAP.md
Original file line number Diff line number Diff line change
@@ -1,50 +1,47 @@

* Version 1
* Forms from JSON schema ✅
* Forms from JSON schema ✅
* Configurations
* Language model provider ✅
* Embedder
* Plugins list ✅
* Why in the admin ✅
* Documentation ✅
* Markdown support ✅
* Static admin inside main container ✅
* Language model provider ✅
* Embedder
* Plugins list ✅
* Why in the admin ✅
* Documentation ✅
* Markdown support ✅
* Static admin inside main container ✅
* [Public `/chat` endpoint](https://github.com/cheshire-cat-ai/core/issues/267/) ✅
* [js widget (for `/chat` and external websites)](https://github.com/cheshire-cat-ai/core/issues/269/) ✅
* Memory management page in admin ✅

<hr>

* Version 2
* User management
* User specific conversation and memory
* User specific conversation and memory
* Dissemination
* minimal website ✅
* how to guides
* how to guides
* use cases examples
* QA / Test
* End2End tests
* Setup ✅
* Essential coverage
* Unit tests
* Setup
* Essential coverage
* Essential coverage
* Unit tests
* Setup
* Essential coverage
* Admin
* import memories
* import memories
* export memeories ✅
* filters for memory search
* better `why` UI
* better `why` UI
* Agent
* Tool embeddings ✅
* Custom hookable agent
* Local LLM / embedder
* CustomLLMConfig / CustomEmbedderConfig adapters
* CustomLLMConfig
* CustomEmbedderConfig adapters
* LLM / embedder example docker container
* Hook surface
* 20 hooks ✅
* more hooks where customization is needed
* Plugin management
* Install plugin dependencies ✅
* Activate / deactivate plugins
* External plugin directory
* Activate / deactivate plugins
* External plugin directory
* Pugin manifesto (`plugin.json`) ✅
Loading