Skip to content

Commit b96fdab

Browse files
rounak610TransformerOptimusluciferlinx101TarraannFluder-Paradyne
authored
Local LLMs into main (#1351)
* fix * fixing the toolkit config in iteration workflow * List file s3 fix (#1076) * List File S3 * Unit Test Added --------- Co-authored-by: Taran <[email protected]> * workflow changes * minor seed file fix * frontend fixes (#1079) * fixed api_bug (#1080) * add one condition (#1082) * api fix (#1087) * Tools error fix (#1093) * webhooks frontend + api calls complete almost * Tool-LTM(Updated) (#1039) * Toolkit configuration fix (#1102) * webhooks compplete frontend * schedule agent fix (#1104) Co-authored-by: Rounak Bhatia <[email protected]> * Models superagi (#936) Models Superagi * Models superagi (#1108) Bug Fixes * Changes for no receiver address * made changes to github helper * Models superagi (#1112) * Models superagi (#1117) * Models fixes (#1118) * Models Frontend Changes * Models Frontend Changes * Models Frontend Changes * \n bug resolved (#1122) * PDF and DOCX support in Write File - Feature Improvement, close #548 (#928) * Added functions to write various file types and a file handler too * FileManager updated to handle and save HTMLs. * adding PDF + DOCX support to save images * Added Wkhtmltopdf package installation run commands in docker * Added get_all_responses feature for extractng the response for particular tools * Added Image embedding feature, this will extract and embed the images generated by the agent during the run * renaming functions and refactoring * renaming functions and refactoring * Update Dockerfile * removing unsused classmethods * Finding generated images and attached files in the write tool. Images are fetched inorder to be embedded in the respective file type. * Adding the filename and paths to the Resource Manager S3 storage * Code Cleanup * added logger: Fix for the failing TEST --------- Co-authored-by: Fluder-Paradyne <[email protected]> * Revert "PDF and DOCX support in Write File - Feature Improvement, close #548 (#928)" (#1124) This reverts commit 8b01357. * expose port * latest safetensors breaking in macs (#1134) * Changes in save template (#1120) * fix * changes in save template * changes in save template * bug fixes * changes in save template * changes in save template --------- Co-authored-by: Rounak Bhatia <[email protected]> Co-authored-by: namansleeps <[email protected]> * Main to dev sync (#1139) * Models fixes (#1126) * Models Frontend Changes * Models Frontend Changes * Models Frontend Changes * Backend Compatibility for New/Existing users on local * DEV api key requirements * removing print statements * removing print statements * removing print statements * removing print statements * backend compatibility * backend compatibility * backend compatibility * added filters in the webhooks * fix * added filters in the webhooks * Models fixes (#1145) Fixes related to Models Feature * Jira Bug Fix * Jira Bug Fix 2.0 * Jira Bug Fix 3.0 * added filters in the webhooks * Models fixes (#1147) Model Feature Fixes * Bug fix model redirection (#1148) Bug Fix - Model URL Redirection * added tool config for dalle * removed model dependency on dalle tool * Remove hardcoded creds * fixed env error * removed refactoring from main * removed refactoring * removed refactoring * handled error * stop agent from executing if model is not found (#1156) * entity details (#1158) * Metric frontend (#1152) * added filters in webhooks * added filters in webhooks * minor changes * webhooks complete * minor changes for PR * minor changes for PR * Publish agent template to marketplace (#1106) * publish agent to marketplace --------- Co-authored-by: Rounak Bhatia <[email protected]> Co-authored-by: namansleeps <[email protected]> * added filters in webhooks * added filters in webhooks * resolving conflicts * added filters in the webhooks * lint issue fixed * bug fix of prev PR * fix for new run and edit agent * error handling * added filters in webhooks * fix for knowledge search tool * Docker digitalocean deployment * changed branch name * added filters in the webhooks * changes * removed region * added button * change in branch * added filters in the webhooks * Update conftest.py * Added filters in the webhooks (#1140) * webhooks frontend + web hooks with filters --------- Co-authored-by: namansleeps <[email protected]> Co-authored-by: Fluder-Paradyne <[email protected]> * Models calls logs dev (#1174) Call logs organisation level bug fix * models scroll fix, format of log timestamp fix, adding of loader to models, toolkit metrics dropdown bug fixed, publish agent dropdown bug (#1171) * Update app.yaml (#1179) * fixes related to webhooks * fixes for webhooks * Fixes for webhooks (#1181) * fixes for webhooks --------- Co-authored-by: namansleeps <[email protected]> Co-authored-by: Fluder-Paradyne <[email protected]> * bugs by qa (#1178) * Fix for schedule agent (#1184) Co-authored-by: Tarraann <[email protected]> * Entity fix (#1185) * fixes for webhooks * fixes for webhooks * fix added for index state (#1188) * fix added for index state * Update KnowledgeTemplate.js --------- Co-authored-by: Tarraann <[email protected]> * API bug fixes for SDK (#1189) * fix api's for sdk * removed unused imports --------- Co-authored-by: jagtarcontlo <[email protected]> * Main to dev sync v12 (#1193) sync back to dev ------ Co-authored-by: Taran <[email protected]> Co-authored-by: TransformerOptimus <[email protected]> Co-authored-by: Fluder-Paradyne <[email protected]> Co-authored-by: Maverick-F35 <[email protected]> Co-authored-by: BoundlessAsura <[email protected]> Co-authored-by: Akshat Jain <[email protected]> Co-authored-by: sayan1101 <[email protected]> Co-authored-by: Rounak Bhatia <[email protected]> Co-authored-by: Rounak Bhatia <[email protected]> Co-authored-by: Kalki <[email protected]> Co-authored-by: Tarraann <[email protected]> Co-authored-by: rakesh-krishna-a-s <[email protected]> Co-authored-by: Captain Levi <[email protected]> Co-authored-by: andrew-kelly-neutralaiz <[email protected]> Co-authored-by: James Wakelim <[email protected]> * added button * GitHub pull request tools (#1190) * adding github review tools * cleanup and adding code review prompt * fixing comments * PDF and DOCX support in Write File - Feature Improvement, close #548 (#1125) Co-authored-by: Fluder-Paradyne <[email protected]> Co-authored-by: Abhijeet <[email protected]> * minor documentation fix * Design bugs (#1199) * fetching token limit from db * Revert "PDF and DOCX support in Write File - Feature Improvement, close #548 (#1125)" (#1202) This reverts commit 26f6a1d. * Unit Test Fix (#1203) * adding of docs and and discord link correction (#1205) * openai error handling * error_handling * api call only when agent is running * Feature : Wait block for agent workflow (#1186) Agent Wait Block Step * minor changes (#1213) Co-authored-by: Jagtar Saggu <[email protected]> * error handling * error handling * error handling * error handling * error handling * fix * fix * fix * error handling * models changes (#1207) Model related frontend changes. * error handling * models marketplace changes (#1219) Co-authored-by: Abhijeet <[email protected]> * minor changes * error handling * error handling * removing single qoutes (#1224) Co-authored-by: namansleeps <[email protected]> * apm changes (#1222) APM Bug Fixes * list tool fix * list tool fix * PR CHANGES * entity fix for dev (#1230) Entity Fix for Dev * frontend changes (#1231) * read_tool_fix * fix * waiting block frontend (#1233) Waiting Block Changes and Frontend Addition * Dev Fixes (#1242) * read tool fix * Maintaining dev (#1244) Dev Fix * added logs (#1246) * error_handling fix (#1247) Error handling fix. * Feature first login src (#1241) Adding source in user database for analytics. * apollo NoneType bug fix (#1238) Bug Fix * Mixpanel integration (#1256) Mix Panel * Models Marketplace bug fix for dev (#1266) * Fix 1257 dev (#1269) Bug Fix * add cache layer (#1275) Added package caching for github actions workflow * Fix api dev (#1283) * save other config to agent_execution config * add config * mixpanel changes (#1285) * rename error_handling.py to error_handler.py (#1287) * Analytics login (#1258) * calendar issues fixed * append fle tool bug fixed (#1294) Co-authored-by: Tarraann <[email protected]> * adding cookie in access token (#1301) * local_llms * local_llms * local_llms * local_llms * local_llms * fixes * models error fixed (#1308) * local_llms * local_llms * local_llms * local_llms * local_llms * frontend_changes * local_llms * local_llms * local_llms * local_llms * local_llms_frontend * fixes * fixes * fixes * fixes * merged main into local_llm_final * merged main into local_llm_final * local llms --------- Co-authored-by: TransformerOptimus <[email protected]> Co-authored-by: Abhijeet <[email protected]> Co-authored-by: Taran <[email protected]> Co-authored-by: Fluder-Paradyne <[email protected]> Co-authored-by: Maverick-F35 <[email protected]> Co-authored-by: namansleeps <[email protected]> Co-authored-by: Aditya Sharma <[email protected]> Co-authored-by: Sayan Samanta <[email protected]> Co-authored-by: Kalki <[email protected]> Co-authored-by: Tarraann <[email protected]> Co-authored-by: Arkajit Datta <[email protected]> Co-authored-by: rakesh-krishna-a-s <[email protected]> Co-authored-by: jagtarcontlo <[email protected]> Co-authored-by: Captain Levi <[email protected]> Co-authored-by: namansleeps <[email protected]> Co-authored-by: I’m <[email protected]> Co-authored-by: Jagtar Saggu <[email protected]> Co-authored-by: Ubuntu <[email protected]>
1 parent 862a701 commit b96fdab

File tree

10 files changed

+198
-2
lines changed

10 files changed

+198
-2
lines changed

gui/pages/Content/APM/ApmDashboard.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export default function ApmDashboard() {
7676
const fetchData = async () => {
7777
try {
7878
const [metricsResponse, agentsResponse, activeRunsResponse, toolsUsageResponse] = await Promise.all([getMetrics(), getAllAgents(), getActiveRuns(), getToolsUsage()]);
79-
const models = ['gpt-4', 'gpt-3.5-turbo', 'gpt-3.5-turbo-16k', 'gpt-4-32k', 'google-palm-bison-001'];
79+
const models = ['gpt-4', 'gpt-3.5-turbo', 'gpt-3.5-turbo-16k', 'gpt-4-32k', 'google-palm-bison-001', 'replicate-llama13b-v2-chat'];
8080

8181
assignDefaultDataPerModel(metricsResponse.data.agent_details.model_metrics, models);
8282
assignDefaultDataPerModel(metricsResponse.data.tokens_details.model_metrics, models);

gui/pages/_app.js

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export default function App() {
6161
});
6262
}
6363

64+
6465
const installFromMarketplace = () => {
6566
const toolkitName = localStorage.getItem('toolkit_to_install') || null;
6667
const agentTemplateId = localStorage.getItem('agent_to_install') || null;

main.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
from superagi.llms.replicate import Replicate
5151
from superagi.llms.hugging_face import HuggingFace
5252
from superagi.models.agent_template import AgentTemplate
53+
from superagi.models.models_config import ModelsConfig
5354
from superagi.models.organisation import Organisation
5455
from superagi.models.types.login_request import LoginRequest
5556
from superagi.models.types.validate_llm_api_key_request import ValidateAPIKeyRequest
@@ -215,6 +216,13 @@ def register_toolkit_for_master_organisation():
215216
Organisation.id == marketplace_organisation_id).first()
216217
if marketplace_organisation is not None:
217218
register_marketplace_toolkits(session, marketplace_organisation)
219+
220+
def local_llm_model_config():
221+
existing_models_config = session.query(ModelsConfig).filter(ModelsConfig.org_id == default_user.organisation_id, ModelsConfig.provider == 'Local LLM').first()
222+
if existing_models_config is None:
223+
models_config = ModelsConfig(org_id=default_user.organisation_id, provider='Local LLM', api_key="EMPTY")
224+
session.add(models_config)
225+
session.commit()
218226

219227
IterationWorkflowSeed.build_single_step_agent(session)
220228
IterationWorkflowSeed.build_task_based_agents(session)
@@ -238,7 +246,8 @@ def register_toolkit_for_master_organisation():
238246
# AgentWorkflowSeed.doc_search_and_code(session)
239247
# AgentWorkflowSeed.build_research_email_workflow(session)
240248
replace_old_iteration_workflows(session)
241-
249+
local_llm_model_config()
250+
242251
if env != "PROD":
243252
register_toolkit_for_all_organisation()
244253
else:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""local_llms
2+
3+
Revision ID: 9270eb5a8475
4+
Revises: 3867bb00a495
5+
Create Date: 2023-10-04 09:26:33.865424
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
11+
12+
# revision identifiers, used by Alembic.
13+
revision = '9270eb5a8475'
14+
down_revision = '3867bb00a495'
15+
branch_labels = None
16+
depends_on = None
17+
18+
19+
def upgrade() -> None:
20+
# ### commands auto generated by Alembic - please adjust! ###
21+
op.add_column('models', sa.Column('context_length', sa.Integer(), nullable=True))
22+
# ### end Alembic commands ###
23+
24+
25+
def downgrade() -> None:
26+
# ### commands auto generated by Alembic - please adjust! ###
27+
op.drop_column('models', 'context_length')
28+
# ### end Alembic commands ###

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,4 @@ google-generativeai==0.1.0
158158
unstructured==0.8.1
159159
ai21==1.2.6
160160
typing-extensions==4.5.0
161+
llama_cpp_python==0.2.7

superagi/helper/llm_loader.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from llama_cpp import Llama
2+
from llama_cpp import LlamaGrammar
3+
from superagi.config.config import get_config
4+
from superagi.lib.logger import logger
5+
6+
7+
class LLMLoader:
8+
_instance = None
9+
_model = None
10+
_grammar = None
11+
12+
def __new__(cls, *args, **kwargs):
13+
if cls._instance is None:
14+
cls._instance = super(LLMLoader, cls).__new__(cls)
15+
return cls._instance
16+
17+
def __init__(self, context_length):
18+
self.context_length = context_length
19+
20+
@property
21+
def model(self):
22+
if self._model is None:
23+
try:
24+
self._model = Llama(
25+
model_path="/app/local_model_path", n_ctx=self.context_length)
26+
except Exception as e:
27+
logger.error(e)
28+
return self._model
29+
30+
@property
31+
def grammar(self):
32+
if self._grammar is None:
33+
try:
34+
self._grammar = LlamaGrammar.from_file(
35+
"superagi/llms/grammar/json.gbnf")
36+
except Exception as e:
37+
logger.error(e)
38+
return self._grammar

superagi/jobs/agent_executor.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from datetime import datetime, timedelta
22

33
from sqlalchemy.orm import sessionmaker
4+
from superagi.llms.local_llm import LocalLLM
45

56
import superagi.worker
67
from superagi.agent.agent_iteration_step_handler import AgentIterationStepHandler

superagi/llms/grammar/json.gbnf

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
root ::= object
2+
value ::= object | array | string | number | ("true" | "false" | "null") ws
3+
4+
object ::=
5+
"{" ws (
6+
string ":" ws value
7+
("," ws string ":" ws value)*
8+
)? "}" ws
9+
10+
array ::=
11+
"[" ws (
12+
value
13+
("," ws value)*
14+
)? "]" ws
15+
16+
string ::=
17+
"\"" (
18+
[^"\\] |
19+
"\\" (["\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F]) # escapes
20+
)* "\"" ws
21+
22+
number ::= ("-"? ([0-9] | [1-9] [0-9]*)) ("." [0-9]+)? ([eE] [-+]? [0-9]+)? ws
23+
24+
# Optional space: by convention, applied in this grammar after literal chars when allowed
25+
ws ::= ([ \t\n] ws)?

superagi/llms/llm_model_factory.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from superagi.llms.google_palm import GooglePalm
2+
from superagi.llms.local_llm import LocalLLM
23
from superagi.llms.openai import OpenAi
34
from superagi.llms.replicate import Replicate
45
from superagi.llms.hugging_face import HuggingFace

superagi/llms/local_llm.py

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
from superagi.config.config import get_config
2+
from superagi.lib.logger import logger
3+
from superagi.llms.base_llm import BaseLlm
4+
from superagi.helper.llm_loader import LLMLoader
5+
6+
7+
class LocalLLM(BaseLlm):
8+
def __init__(self, temperature=0.6, max_tokens=get_config("MAX_MODEL_TOKEN_LIMIT"), top_p=1,
9+
frequency_penalty=0,
10+
presence_penalty=0, number_of_results=1, model=None, api_key='EMPTY', context_length=4096):
11+
"""
12+
Args:
13+
model (str): The model.
14+
temperature (float): The temperature.
15+
max_tokens (int): The maximum number of tokens.
16+
top_p (float): The top p.
17+
frequency_penalty (float): The frequency penalty.
18+
presence_penalty (float): The presence penalty.
19+
number_of_results (int): The number of results.
20+
"""
21+
self.model = model
22+
self.api_key = api_key
23+
self.temperature = temperature
24+
self.max_tokens = max_tokens
25+
self.top_p = top_p
26+
self.frequency_penalty = frequency_penalty
27+
self.presence_penalty = presence_penalty
28+
self.number_of_results = number_of_results
29+
self.context_length = context_length
30+
31+
llm_loader = LLMLoader(self.context_length)
32+
self.llm_model = llm_loader.model
33+
self.llm_grammar = llm_loader.grammar
34+
35+
def chat_completion(self, messages, max_tokens=get_config("MAX_MODEL_TOKEN_LIMIT")):
36+
"""
37+
Call the chat completion.
38+
39+
Args:
40+
messages (list): The messages.
41+
max_tokens (int): The maximum number of tokens.
42+
43+
Returns:
44+
dict: The response.
45+
"""
46+
try:
47+
if self.llm_model is None or self.llm_grammar is None:
48+
logger.error("Model not found.")
49+
return {"error": "Model loading error", "message": "Model not found. Please check your model path and try again."}
50+
else:
51+
response = self.llm_model.create_chat_completion(messages=messages, functions=None, function_call=None, temperature=self.temperature, top_p=self.top_p,
52+
max_tokens=int(max_tokens), presence_penalty=self.presence_penalty, frequency_penalty=self.frequency_penalty, grammar=self.llm_grammar)
53+
content = response["choices"][0]["message"]["content"]
54+
logger.info(content)
55+
return {"response": response, "content": content}
56+
57+
except Exception as exception:
58+
logger.info("Exception:", exception)
59+
return {"error": "ERROR", "message": "Error: "+str(exception)}
60+
61+
def get_source(self):
62+
"""
63+
Get the source.
64+
65+
Returns:
66+
str: The source.
67+
"""
68+
return "Local LLM"
69+
70+
def get_api_key(self):
71+
"""
72+
Returns:
73+
str: The API key.
74+
"""
75+
return self.api_key
76+
77+
def get_model(self):
78+
"""
79+
Returns:
80+
str: The model.
81+
"""
82+
return self.model
83+
84+
def get_models(self):
85+
"""
86+
Returns:
87+
list: The models.
88+
"""
89+
return self.model
90+
91+
def verify_access_key(self, api_key):
92+
return True

0 commit comments

Comments
 (0)