mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-21 10:50:14 +01:00
Merge branch 'main' into partial-filter-index
This commit is contained in:
commit
915022bc08
1573 changed files with 145791 additions and 49740 deletions
|
|
@ -1,5 +1,3 @@
|
||||||
version: "3.8"
|
|
||||||
|
|
||||||
services:
|
services:
|
||||||
app:
|
app:
|
||||||
build:
|
build:
|
||||||
|
|
|
||||||
218
.env.example
218
.env.example
|
|
@ -2,11 +2,9 @@
|
||||||
# LibreChat Configuration #
|
# LibreChat Configuration #
|
||||||
#=====================================================================#
|
#=====================================================================#
|
||||||
# Please refer to the reference documentation for assistance #
|
# Please refer to the reference documentation for assistance #
|
||||||
# with configuring your LibreChat environment. The guide is #
|
# with configuring your LibreChat environment. #
|
||||||
# available both online and within your local LibreChat #
|
# #
|
||||||
# directory: #
|
# https://www.librechat.ai/docs/configuration/dotenv #
|
||||||
# Online: https://docs.librechat.ai/install/configuration/dotenv.html #
|
|
||||||
# Locally: ./docs/install/configuration/dotenv.md #
|
|
||||||
#=====================================================================#
|
#=====================================================================#
|
||||||
|
|
||||||
#==================================================#
|
#==================================================#
|
||||||
|
|
@ -23,6 +21,13 @@ DOMAIN_SERVER=http://localhost:3080
|
||||||
|
|
||||||
NO_INDEX=true
|
NO_INDEX=true
|
||||||
|
|
||||||
|
#===============#
|
||||||
|
# JSON Logging #
|
||||||
|
#===============#
|
||||||
|
|
||||||
|
# Use when process console logs in cloud deployment like GCP/AWS
|
||||||
|
CONSOLE_JSON=false
|
||||||
|
|
||||||
#===============#
|
#===============#
|
||||||
# Debug Logging #
|
# Debug Logging #
|
||||||
#===============#
|
#===============#
|
||||||
|
|
@ -40,6 +45,7 @@ DEBUG_CONSOLE=false
|
||||||
#===============#
|
#===============#
|
||||||
# Configuration #
|
# Configuration #
|
||||||
#===============#
|
#===============#
|
||||||
|
# Use an absolute path, a relative path, or a URL
|
||||||
|
|
||||||
# CONFIG_PATH="/alternative/path/to/librechat.yaml"
|
# CONFIG_PATH="/alternative/path/to/librechat.yaml"
|
||||||
|
|
||||||
|
|
@ -47,35 +53,43 @@ DEBUG_CONSOLE=false
|
||||||
# Endpoints #
|
# Endpoints #
|
||||||
#===================================================#
|
#===================================================#
|
||||||
|
|
||||||
# ENDPOINTS=openAI,assistants,azureOpenAI,bingAI,google,gptPlugins,anthropic
|
# ENDPOINTS=openAI,assistants,azureOpenAI,google,gptPlugins,anthropic
|
||||||
|
|
||||||
PROXY=
|
PROXY=
|
||||||
|
|
||||||
#===================================#
|
#===================================#
|
||||||
# Known Endpoints - librechat.yaml #
|
# Known Endpoints - librechat.yaml #
|
||||||
#===================================#
|
#===================================#
|
||||||
# https://docs.librechat.ai/install/configuration/ai_endpoints.html
|
# https://www.librechat.ai/docs/configuration/librechat_yaml/ai_endpoints
|
||||||
|
|
||||||
|
# ANYSCALE_API_KEY=
|
||||||
|
# APIPIE_API_KEY=
|
||||||
|
# COHERE_API_KEY=
|
||||||
|
# DEEPSEEK_API_KEY=
|
||||||
|
# DATABRICKS_API_KEY=
|
||||||
|
# FIREWORKS_API_KEY=
|
||||||
# GROQ_API_KEY=
|
# GROQ_API_KEY=
|
||||||
|
# HUGGINGFACE_TOKEN=
|
||||||
# MISTRAL_API_KEY=
|
# MISTRAL_API_KEY=
|
||||||
# OPENROUTER_KEY=
|
# OPENROUTER_KEY=
|
||||||
# ANYSCALE_API_KEY=
|
|
||||||
# FIREWORKS_API_KEY=
|
|
||||||
# PERPLEXITY_API_KEY=
|
# PERPLEXITY_API_KEY=
|
||||||
|
# SHUTTLEAI_API_KEY=
|
||||||
# TOGETHERAI_API_KEY=
|
# TOGETHERAI_API_KEY=
|
||||||
|
# UNIFY_API_KEY=
|
||||||
|
# XAI_API_KEY=
|
||||||
|
|
||||||
#============#
|
#============#
|
||||||
# Anthropic #
|
# Anthropic #
|
||||||
#============#
|
#============#
|
||||||
|
|
||||||
ANTHROPIC_API_KEY=user_provided
|
ANTHROPIC_API_KEY=user_provided
|
||||||
# ANTHROPIC_MODELS=claude-3-opus-20240229,claude-3-sonnet-20240229,claude-2.1,claude-2,claude-1.2,claude-1,claude-1-100k,claude-instant-1,claude-instant-1-100k
|
# ANTHROPIC_MODELS=claude-3-5-haiku-20241022,claude-3-5-sonnet-20241022,claude-3-5-sonnet-latest,claude-3-5-sonnet-20240620,claude-3-opus-20240229,claude-3-sonnet-20240229,claude-3-haiku-20240307,claude-2.1,claude-2,claude-1.2,claude-1,claude-1-100k,claude-instant-1,claude-instant-1-100k
|
||||||
|
# ANTHROPIC_REVERSE_PROXY=
|
||||||
|
|
||||||
#============#
|
#============#
|
||||||
# Azure #
|
# Azure #
|
||||||
#============#
|
#============#
|
||||||
|
|
||||||
|
|
||||||
# Note: these variables are DEPRECATED
|
# Note: these variables are DEPRECATED
|
||||||
# Use the `librechat.yaml` configuration for `azureOpenAI` instead
|
# Use the `librechat.yaml` configuration for `azureOpenAI` instead
|
||||||
# You may also continue to use them if you opt out of using the `librechat.yaml` configuration
|
# You may also continue to use them if you opt out of using the `librechat.yaml` configuration
|
||||||
|
|
@ -91,35 +105,80 @@ ANTHROPIC_API_KEY=user_provided
|
||||||
# AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME= # Deprecated
|
# AZURE_OPENAI_API_EMBEDDINGS_DEPLOYMENT_NAME= # Deprecated
|
||||||
# PLUGINS_USE_AZURE="true" # Deprecated
|
# PLUGINS_USE_AZURE="true" # Deprecated
|
||||||
|
|
||||||
#============#
|
#=================#
|
||||||
# BingAI #
|
# AWS Bedrock #
|
||||||
#============#
|
#=================#
|
||||||
|
|
||||||
BINGAI_TOKEN=user_provided
|
# BEDROCK_AWS_DEFAULT_REGION=us-east-1 # A default region must be provided
|
||||||
# BINGAI_HOST=https://cn.bing.com
|
# BEDROCK_AWS_ACCESS_KEY_ID=someAccessKey
|
||||||
|
# BEDROCK_AWS_SECRET_ACCESS_KEY=someSecretAccessKey
|
||||||
|
# BEDROCK_AWS_SESSION_TOKEN=someSessionToken
|
||||||
|
|
||||||
|
# Note: This example list is not meant to be exhaustive. If omitted, all known, supported model IDs will be included for you.
|
||||||
|
# BEDROCK_AWS_MODELS=anthropic.claude-3-5-sonnet-20240620-v1:0,meta.llama3-1-8b-instruct-v1:0
|
||||||
|
|
||||||
|
# See all Bedrock model IDs here: https://docs.aws.amazon.com/bedrock/latest/userguide/model-ids.html#model-ids-arns
|
||||||
|
|
||||||
|
# Notes on specific models:
|
||||||
|
# The following models are not support due to not supporting streaming:
|
||||||
|
# ai21.j2-mid-v1
|
||||||
|
|
||||||
|
# The following models are not support due to not supporting conversation history:
|
||||||
|
# ai21.j2-ultra-v1, cohere.command-text-v14, cohere.command-light-text-v14
|
||||||
|
|
||||||
#============#
|
#============#
|
||||||
# Google #
|
# Google #
|
||||||
#============#
|
#============#
|
||||||
|
|
||||||
GOOGLE_KEY=user_provided
|
GOOGLE_KEY=user_provided
|
||||||
# GOOGLE_MODELS=gemini-pro,gemini-pro-vision,chat-bison,chat-bison-32k,codechat-bison,codechat-bison-32k,text-bison,text-bison-32k,text-unicorn,code-gecko,code-bison,code-bison-32k
|
|
||||||
# GOOGLE_REVERSE_PROXY=
|
# GOOGLE_REVERSE_PROXY=
|
||||||
|
# Some reverse proxies do not support the X-goog-api-key header, uncomment to pass the API key in Authorization header instead.
|
||||||
|
# GOOGLE_AUTH_HEADER=true
|
||||||
|
|
||||||
|
# Gemini API (AI Studio)
|
||||||
|
# GOOGLE_MODELS=gemini-2.0-flash-exp,gemini-2.0-flash-thinking-exp-1219,gemini-exp-1121,gemini-exp-1114,gemini-1.5-flash-latest,gemini-1.0-pro,gemini-1.0-pro-001,gemini-1.0-pro-latest,gemini-1.0-pro-vision-latest,gemini-1.5-pro-latest,gemini-pro,gemini-pro-vision
|
||||||
|
|
||||||
|
# Vertex AI
|
||||||
|
# GOOGLE_MODELS=gemini-1.5-flash-preview-0514,gemini-1.5-pro-preview-0514,gemini-1.0-pro-vision-001,gemini-1.0-pro-002,gemini-1.0-pro-001,gemini-pro-vision,gemini-1.0-pro
|
||||||
|
|
||||||
|
# GOOGLE_TITLE_MODEL=gemini-pro
|
||||||
|
|
||||||
|
# GOOGLE_LOC=us-central1
|
||||||
|
|
||||||
|
# Google Safety Settings
|
||||||
|
# NOTE: These settings apply to both Vertex AI and Gemini API (AI Studio)
|
||||||
|
#
|
||||||
|
# For Vertex AI:
|
||||||
|
# To use the BLOCK_NONE setting, you need either:
|
||||||
|
# (a) Access through an allowlist via your Google account team, or
|
||||||
|
# (b) Switch to monthly invoiced billing: https://cloud.google.com/billing/docs/how-to/invoiced-billing
|
||||||
|
#
|
||||||
|
# For Gemini API (AI Studio):
|
||||||
|
# BLOCK_NONE is available by default, no special account requirements.
|
||||||
|
#
|
||||||
|
# Available options: BLOCK_NONE, BLOCK_ONLY_HIGH, BLOCK_MEDIUM_AND_ABOVE, BLOCK_LOW_AND_ABOVE
|
||||||
|
#
|
||||||
|
# GOOGLE_SAFETY_SEXUALLY_EXPLICIT=BLOCK_ONLY_HIGH
|
||||||
|
# GOOGLE_SAFETY_HATE_SPEECH=BLOCK_ONLY_HIGH
|
||||||
|
# GOOGLE_SAFETY_HARASSMENT=BLOCK_ONLY_HIGH
|
||||||
|
# GOOGLE_SAFETY_DANGEROUS_CONTENT=BLOCK_ONLY_HIGH
|
||||||
|
# GOOGLE_SAFETY_CIVIC_INTEGRITY=BLOCK_ONLY_HIGH
|
||||||
|
|
||||||
#============#
|
#============#
|
||||||
# OpenAI #
|
# OpenAI #
|
||||||
#============#
|
#============#
|
||||||
|
|
||||||
OPENAI_API_KEY=user_provided
|
OPENAI_API_KEY=user_provided
|
||||||
# OPENAI_MODELS=gpt-3.5-turbo-0125,gpt-3.5-turbo-0301,gpt-3.5-turbo,gpt-4,gpt-4-0613,gpt-4-vision-preview,gpt-3.5-turbo-0613,gpt-3.5-turbo-16k-0613,gpt-4-0125-preview,gpt-4-turbo-preview,gpt-4-1106-preview,gpt-3.5-turbo-1106,gpt-3.5-turbo-instruct,gpt-3.5-turbo-instruct-0914,gpt-3.5-turbo-16k
|
# OPENAI_MODELS=o1,o1-mini,o1-preview,gpt-4o,chatgpt-4o-latest,gpt-4o-mini,gpt-3.5-turbo-0125,gpt-3.5-turbo-0301,gpt-3.5-turbo,gpt-4,gpt-4-0613,gpt-4-vision-preview,gpt-3.5-turbo-0613,gpt-3.5-turbo-16k-0613,gpt-4-0125-preview,gpt-4-turbo-preview,gpt-4-1106-preview,gpt-3.5-turbo-1106,gpt-3.5-turbo-instruct,gpt-3.5-turbo-instruct-0914,gpt-3.5-turbo-16k
|
||||||
|
|
||||||
DEBUG_OPENAI=false
|
DEBUG_OPENAI=false
|
||||||
|
|
||||||
# TITLE_CONVO=false
|
# TITLE_CONVO=false
|
||||||
# OPENAI_TITLE_MODEL=gpt-3.5-turbo
|
# OPENAI_TITLE_MODEL=gpt-4o-mini
|
||||||
|
|
||||||
# OPENAI_SUMMARIZE=true
|
# OPENAI_SUMMARIZE=true
|
||||||
# OPENAI_SUMMARY_MODEL=gpt-3.5-turbo
|
# OPENAI_SUMMARY_MODEL=gpt-4o-mini
|
||||||
|
|
||||||
# OPENAI_FORCE_PROMPT=true
|
# OPENAI_FORCE_PROMPT=true
|
||||||
|
|
||||||
|
|
@ -133,19 +192,29 @@ DEBUG_OPENAI=false
|
||||||
|
|
||||||
ASSISTANTS_API_KEY=user_provided
|
ASSISTANTS_API_KEY=user_provided
|
||||||
# ASSISTANTS_BASE_URL=
|
# ASSISTANTS_BASE_URL=
|
||||||
# ASSISTANTS_MODELS=gpt-3.5-turbo-0125,gpt-3.5-turbo-16k-0613,gpt-3.5-turbo-16k,gpt-3.5-turbo,gpt-4,gpt-4-0314,gpt-4-32k-0314,gpt-4-0613,gpt-3.5-turbo-0613,gpt-3.5-turbo-1106,gpt-4-0125-preview,gpt-4-turbo-preview,gpt-4-1106-preview
|
# ASSISTANTS_MODELS=gpt-4o,gpt-4o-mini,gpt-3.5-turbo-0125,gpt-3.5-turbo-16k-0613,gpt-3.5-turbo-16k,gpt-3.5-turbo,gpt-4,gpt-4-0314,gpt-4-32k-0314,gpt-4-0613,gpt-3.5-turbo-0613,gpt-3.5-turbo-1106,gpt-4-0125-preview,gpt-4-turbo-preview,gpt-4-1106-preview
|
||||||
|
|
||||||
|
#==========================#
|
||||||
|
# Azure Assistants API #
|
||||||
|
#==========================#
|
||||||
|
|
||||||
|
# Note: You should map your credentials with custom variables according to your Azure OpenAI Configuration
|
||||||
|
# The models for Azure Assistants are also determined by your Azure OpenAI configuration.
|
||||||
|
|
||||||
|
# More info, including how to enable use of Assistants with Azure here:
|
||||||
|
# https://www.librechat.ai/docs/configuration/librechat_yaml/ai_endpoints/azure#using-assistants-with-azure
|
||||||
|
|
||||||
#============#
|
#============#
|
||||||
# OpenRouter #
|
# OpenRouter #
|
||||||
#============#
|
#============#
|
||||||
|
# !!!Warning: Use the variable above instead of this one. Using this one will override the OpenAI endpoint
|
||||||
# OPENROUTER_API_KEY=
|
# OPENROUTER_API_KEY=
|
||||||
|
|
||||||
#============#
|
#============#
|
||||||
# Plugins #
|
# Plugins #
|
||||||
#============#
|
#============#
|
||||||
|
|
||||||
# PLUGIN_MODELS=gpt-4,gpt-4-turbo-preview,gpt-4-0125-preview,gpt-4-1106-preview,gpt-4-0613,gpt-3.5-turbo,gpt-3.5-turbo-0125,gpt-3.5-turbo-1106,gpt-3.5-turbo-0613
|
# PLUGIN_MODELS=gpt-4o,gpt-4o-mini,gpt-4,gpt-4-turbo-preview,gpt-4-0125-preview,gpt-4-1106-preview,gpt-4-0613,gpt-3.5-turbo,gpt-3.5-turbo-0125,gpt-3.5-turbo-1106,gpt-3.5-turbo-0613
|
||||||
|
|
||||||
DEBUG_PLUGINS=true
|
DEBUG_PLUGINS=true
|
||||||
|
|
||||||
|
|
@ -180,11 +249,16 @@ AZURE_AI_SEARCH_SEARCH_OPTION_SELECT=
|
||||||
# DALLE3_AZURE_API_VERSION=
|
# DALLE3_AZURE_API_VERSION=
|
||||||
# DALLE2_AZURE_API_VERSION=
|
# DALLE2_AZURE_API_VERSION=
|
||||||
|
|
||||||
|
|
||||||
# Google
|
# Google
|
||||||
#-----------------
|
#-----------------
|
||||||
GOOGLE_API_KEY=
|
GOOGLE_SEARCH_API_KEY=
|
||||||
GOOGLE_CSE_ID=
|
GOOGLE_CSE_ID=
|
||||||
|
|
||||||
|
# YOUTUBE
|
||||||
|
#-----------------
|
||||||
|
YOUTUBE_API_KEY=
|
||||||
|
|
||||||
# SerpAPI
|
# SerpAPI
|
||||||
#-----------------
|
#-----------------
|
||||||
SERPAPI_API_KEY=
|
SERPAPI_API_KEY=
|
||||||
|
|
@ -218,6 +292,24 @@ MEILI_NO_ANALYTICS=true
|
||||||
MEILI_HOST=http://0.0.0.0:7700
|
MEILI_HOST=http://0.0.0.0:7700
|
||||||
MEILI_MASTER_KEY=DrhYf7zENyR6AlUCKmnz0eYASOQdl6zxH7s7MKFSfFCt
|
MEILI_MASTER_KEY=DrhYf7zENyR6AlUCKmnz0eYASOQdl6zxH7s7MKFSfFCt
|
||||||
|
|
||||||
|
#==================================================#
|
||||||
|
# Speech to Text & Text to Speech #
|
||||||
|
#==================================================#
|
||||||
|
|
||||||
|
STT_API_KEY=
|
||||||
|
TTS_API_KEY=
|
||||||
|
|
||||||
|
#==================================================#
|
||||||
|
# RAG #
|
||||||
|
#==================================================#
|
||||||
|
# More info: https://www.librechat.ai/docs/configuration/rag_api
|
||||||
|
|
||||||
|
# RAG_OPENAI_BASEURL=
|
||||||
|
# RAG_OPENAI_API_KEY=
|
||||||
|
# RAG_USE_FULL_CONTEXT=
|
||||||
|
# EMBEDDINGS_PROVIDER=openai
|
||||||
|
# EMBEDDINGS_MODEL=text-embedding-3-small
|
||||||
|
|
||||||
#===================================================#
|
#===================================================#
|
||||||
# User System #
|
# User System #
|
||||||
#===================================================#
|
#===================================================#
|
||||||
|
|
@ -263,6 +355,7 @@ ILLEGAL_MODEL_REQ_SCORE=5
|
||||||
#========================#
|
#========================#
|
||||||
|
|
||||||
CHECK_BALANCE=false
|
CHECK_BALANCE=false
|
||||||
|
# START_BALANCE=20000 # note: the number of tokens that will be credited after registration.
|
||||||
|
|
||||||
#========================#
|
#========================#
|
||||||
# Registration and Login #
|
# Registration and Login #
|
||||||
|
|
@ -272,6 +365,9 @@ ALLOW_EMAIL_LOGIN=true
|
||||||
ALLOW_REGISTRATION=true
|
ALLOW_REGISTRATION=true
|
||||||
ALLOW_SOCIAL_LOGIN=false
|
ALLOW_SOCIAL_LOGIN=false
|
||||||
ALLOW_SOCIAL_REGISTRATION=false
|
ALLOW_SOCIAL_REGISTRATION=false
|
||||||
|
ALLOW_PASSWORD_RESET=false
|
||||||
|
# ALLOW_ACCOUNT_DELETION=true # note: enabled by default if omitted/commented out
|
||||||
|
ALLOW_UNVERIFIED_EMAIL_LOGIN=true
|
||||||
|
|
||||||
SESSION_EXPIRY=1000 * 60 * 15
|
SESSION_EXPIRY=1000 * 60 * 15
|
||||||
REFRESH_TOKEN_EXPIRY=(1000 * 60 * 60 * 24) * 7
|
REFRESH_TOKEN_EXPIRY=(1000 * 60 * 60 * 24) * 7
|
||||||
|
|
@ -293,12 +389,22 @@ FACEBOOK_CALLBACK_URL=/oauth/facebook/callback
|
||||||
GITHUB_CLIENT_ID=
|
GITHUB_CLIENT_ID=
|
||||||
GITHUB_CLIENT_SECRET=
|
GITHUB_CLIENT_SECRET=
|
||||||
GITHUB_CALLBACK_URL=/oauth/github/callback
|
GITHUB_CALLBACK_URL=/oauth/github/callback
|
||||||
|
# GitHub Eenterprise
|
||||||
|
# GITHUB_ENTERPRISE_BASE_URL=
|
||||||
|
# GITHUB_ENTERPRISE_USER_AGENT=
|
||||||
|
|
||||||
# Google
|
# Google
|
||||||
GOOGLE_CLIENT_ID=
|
GOOGLE_CLIENT_ID=
|
||||||
GOOGLE_CLIENT_SECRET=
|
GOOGLE_CLIENT_SECRET=
|
||||||
GOOGLE_CALLBACK_URL=/oauth/google/callback
|
GOOGLE_CALLBACK_URL=/oauth/google/callback
|
||||||
|
|
||||||
|
# Apple
|
||||||
|
APPLE_CLIENT_ID=
|
||||||
|
APPLE_TEAM_ID=
|
||||||
|
APPLE_KEY_ID=
|
||||||
|
APPLE_PRIVATE_KEY_PATH=
|
||||||
|
APPLE_CALLBACK_URL=/oauth/apple/callback
|
||||||
|
|
||||||
# OpenID
|
# OpenID
|
||||||
OPENID_CLIENT_ID=
|
OPENID_CLIENT_ID=
|
||||||
OPENID_CLIENT_SECRET=
|
OPENID_CLIENT_SECRET=
|
||||||
|
|
@ -306,10 +412,31 @@ OPENID_ISSUER=
|
||||||
OPENID_SESSION_SECRET=
|
OPENID_SESSION_SECRET=
|
||||||
OPENID_SCOPE="openid profile email"
|
OPENID_SCOPE="openid profile email"
|
||||||
OPENID_CALLBACK_URL=/oauth/openid/callback
|
OPENID_CALLBACK_URL=/oauth/openid/callback
|
||||||
|
OPENID_REQUIRED_ROLE=
|
||||||
|
OPENID_REQUIRED_ROLE_TOKEN_KIND=
|
||||||
|
OPENID_REQUIRED_ROLE_PARAMETER_PATH=
|
||||||
|
# Set to determine which user info property returned from OpenID Provider to store as the User's username
|
||||||
|
OPENID_USERNAME_CLAIM=
|
||||||
|
# Set to determine which user info property returned from OpenID Provider to store as the User's name
|
||||||
|
OPENID_NAME_CLAIM=
|
||||||
|
|
||||||
OPENID_BUTTON_LABEL=
|
OPENID_BUTTON_LABEL=
|
||||||
OPENID_IMAGE_URL=
|
OPENID_IMAGE_URL=
|
||||||
|
|
||||||
|
# LDAP
|
||||||
|
LDAP_URL=
|
||||||
|
LDAP_BIND_DN=
|
||||||
|
LDAP_BIND_CREDENTIALS=
|
||||||
|
LDAP_USER_SEARCH_BASE=
|
||||||
|
LDAP_SEARCH_FILTER=mail={{username}}
|
||||||
|
LDAP_CA_CERT_PATH=
|
||||||
|
# LDAP_TLS_REJECT_UNAUTHORIZED=
|
||||||
|
# LDAP_LOGIN_USES_USERNAME=true
|
||||||
|
# LDAP_ID=
|
||||||
|
# LDAP_USERNAME=
|
||||||
|
# LDAP_EMAIL=
|
||||||
|
# LDAP_FULL_NAME=
|
||||||
|
|
||||||
#========================#
|
#========================#
|
||||||
# Email Password Reset #
|
# Email Password Reset #
|
||||||
#========================#
|
#========================#
|
||||||
|
|
@ -336,6 +463,25 @@ FIREBASE_STORAGE_BUCKET=
|
||||||
FIREBASE_MESSAGING_SENDER_ID=
|
FIREBASE_MESSAGING_SENDER_ID=
|
||||||
FIREBASE_APP_ID=
|
FIREBASE_APP_ID=
|
||||||
|
|
||||||
|
#========================#
|
||||||
|
# Shared Links #
|
||||||
|
#========================#
|
||||||
|
|
||||||
|
ALLOW_SHARED_LINKS=true
|
||||||
|
ALLOW_SHARED_LINKS_PUBLIC=true
|
||||||
|
|
||||||
|
#==============================#
|
||||||
|
# Static File Cache Control #
|
||||||
|
#==============================#
|
||||||
|
|
||||||
|
# Leave commented out to use defaults: 1 day (86400 seconds) for s-maxage and 2 days (172800 seconds) for max-age
|
||||||
|
# NODE_ENV must be set to production for these to take effect
|
||||||
|
# STATIC_CACHE_MAX_AGE=172800
|
||||||
|
# STATIC_CACHE_S_MAX_AGE=86400
|
||||||
|
|
||||||
|
# If you have another service in front of your LibreChat doing compression, disable express based compression here
|
||||||
|
# DISABLE_COMPRESSION=true
|
||||||
|
|
||||||
#===================================================#
|
#===================================================#
|
||||||
# UI #
|
# UI #
|
||||||
#===================================================#
|
#===================================================#
|
||||||
|
|
@ -346,6 +492,9 @@ HELP_AND_FAQ_URL=https://librechat.ai
|
||||||
|
|
||||||
# SHOW_BIRTHDAY_ICON=true
|
# SHOW_BIRTHDAY_ICON=true
|
||||||
|
|
||||||
|
# Google tag manager id
|
||||||
|
#ANALYTICS_GTM_ID=user provided google tag manager id
|
||||||
|
|
||||||
#==================================================#
|
#==================================================#
|
||||||
# Others #
|
# Others #
|
||||||
#==================================================#
|
#==================================================#
|
||||||
|
|
@ -358,3 +507,24 @@ HELP_AND_FAQ_URL=https://librechat.ai
|
||||||
|
|
||||||
# E2E_USER_EMAIL=
|
# E2E_USER_EMAIL=
|
||||||
# E2E_USER_PASSWORD=
|
# E2E_USER_PASSWORD=
|
||||||
|
|
||||||
|
#=====================================================#
|
||||||
|
# Cache Headers #
|
||||||
|
#=====================================================#
|
||||||
|
# Headers that control caching of the index.html #
|
||||||
|
# Default configuration prevents caching to ensure #
|
||||||
|
# users always get the latest version. Customize #
|
||||||
|
# only if you understand caching implications. #
|
||||||
|
|
||||||
|
# INDEX_HTML_CACHE_CONTROL=no-cache, no-store, must-revalidate
|
||||||
|
# INDEX_HTML_PRAGMA=no-cache
|
||||||
|
# INDEX_HTML_EXPIRES=0
|
||||||
|
|
||||||
|
# no-cache: Forces validation with server before using cached version
|
||||||
|
# no-store: Prevents storing the response entirely
|
||||||
|
# must-revalidate: Prevents using stale content when offline
|
||||||
|
|
||||||
|
#=====================================================#
|
||||||
|
# OpenWeather #
|
||||||
|
#=====================================================#
|
||||||
|
OPENWEATHER_API_KEY=
|
||||||
161
.eslintrc.js
161
.eslintrc.js
|
|
@ -1,161 +0,0 @@
|
||||||
module.exports = {
|
|
||||||
env: {
|
|
||||||
browser: true,
|
|
||||||
es2021: true,
|
|
||||||
node: true,
|
|
||||||
commonjs: true,
|
|
||||||
es6: true,
|
|
||||||
},
|
|
||||||
extends: [
|
|
||||||
'eslint:recommended',
|
|
||||||
'plugin:react/recommended',
|
|
||||||
'plugin:react-hooks/recommended',
|
|
||||||
'plugin:jest/recommended',
|
|
||||||
'prettier',
|
|
||||||
],
|
|
||||||
ignorePatterns: [
|
|
||||||
'client/dist/**/*',
|
|
||||||
'client/public/**/*',
|
|
||||||
'e2e/playwright-report/**/*',
|
|
||||||
'packages/data-provider/types/**/*',
|
|
||||||
'packages/data-provider/dist/**/*',
|
|
||||||
'data-node/**/*',
|
|
||||||
'meili_data/**/*',
|
|
||||||
'node_modules/**/*',
|
|
||||||
],
|
|
||||||
parser: '@typescript-eslint/parser',
|
|
||||||
parserOptions: {
|
|
||||||
ecmaVersion: 'latest',
|
|
||||||
sourceType: 'module',
|
|
||||||
ecmaFeatures: {
|
|
||||||
jsx: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
plugins: ['react', 'react-hooks', '@typescript-eslint', 'import'],
|
|
||||||
rules: {
|
|
||||||
'react/react-in-jsx-scope': 'off',
|
|
||||||
'@typescript-eslint/ban-ts-comment': ['error', { 'ts-ignore': 'allow' }],
|
|
||||||
indent: ['error', 2, { SwitchCase: 1 }],
|
|
||||||
'max-len': [
|
|
||||||
'error',
|
|
||||||
{
|
|
||||||
code: 120,
|
|
||||||
ignoreStrings: true,
|
|
||||||
ignoreTemplateLiterals: true,
|
|
||||||
ignoreComments: true,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
'linebreak-style': 0,
|
|
||||||
curly: ['error', 'all'],
|
|
||||||
semi: ['error', 'always'],
|
|
||||||
'object-curly-spacing': ['error', 'always'],
|
|
||||||
'no-multiple-empty-lines': ['error', { max: 1 }],
|
|
||||||
'no-trailing-spaces': 'error',
|
|
||||||
'comma-dangle': ['error', 'always-multiline'],
|
|
||||||
// "arrow-parens": [2, "as-needed", { requireForBlockBody: true }],
|
|
||||||
// 'no-plusplus': ['error', { allowForLoopAfterthoughts: true }],
|
|
||||||
'no-console': 'off',
|
|
||||||
'import/no-cycle': 'error',
|
|
||||||
'import/no-self-import': 'error',
|
|
||||||
'import/extensions': 'off',
|
|
||||||
'no-promise-executor-return': 'off',
|
|
||||||
'no-param-reassign': 'off',
|
|
||||||
'no-continue': 'off',
|
|
||||||
'no-restricted-syntax': 'off',
|
|
||||||
'react/prop-types': ['off'],
|
|
||||||
'react/display-name': ['off'],
|
|
||||||
'no-unused-vars': ['error', { varsIgnorePattern: '^_' }],
|
|
||||||
quotes: ['error', 'single'],
|
|
||||||
},
|
|
||||||
overrides: [
|
|
||||||
{
|
|
||||||
files: ['**/*.ts', '**/*.tsx'],
|
|
||||||
rules: {
|
|
||||||
'no-unused-vars': 'off', // off because it conflicts with '@typescript-eslint/no-unused-vars'
|
|
||||||
'react/display-name': 'off',
|
|
||||||
'@typescript-eslint/no-unused-vars': 'warn',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
files: ['rollup.config.js', '.eslintrc.js', 'jest.config.js'],
|
|
||||||
env: {
|
|
||||||
node: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
files: [
|
|
||||||
'**/*.test.js',
|
|
||||||
'**/*.test.jsx',
|
|
||||||
'**/*.test.ts',
|
|
||||||
'**/*.test.tsx',
|
|
||||||
'**/*.spec.js',
|
|
||||||
'**/*.spec.jsx',
|
|
||||||
'**/*.spec.ts',
|
|
||||||
'**/*.spec.tsx',
|
|
||||||
'setupTests.js',
|
|
||||||
],
|
|
||||||
env: {
|
|
||||||
jest: true,
|
|
||||||
node: true,
|
|
||||||
},
|
|
||||||
rules: {
|
|
||||||
'react/display-name': 'off',
|
|
||||||
'react/prop-types': 'off',
|
|
||||||
'react/no-unescaped-entities': 'off',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
files: ['**/*.ts', '**/*.tsx'],
|
|
||||||
parser: '@typescript-eslint/parser',
|
|
||||||
parserOptions: {
|
|
||||||
project: './client/tsconfig.json',
|
|
||||||
},
|
|
||||||
plugins: ['@typescript-eslint/eslint-plugin', 'jest'],
|
|
||||||
extends: [
|
|
||||||
'plugin:@typescript-eslint/eslint-recommended',
|
|
||||||
'plugin:@typescript-eslint/recommended',
|
|
||||||
],
|
|
||||||
rules: {
|
|
||||||
'@typescript-eslint/no-explicit-any': 'error',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
files: './packages/data-provider/**/*.ts',
|
|
||||||
overrides: [
|
|
||||||
{
|
|
||||||
files: '**/*.ts',
|
|
||||||
parser: '@typescript-eslint/parser',
|
|
||||||
parserOptions: {
|
|
||||||
project: './packages/data-provider/tsconfig.json',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
files: ['./packages/data-provider/specs/**/*.ts'],
|
|
||||||
parserOptions: {
|
|
||||||
project: './packages/data-provider/tsconfig.spec.json',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
settings: {
|
|
||||||
react: {
|
|
||||||
createClass: 'createReactClass', // Regex for Component Factory to use,
|
|
||||||
// default to "createReactClass"
|
|
||||||
pragma: 'React', // Pragma to use, default to "React"
|
|
||||||
fragment: 'Fragment', // Fragment to use (may be a property of <pragma>), default to "Fragment"
|
|
||||||
version: 'detect', // React version. "detect" automatically picks the version you have installed.
|
|
||||||
},
|
|
||||||
'import/parsers': {
|
|
||||||
'@typescript-eslint/parser': ['.ts', '.tsx'],
|
|
||||||
},
|
|
||||||
'import/resolver': {
|
|
||||||
typescript: {
|
|
||||||
project: ['./client/tsconfig.json'],
|
|
||||||
},
|
|
||||||
node: {
|
|
||||||
project: ['./client/tsconfig.json'],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
12
.github/CONTRIBUTING.md
vendored
12
.github/CONTRIBUTING.md
vendored
|
|
@ -126,6 +126,18 @@ Apply the following naming conventions to branches, labels, and other Git-relate
|
||||||
|
|
||||||
- **Current Stance**: At present, this backend transition is of lower priority and might not be pursued.
|
- **Current Stance**: At present, this backend transition is of lower priority and might not be pursued.
|
||||||
|
|
||||||
|
## 7. Module Import Conventions
|
||||||
|
|
||||||
|
- `npm` packages first,
|
||||||
|
- from shortest line (top) to longest (bottom)
|
||||||
|
|
||||||
|
- Followed by typescript types (pertains to data-provider and client workspaces)
|
||||||
|
- longest line (top) to shortest (bottom)
|
||||||
|
- types from package come first
|
||||||
|
|
||||||
|
- Lastly, local imports
|
||||||
|
- longest line (top) to shortest (bottom)
|
||||||
|
- imports with alias `~` treated the same as relative import with respect to line length
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
|
||||||
44
.github/ISSUE_TEMPLATE/BUG-REPORT.yml
vendored
44
.github/ISSUE_TEMPLATE/BUG-REPORT.yml
vendored
|
|
@ -1,12 +1,19 @@
|
||||||
name: Bug Report
|
name: Bug Report
|
||||||
description: File a bug report
|
description: File a bug report
|
||||||
title: "[Bug]: "
|
title: "[Bug]: "
|
||||||
labels: ["bug"]
|
labels: ["🐛 bug"]
|
||||||
body:
|
body:
|
||||||
- type: markdown
|
- type: markdown
|
||||||
attributes:
|
attributes:
|
||||||
value: |
|
value: |
|
||||||
Thanks for taking the time to fill out this bug report!
|
Thanks for taking the time to fill out this bug report!
|
||||||
|
|
||||||
|
Before submitting, please:
|
||||||
|
- Search existing [Issues and Discussions](https://github.com/danny-avila/LibreChat/discussions) to see if your bug has already been reported
|
||||||
|
- Use [Discussions](https://github.com/danny-avila/LibreChat/discussions) instead of Issues for:
|
||||||
|
- General inquiries
|
||||||
|
- Help with setup
|
||||||
|
- Questions about whether you're experiencing a bug
|
||||||
- type: textarea
|
- type: textarea
|
||||||
id: what-happened
|
id: what-happened
|
||||||
attributes:
|
attributes:
|
||||||
|
|
@ -15,6 +22,23 @@ body:
|
||||||
placeholder: Please give as many details as possible
|
placeholder: Please give as many details as possible
|
||||||
validations:
|
validations:
|
||||||
required: true
|
required: true
|
||||||
|
- type: textarea
|
||||||
|
id: version-info
|
||||||
|
attributes:
|
||||||
|
label: Version Information
|
||||||
|
description: |
|
||||||
|
If using Docker, please run and provide the output of:
|
||||||
|
```bash
|
||||||
|
docker images | grep librechat
|
||||||
|
```
|
||||||
|
|
||||||
|
If running from source, please run and provide the output of:
|
||||||
|
```bash
|
||||||
|
git rev-parse HEAD
|
||||||
|
```
|
||||||
|
placeholder: Paste the output here
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
- type: textarea
|
- type: textarea
|
||||||
id: steps-to-reproduce
|
id: steps-to-reproduce
|
||||||
attributes:
|
attributes:
|
||||||
|
|
@ -39,7 +63,21 @@ body:
|
||||||
id: logs
|
id: logs
|
||||||
attributes:
|
attributes:
|
||||||
label: Relevant log output
|
label: Relevant log output
|
||||||
description: Please copy and paste any relevant log output. This will be automatically formatted into code, so no need for backticks.
|
description: |
|
||||||
|
Please paste relevant logs that were created when reproducing the error.
|
||||||
|
|
||||||
|
Log locations:
|
||||||
|
- Docker: Project root directory ./logs
|
||||||
|
- npm: ./api/logs
|
||||||
|
|
||||||
|
There are two types of logs that can help diagnose the issue:
|
||||||
|
- debug logs (debug-YYYY-MM-DD.log)
|
||||||
|
- error logs (error-YYYY-MM-DD.log)
|
||||||
|
|
||||||
|
Error logs contain exact stack traces and are especially helpful, but both can provide valuable information.
|
||||||
|
Please only include the relevant portions of logs that correspond to when you reproduced the error.
|
||||||
|
|
||||||
|
For UI-related issues, browser console logs can be very helpful. You can provide these as screenshots or paste the text here.
|
||||||
render: shell
|
render: shell
|
||||||
- type: textarea
|
- type: textarea
|
||||||
id: screenshots
|
id: screenshots
|
||||||
|
|
@ -50,7 +88,7 @@ body:
|
||||||
id: terms
|
id: terms
|
||||||
attributes:
|
attributes:
|
||||||
label: Code of Conduct
|
label: Code of Conduct
|
||||||
description: By submitting this issue, you agree to follow our [Code of Conduct](https://github.com/danny-avila/LibreChat/blob/main/CODE_OF_CONDUCT.md)
|
description: By submitting this issue, you agree to follow our [Code of Conduct](https://github.com/danny-avila/LibreChat/blob/main/.github/CODE_OF_CONDUCT.md)
|
||||||
options:
|
options:
|
||||||
- label: I agree to follow this project's Code of Conduct
|
- label: I agree to follow this project's Code of Conduct
|
||||||
required: true
|
required: true
|
||||||
6
.github/ISSUE_TEMPLATE/FEATURE-REQUEST.yml
vendored
6
.github/ISSUE_TEMPLATE/FEATURE-REQUEST.yml
vendored
|
|
@ -1,7 +1,7 @@
|
||||||
name: Feature Request
|
name: Feature Request
|
||||||
description: File a feature request
|
description: File a feature request
|
||||||
title: "Enhancement: "
|
title: "[Enhancement]: "
|
||||||
labels: ["enhancement"]
|
labels: ["✨ enhancement"]
|
||||||
body:
|
body:
|
||||||
- type: markdown
|
- type: markdown
|
||||||
attributes:
|
attributes:
|
||||||
|
|
@ -43,7 +43,7 @@ body:
|
||||||
id: terms
|
id: terms
|
||||||
attributes:
|
attributes:
|
||||||
label: Code of Conduct
|
label: Code of Conduct
|
||||||
description: By submitting this issue, you agree to follow our [Code of Conduct](https://github.com/danny-avila/LibreChat/blob/main/CODE_OF_CONDUCT.md)
|
description: By submitting this issue, you agree to follow our [Code of Conduct](https://github.com/danny-avila/LibreChat/blob/main/.github/CODE_OF_CONDUCT.md)
|
||||||
options:
|
options:
|
||||||
- label: I agree to follow this project's Code of Conduct
|
- label: I agree to follow this project's Code of Conduct
|
||||||
required: true
|
required: true
|
||||||
|
|
|
||||||
33
.github/ISSUE_TEMPLATE/NEW-LANGUAGE-REQUEST.yml
vendored
Normal file
33
.github/ISSUE_TEMPLATE/NEW-LANGUAGE-REQUEST.yml
vendored
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
name: New Language Request
|
||||||
|
description: Request to add a new language for LibreChat translations.
|
||||||
|
title: "New Language Request: "
|
||||||
|
labels: ["✨ enhancement", "🌍 i18n"]
|
||||||
|
body:
|
||||||
|
- type: markdown
|
||||||
|
attributes:
|
||||||
|
value: |
|
||||||
|
Thank you for taking the time to submit a new language request! Please fill out the following details so we can review your request.
|
||||||
|
- type: input
|
||||||
|
id: language_name
|
||||||
|
attributes:
|
||||||
|
label: Language Name
|
||||||
|
description: Please provide the full name of the language (e.g., Spanish, Mandarin).
|
||||||
|
placeholder: e.g., Spanish
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: input
|
||||||
|
id: iso_code
|
||||||
|
attributes:
|
||||||
|
label: ISO 639-1 Code
|
||||||
|
description: Please provide the ISO 639-1 code for the language (e.g., es for Spanish). You can refer to [this list](https://www.w3schools.com/tags/ref_language_codes.asp) for valid codes.
|
||||||
|
placeholder: e.g., es
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: checkboxes
|
||||||
|
id: terms
|
||||||
|
attributes:
|
||||||
|
label: Code of Conduct
|
||||||
|
description: By submitting this issue, you agree to follow our [Code of Conduct](https://github.com/danny-avila/LibreChat/blob/main/.github/CODE_OF_CONDUCT.md).
|
||||||
|
options:
|
||||||
|
- label: I agree to follow this project's Code of Conduct
|
||||||
|
required: true
|
||||||
4
.github/ISSUE_TEMPLATE/QUESTION.yml
vendored
4
.github/ISSUE_TEMPLATE/QUESTION.yml
vendored
|
|
@ -1,7 +1,7 @@
|
||||||
name: Question
|
name: Question
|
||||||
description: Ask your question
|
description: Ask your question
|
||||||
title: "[Question]: "
|
title: "[Question]: "
|
||||||
labels: ["question"]
|
labels: ["❓ question"]
|
||||||
body:
|
body:
|
||||||
- type: markdown
|
- type: markdown
|
||||||
attributes:
|
attributes:
|
||||||
|
|
@ -44,7 +44,7 @@ body:
|
||||||
id: terms
|
id: terms
|
||||||
attributes:
|
attributes:
|
||||||
label: Code of Conduct
|
label: Code of Conduct
|
||||||
description: By submitting this issue, you agree to follow our [Code of Conduct](https://github.com/danny-avila/LibreChat/blob/main/CODE_OF_CONDUCT.md)
|
description: By submitting this issue, you agree to follow our [Code of Conduct](https://github.com/danny-avila/LibreChat/blob/main/.github/CODE_OF_CONDUCT.md)
|
||||||
options:
|
options:
|
||||||
- label: I agree to follow this project's Code of Conduct
|
- label: I agree to follow this project's Code of Conduct
|
||||||
required: true
|
required: true
|
||||||
|
|
|
||||||
47
.github/dependabot.yml
vendored
47
.github/dependabot.yml
vendored
|
|
@ -1,47 +0,0 @@
|
||||||
# To get started with Dependabot version updates, you'll need to specify which
|
|
||||||
# package ecosystems to update and where the package manifests are located.
|
|
||||||
# Please see the documentation for all configuration options:
|
|
||||||
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
|
|
||||||
|
|
||||||
version: 2
|
|
||||||
updates:
|
|
||||||
- package-ecosystem: "npm" # See documentation for possible values
|
|
||||||
directory: "/api" # Location of package manifests
|
|
||||||
target-branch: "dev"
|
|
||||||
versioning-strategy: increase-if-necessary
|
|
||||||
schedule:
|
|
||||||
interval: "weekly"
|
|
||||||
allow:
|
|
||||||
# Allow both direct and indirect updates for all packages
|
|
||||||
- dependency-type: "all"
|
|
||||||
commit-message:
|
|
||||||
prefix: "npm api prod"
|
|
||||||
prefix-development: "npm api dev"
|
|
||||||
include: "scope"
|
|
||||||
- package-ecosystem: "npm" # See documentation for possible values
|
|
||||||
directory: "/client" # Location of package manifests
|
|
||||||
target-branch: "dev"
|
|
||||||
versioning-strategy: increase-if-necessary
|
|
||||||
schedule:
|
|
||||||
interval: "weekly"
|
|
||||||
allow:
|
|
||||||
# Allow both direct and indirect updates for all packages
|
|
||||||
- dependency-type: "all"
|
|
||||||
commit-message:
|
|
||||||
prefix: "npm client prod"
|
|
||||||
prefix-development: "npm client dev"
|
|
||||||
include: "scope"
|
|
||||||
- package-ecosystem: "npm" # See documentation for possible values
|
|
||||||
directory: "/" # Location of package manifests
|
|
||||||
target-branch: "dev"
|
|
||||||
versioning-strategy: increase-if-necessary
|
|
||||||
schedule:
|
|
||||||
interval: "weekly"
|
|
||||||
allow:
|
|
||||||
# Allow both direct and indirect updates for all packages
|
|
||||||
- dependency-type: "all"
|
|
||||||
commit-message:
|
|
||||||
prefix: "npm all prod"
|
|
||||||
prefix-development: "npm all dev"
|
|
||||||
include: "scope"
|
|
||||||
|
|
||||||
9
.github/pull_request_template.md
vendored
9
.github/pull_request_template.md
vendored
|
|
@ -1,7 +1,10 @@
|
||||||
# Pull Request Template
|
# Pull Request Template
|
||||||
|
|
||||||
|
⚠️ Before Submitting a PR, Please Review:
|
||||||
|
- Please ensure that you have thoroughly read and understood the [Contributing Docs](https://github.com/danny-avila/LibreChat/blob/main/.github/CONTRIBUTING.md) before submitting your Pull Request.
|
||||||
|
|
||||||
### ⚠️ Before Submitting a PR, read the [Contributing Docs](https://github.com/danny-avila/LibreChat/blob/main/.github/CONTRIBUTING.md) in full!
|
⚠️ Documentation Updates Notice:
|
||||||
|
- Kindly note that documentation updates are managed in this repository: [librechat.ai](https://github.com/LibreChat-AI/librechat.ai)
|
||||||
|
|
||||||
## Summary
|
## Summary
|
||||||
|
|
||||||
|
|
@ -15,7 +18,6 @@ Please delete any irrelevant options.
|
||||||
- [ ] New feature (non-breaking change which adds functionality)
|
- [ ] New feature (non-breaking change which adds functionality)
|
||||||
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
|
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
|
||||||
- [ ] This change requires a documentation update
|
- [ ] This change requires a documentation update
|
||||||
- [ ] Documentation update
|
|
||||||
- [ ] Translation update
|
- [ ] Translation update
|
||||||
|
|
||||||
## Testing
|
## Testing
|
||||||
|
|
@ -26,6 +28,8 @@ Please describe your test process and include instructions so that we can reprod
|
||||||
|
|
||||||
## Checklist
|
## Checklist
|
||||||
|
|
||||||
|
Please delete any irrelevant options.
|
||||||
|
|
||||||
- [ ] My code adheres to this project's style guidelines
|
- [ ] My code adheres to this project's style guidelines
|
||||||
- [ ] I have performed a self-review of my own code
|
- [ ] I have performed a self-review of my own code
|
||||||
- [ ] I have commented in any complex areas of my code
|
- [ ] I have commented in any complex areas of my code
|
||||||
|
|
@ -34,3 +38,4 @@ Please describe your test process and include instructions so that we can reprod
|
||||||
- [ ] I have written tests demonstrating that my changes are effective or that my feature works
|
- [ ] I have written tests demonstrating that my changes are effective or that my feature works
|
||||||
- [ ] Local unit tests pass with my changes
|
- [ ] Local unit tests pass with my changes
|
||||||
- [ ] Any changes dependent on mine have been merged and published in downstream modules.
|
- [ ] Any changes dependent on mine have been merged and published in downstream modules.
|
||||||
|
- [ ] A pull request for updating the documentation has been submitted.
|
||||||
|
|
|
||||||
26
.github/workflows/a11y.yml
vendored
Normal file
26
.github/workflows/a11y.yml
vendored
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
name: Lint for accessibility issues
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
paths:
|
||||||
|
- 'client/src/**'
|
||||||
|
workflow_dispatch:
|
||||||
|
inputs:
|
||||||
|
run_workflow:
|
||||||
|
description: 'Set to true to run this workflow'
|
||||||
|
required: true
|
||||||
|
default: 'false'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
axe-linter:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
if: >
|
||||||
|
(github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == 'danny-avila/LibreChat') ||
|
||||||
|
(github.event_name == 'workflow_dispatch' && github.event.inputs.run_workflow == 'true')
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- uses: dequelabs/axe-linter-action@v1
|
||||||
|
with:
|
||||||
|
api_key: ${{ secrets.AXE_LINTER_API_KEY }}
|
||||||
|
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
28
.github/workflows/backend-review.yml
vendored
28
.github/workflows/backend-review.yml
vendored
|
|
@ -33,16 +33,32 @@ jobs:
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: npm ci
|
run: npm ci
|
||||||
|
|
||||||
- name: Install Data Provider
|
- name: Install Data Provider Package
|
||||||
run: npm run build:data-provider
|
run: npm run build:data-provider
|
||||||
|
|
||||||
|
- name: Install MCP Package
|
||||||
|
run: npm run build:mcp
|
||||||
|
|
||||||
|
- name: Create empty auth.json file
|
||||||
|
run: |
|
||||||
|
mkdir -p api/data
|
||||||
|
echo '{}' > api/data/auth.json
|
||||||
|
|
||||||
|
- name: Check for Circular dependency in rollup
|
||||||
|
working-directory: ./packages/data-provider
|
||||||
|
run: |
|
||||||
|
output=$(npm run rollup:api)
|
||||||
|
echo "$output"
|
||||||
|
if echo "$output" | grep -q "Circular dependency"; then
|
||||||
|
echo "Error: Circular dependency detected!"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
- name: Prepare .env.test file
|
||||||
|
run: cp api/test/.env.test.example api/test/.env.test
|
||||||
|
|
||||||
- name: Run unit tests
|
- name: Run unit tests
|
||||||
run: cd api && npm run test:ci
|
run: cd api && npm run test:ci
|
||||||
|
|
||||||
- name: Run librechat-data-provider unit tests
|
- name: Run librechat-data-provider unit tests
|
||||||
run: cd packages/data-provider && npm run test:ci
|
run: cd packages/data-provider && npm run test:ci
|
||||||
|
|
||||||
- name: Run linters
|
|
||||||
uses: wearerequired/lint-action@v2
|
|
||||||
with:
|
|
||||||
eslint: true
|
|
||||||
83
.github/workflows/container.yml
vendored
83
.github/workflows/container.yml
vendored
|
|
@ -1,83 +0,0 @@
|
||||||
name: Docker Compose Build on Tag
|
|
||||||
|
|
||||||
# The workflow is triggered when a tag is pushed
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
tags:
|
|
||||||
- "*"
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
|
|
||||||
steps:
|
|
||||||
# Check out the repository
|
|
||||||
- name: Checkout
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
|
|
||||||
# Set up Docker
|
|
||||||
- name: Set up Docker
|
|
||||||
uses: docker/setup-buildx-action@v3
|
|
||||||
|
|
||||||
# Set up QEMU for cross-platform builds
|
|
||||||
- name: Set up QEMU
|
|
||||||
uses: docker/setup-qemu-action@v3
|
|
||||||
|
|
||||||
# Log in to GitHub Container Registry
|
|
||||||
- name: Log in to GitHub Container Registry
|
|
||||||
uses: docker/login-action@v2
|
|
||||||
with:
|
|
||||||
registry: ghcr.io
|
|
||||||
username: ${{ github.actor }}
|
|
||||||
password: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
|
|
||||||
# Prepare Docker Build
|
|
||||||
- name: Build Docker images
|
|
||||||
run: |
|
|
||||||
cp .env.example .env
|
|
||||||
|
|
||||||
# Tag and push librechat-api
|
|
||||||
- name: Docker metadata for librechat-api
|
|
||||||
id: meta-librechat-api
|
|
||||||
uses: docker/metadata-action@v5
|
|
||||||
with:
|
|
||||||
images: |
|
|
||||||
ghcr.io/${{ github.repository_owner }}/librechat-api
|
|
||||||
tags: |
|
|
||||||
type=raw,value=latest
|
|
||||||
type=semver,pattern={{version}}
|
|
||||||
type=semver,pattern={{major}}
|
|
||||||
type=semver,pattern={{major}}.{{minor}}
|
|
||||||
|
|
||||||
- name: Build and librechat-api
|
|
||||||
uses: docker/build-push-action@v5
|
|
||||||
with:
|
|
||||||
file: Dockerfile.multi
|
|
||||||
context: .
|
|
||||||
push: true
|
|
||||||
tags: ${{ steps.meta-librechat-api.outputs.tags }}
|
|
||||||
platforms: linux/amd64,linux/arm64
|
|
||||||
target: api-build
|
|
||||||
|
|
||||||
# Tag and push librechat
|
|
||||||
- name: Docker metadata for librechat
|
|
||||||
id: meta-librechat
|
|
||||||
uses: docker/metadata-action@v5
|
|
||||||
with:
|
|
||||||
images: |
|
|
||||||
ghcr.io/${{ github.repository_owner }}/librechat
|
|
||||||
tags: |
|
|
||||||
type=raw,value=latest
|
|
||||||
type=semver,pattern={{version}}
|
|
||||||
type=semver,pattern={{major}}
|
|
||||||
type=semver,pattern={{major}}.{{minor}}
|
|
||||||
|
|
||||||
- name: Build and librechat
|
|
||||||
uses: docker/build-push-action@v5
|
|
||||||
with:
|
|
||||||
file: Dockerfile
|
|
||||||
context: .
|
|
||||||
push: true
|
|
||||||
tags: ${{ steps.meta-librechat.outputs.tags }}
|
|
||||||
platforms: linux/amd64,linux/arm64
|
|
||||||
target: node
|
|
||||||
41
.github/workflows/deploy-dev.yml
vendored
Normal file
41
.github/workflows/deploy-dev.yml
vendored
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
name: Update Test Server
|
||||||
|
|
||||||
|
on:
|
||||||
|
workflow_run:
|
||||||
|
workflows: ["Docker Dev Images Build"]
|
||||||
|
types:
|
||||||
|
- completed
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
deploy:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
if: |
|
||||||
|
github.repository == 'danny-avila/LibreChat' &&
|
||||||
|
(github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success')
|
||||||
|
steps:
|
||||||
|
- name: Checkout repository
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Install SSH Key
|
||||||
|
uses: shimataro/ssh-key-action@v2
|
||||||
|
with:
|
||||||
|
key: ${{ secrets.DO_SSH_PRIVATE_KEY }}
|
||||||
|
known_hosts: ${{ secrets.DO_KNOWN_HOSTS }}
|
||||||
|
|
||||||
|
- name: Run update script on DigitalOcean Droplet
|
||||||
|
env:
|
||||||
|
DO_HOST: ${{ secrets.DO_HOST }}
|
||||||
|
DO_USER: ${{ secrets.DO_USER }}
|
||||||
|
run: |
|
||||||
|
ssh -o StrictHostKeyChecking=no ${DO_USER}@${DO_HOST} << EOF
|
||||||
|
sudo -i -u danny bash << EEOF
|
||||||
|
cd ~/LibreChat && \
|
||||||
|
git fetch origin main && \
|
||||||
|
npm run update:deployed && \
|
||||||
|
git checkout do-deploy && \
|
||||||
|
git rebase main && \
|
||||||
|
npm run start:deployed && \
|
||||||
|
echo "Update completed. Application should be running now."
|
||||||
|
EEOF
|
||||||
|
EOF
|
||||||
73
.github/workflows/eslint-ci.yml
vendored
Normal file
73
.github/workflows/eslint-ci.yml
vendored
Normal file
|
|
@ -0,0 +1,73 @@
|
||||||
|
name: ESLint Code Quality Checks
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
- dev
|
||||||
|
- release/*
|
||||||
|
paths:
|
||||||
|
- 'api/**'
|
||||||
|
- 'client/**'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
eslint_checks:
|
||||||
|
name: Run ESLint Linting
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
security-events: write
|
||||||
|
actions: read
|
||||||
|
steps:
|
||||||
|
- name: Checkout repository
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
|
- name: Set up Node.js 20.x
|
||||||
|
uses: actions/setup-node@v4
|
||||||
|
with:
|
||||||
|
node-version: 20
|
||||||
|
cache: npm
|
||||||
|
|
||||||
|
- name: Install dependencies
|
||||||
|
run: npm ci
|
||||||
|
|
||||||
|
# Run ESLint on changed files within the api/ and client/ directories.
|
||||||
|
- name: Run ESLint on changed files
|
||||||
|
env:
|
||||||
|
SARIF_ESLINT_IGNORE_SUPPRESSED: "true"
|
||||||
|
run: |
|
||||||
|
# Extract the base commit SHA from the pull_request event payload.
|
||||||
|
BASE_SHA=$(jq --raw-output .pull_request.base.sha "$GITHUB_EVENT_PATH")
|
||||||
|
echo "Base commit SHA: $BASE_SHA"
|
||||||
|
|
||||||
|
# Get changed files (only JS/TS files in api/ or client/)
|
||||||
|
CHANGED_FILES=$(git diff --name-only --diff-filter=ACMRTUXB "$BASE_SHA" HEAD | grep -E '^(api|client)/.*\.(js|jsx|ts|tsx)$' || true)
|
||||||
|
|
||||||
|
# Debug output
|
||||||
|
echo "Changed files:"
|
||||||
|
echo "$CHANGED_FILES"
|
||||||
|
|
||||||
|
# Ensure there are files to lint before running ESLint
|
||||||
|
if [[ -z "$CHANGED_FILES" ]]; then
|
||||||
|
echo "No matching files changed. Skipping ESLint."
|
||||||
|
echo "UPLOAD_SARIF=false" >> $GITHUB_ENV
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Set variable to allow SARIF upload
|
||||||
|
echo "UPLOAD_SARIF=true" >> $GITHUB_ENV
|
||||||
|
|
||||||
|
# Run ESLint
|
||||||
|
npx eslint --no-error-on-unmatched-pattern \
|
||||||
|
--config eslint.config.mjs \
|
||||||
|
--format @microsoft/eslint-formatter-sarif \
|
||||||
|
--output-file eslint-results.sarif $CHANGED_FILES || true
|
||||||
|
|
||||||
|
- name: Upload analysis results to GitHub
|
||||||
|
if: env.UPLOAD_SARIF == 'true'
|
||||||
|
uses: github/codeql-action/upload-sarif@v3
|
||||||
|
with:
|
||||||
|
sarif_file: eslint-results.sarif
|
||||||
|
wait-for-processing: true
|
||||||
34
.github/workflows/frontend-review.yml
vendored
34
.github/workflows/frontend-review.yml
vendored
|
|
@ -1,11 +1,6 @@
|
||||||
#github action to run unit tests for frontend with jest
|
|
||||||
name: Frontend Unit Tests
|
name: Frontend Unit Tests
|
||||||
|
|
||||||
on:
|
on:
|
||||||
# push:
|
|
||||||
# branches:
|
|
||||||
# - main
|
|
||||||
# - dev
|
|
||||||
# - release/*
|
|
||||||
pull_request:
|
pull_request:
|
||||||
branches:
|
branches:
|
||||||
- main
|
- main
|
||||||
|
|
@ -14,9 +9,10 @@ on:
|
||||||
paths:
|
paths:
|
||||||
- 'client/**'
|
- 'client/**'
|
||||||
- 'packages/**'
|
- 'packages/**'
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
tests_frontend:
|
tests_frontend_ubuntu:
|
||||||
name: Run frontend unit tests
|
name: Run frontend unit tests on Ubuntu
|
||||||
timeout-minutes: 60
|
timeout-minutes: 60
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
|
|
@ -36,3 +32,25 @@ jobs:
|
||||||
- name: Run unit tests
|
- name: Run unit tests
|
||||||
run: npm run test:ci --verbose
|
run: npm run test:ci --verbose
|
||||||
working-directory: client
|
working-directory: client
|
||||||
|
|
||||||
|
tests_frontend_windows:
|
||||||
|
name: Run frontend unit tests on Windows
|
||||||
|
timeout-minutes: 60
|
||||||
|
runs-on: windows-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- name: Use Node.js 20.x
|
||||||
|
uses: actions/setup-node@v4
|
||||||
|
with:
|
||||||
|
node-version: 20
|
||||||
|
cache: 'npm'
|
||||||
|
|
||||||
|
- name: Install dependencies
|
||||||
|
run: npm ci
|
||||||
|
|
||||||
|
- name: Build Client
|
||||||
|
run: npm run frontend:ci
|
||||||
|
|
||||||
|
- name: Run unit tests
|
||||||
|
run: npm run test:ci --verbose
|
||||||
|
working-directory: client
|
||||||
20
.github/workflows/generate_embeddings.yml
vendored
Normal file
20
.github/workflows/generate_embeddings.yml
vendored
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
name: 'generate_embeddings'
|
||||||
|
on:
|
||||||
|
workflow_dispatch:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
paths:
|
||||||
|
- 'docs/**'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
generate:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
- uses: supabase/embeddings-generator@v0.0.5
|
||||||
|
with:
|
||||||
|
supabase-url: ${{ secrets.SUPABASE_URL }}
|
||||||
|
supabase-service-role-key: ${{ secrets.SUPABASE_SERVICE_ROLE_KEY }}
|
||||||
|
openai-key: ${{ secrets.OPENAI_DOC_EMBEDDINGS_KEY }}
|
||||||
|
docs-root-path: 'docs'
|
||||||
33
.github/workflows/helmcharts.yml
vendored
Normal file
33
.github/workflows/helmcharts.yml
vendored
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
name: Build Helm Charts on Tag
|
||||||
|
|
||||||
|
# The workflow is triggered when a tag is pushed
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- "*"
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
release:
|
||||||
|
permissions:
|
||||||
|
contents: write
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
|
- name: Configure Git
|
||||||
|
run: |
|
||||||
|
git config user.name "$GITHUB_ACTOR"
|
||||||
|
git config user.email "$GITHUB_ACTOR@users.noreply.github.com"
|
||||||
|
|
||||||
|
- name: Install Helm
|
||||||
|
uses: azure/setup-helm@v4
|
||||||
|
env:
|
||||||
|
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
|
||||||
|
|
||||||
|
- name: Run chart-releaser
|
||||||
|
uses: helm/chart-releaser-action@v1.6.0
|
||||||
|
env:
|
||||||
|
CR_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
|
||||||
84
.github/workflows/i18n-unused-keys.yml
vendored
Normal file
84
.github/workflows/i18n-unused-keys.yml
vendored
Normal file
|
|
@ -0,0 +1,84 @@
|
||||||
|
name: Detect Unused i18next Strings
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
paths:
|
||||||
|
- "client/src/**"
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
detect-unused-i18n-keys:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
permissions:
|
||||||
|
pull-requests: write # Required for posting PR comments
|
||||||
|
steps:
|
||||||
|
- name: Checkout repository
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Find unused i18next keys
|
||||||
|
id: find-unused
|
||||||
|
run: |
|
||||||
|
echo "🔍 Scanning for unused i18next keys..."
|
||||||
|
|
||||||
|
# Define paths
|
||||||
|
I18N_FILE="client/src/locales/en/translation.json"
|
||||||
|
SOURCE_DIR="client/src"
|
||||||
|
|
||||||
|
# Check if translation file exists
|
||||||
|
if [[ ! -f "$I18N_FILE" ]]; then
|
||||||
|
echo "::error title=Missing i18n File::Translation file not found: $I18N_FILE"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Extract all keys from the JSON file
|
||||||
|
KEYS=$(jq -r 'keys[]' "$I18N_FILE")
|
||||||
|
|
||||||
|
# Track unused keys
|
||||||
|
UNUSED_KEYS=()
|
||||||
|
|
||||||
|
# Check if each key is used in the source code
|
||||||
|
for KEY in $KEYS; do
|
||||||
|
if ! grep -r --include=\*.{js,jsx,ts,tsx} -q "$KEY" "$SOURCE_DIR"; then
|
||||||
|
UNUSED_KEYS+=("$KEY")
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
# Output results
|
||||||
|
if [[ ${#UNUSED_KEYS[@]} -gt 0 ]]; then
|
||||||
|
echo "🛑 Found ${#UNUSED_KEYS[@]} unused i18n keys:"
|
||||||
|
echo "unused_keys=$(echo "${UNUSED_KEYS[@]}" | jq -R -s -c 'split(" ")')" >> $GITHUB_ENV
|
||||||
|
for KEY in "${UNUSED_KEYS[@]}"; do
|
||||||
|
echo "::warning title=Unused i18n Key::'$KEY' is defined but not used in the codebase."
|
||||||
|
done
|
||||||
|
else
|
||||||
|
echo "✅ No unused i18n keys detected!"
|
||||||
|
echo "unused_keys=[]" >> $GITHUB_ENV
|
||||||
|
fi
|
||||||
|
|
||||||
|
- name: Post verified comment on PR
|
||||||
|
if: env.unused_keys != '[]'
|
||||||
|
run: |
|
||||||
|
PR_NUMBER=$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH")
|
||||||
|
|
||||||
|
# Format the unused keys list correctly, filtering out empty entries
|
||||||
|
FILTERED_KEYS=$(echo "$unused_keys" | jq -r '.[]' | grep -v '^\s*$' | sed 's/^/- `/;s/$/`/' )
|
||||||
|
|
||||||
|
COMMENT_BODY=$(cat <<EOF
|
||||||
|
### 🚨 Unused i18next Keys Detected
|
||||||
|
|
||||||
|
The following translation keys are defined in \`translation.json\` but are **not used** in the codebase:
|
||||||
|
|
||||||
|
$FILTERED_KEYS
|
||||||
|
|
||||||
|
⚠️ **Please remove these unused keys to keep the translation files clean.**
|
||||||
|
EOF
|
||||||
|
)
|
||||||
|
|
||||||
|
gh api "repos/${{ github.repository }}/issues/${PR_NUMBER}/comments" \
|
||||||
|
-f body="$COMMENT_BODY" \
|
||||||
|
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}"
|
||||||
|
env:
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
|
- name: Fail workflow if unused keys found
|
||||||
|
if: env.unused_keys != '[]'
|
||||||
|
run: exit 1 # This makes the PR fail if unused keys exist
|
||||||
88
.github/workflows/latest-images-main.yml
vendored
88
.github/workflows/latest-images-main.yml
vendored
|
|
@ -1,88 +0,0 @@
|
||||||
name: Docker Compose Build Latest Tag (Manual Dispatch)
|
|
||||||
|
|
||||||
# The workflow is manually triggered
|
|
||||||
on:
|
|
||||||
workflow_dispatch:
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
|
|
||||||
steps:
|
|
||||||
# Check out the repository
|
|
||||||
- name: Checkout
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
|
|
||||||
# Fetch all tags and set the latest tag
|
|
||||||
- name: Fetch tags and set the latest tag
|
|
||||||
run: |
|
|
||||||
git fetch --tags
|
|
||||||
echo "LATEST_TAG=$(git describe --tags `git rev-list --tags --max-count=1`)" >> $GITHUB_ENV
|
|
||||||
|
|
||||||
# Set up Docker
|
|
||||||
- name: Set up Docker
|
|
||||||
uses: docker/setup-buildx-action@v3
|
|
||||||
|
|
||||||
# Set up QEMU
|
|
||||||
- name: Set up QEMU
|
|
||||||
uses: docker/setup-qemu-action@v3
|
|
||||||
|
|
||||||
# Log in to GitHub Container Registry
|
|
||||||
- name: Log in to GitHub Container Registry
|
|
||||||
uses: docker/login-action@v2
|
|
||||||
with:
|
|
||||||
registry: ghcr.io
|
|
||||||
username: ${{ github.actor }}
|
|
||||||
password: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
|
|
||||||
# Prepare Docker Build
|
|
||||||
- name: Build Docker images
|
|
||||||
run: cp .env.example .env
|
|
||||||
|
|
||||||
# Docker metadata for librechat-api
|
|
||||||
- name: Docker metadata for librechat-api
|
|
||||||
id: meta-librechat-api
|
|
||||||
uses: docker/metadata-action@v5
|
|
||||||
with:
|
|
||||||
images: ghcr.io/${{ github.repository_owner }}/librechat-api
|
|
||||||
tags: |
|
|
||||||
type=raw,value=${{ env.LATEST_TAG }},enable=true
|
|
||||||
type=raw,value=latest,enable=true
|
|
||||||
type=semver,pattern={{version}}
|
|
||||||
type=semver,pattern={{major}}
|
|
||||||
type=semver,pattern={{major}}.{{minor}}
|
|
||||||
|
|
||||||
# Build and push librechat-api
|
|
||||||
- name: Build and push librechat-api
|
|
||||||
uses: docker/build-push-action@v5
|
|
||||||
with:
|
|
||||||
file: Dockerfile.multi
|
|
||||||
context: .
|
|
||||||
push: true
|
|
||||||
tags: ${{ steps.meta-librechat-api.outputs.tags }}
|
|
||||||
platforms: linux/amd64,linux/arm64
|
|
||||||
target: api-build
|
|
||||||
|
|
||||||
# Docker metadata for librechat
|
|
||||||
- name: Docker metadata for librechat
|
|
||||||
id: meta-librechat
|
|
||||||
uses: docker/metadata-action@v5
|
|
||||||
with:
|
|
||||||
images: ghcr.io/${{ github.repository_owner }}/librechat
|
|
||||||
tags: |
|
|
||||||
type=raw,value=${{ env.LATEST_TAG }},enable=true
|
|
||||||
type=raw,value=latest,enable=true
|
|
||||||
type=semver,pattern={{version}}
|
|
||||||
type=semver,pattern={{major}}
|
|
||||||
type=semver,pattern={{major}}.{{minor}}
|
|
||||||
|
|
||||||
# Build and push librechat
|
|
||||||
- name: Build and push librechat
|
|
||||||
uses: docker/build-push-action@v5
|
|
||||||
with:
|
|
||||||
file: Dockerfile
|
|
||||||
context: .
|
|
||||||
push: true
|
|
||||||
tags: ${{ steps.meta-librechat.outputs.tags }}
|
|
||||||
platforms: linux/amd64,linux/arm64
|
|
||||||
target: node
|
|
||||||
72
.github/workflows/locize-i18n-sync.yml
vendored
Normal file
72
.github/workflows/locize-i18n-sync.yml
vendored
Normal file
|
|
@ -0,0 +1,72 @@
|
||||||
|
name: Sync Locize Translations & Create Translation PR
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [main]
|
||||||
|
repository_dispatch:
|
||||||
|
types: [locize/versionPublished]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
sync-translations:
|
||||||
|
name: Sync Translation Keys with Locize
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout Repository
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Set Up Node.js
|
||||||
|
uses: actions/setup-node@v4
|
||||||
|
with:
|
||||||
|
node-version: 20
|
||||||
|
|
||||||
|
- name: Install locize CLI
|
||||||
|
run: npm install -g locize-cli
|
||||||
|
|
||||||
|
# Sync translations (Push missing keys & remove deleted ones)
|
||||||
|
- name: Sync Locize with Repository
|
||||||
|
if: ${{ github.event_name == 'push' }}
|
||||||
|
run: |
|
||||||
|
cd client/src/locales
|
||||||
|
locize sync --api-key ${{ secrets.LOCIZE_API_KEY }} --project-id ${{ secrets.LOCIZE_PROJECT_ID }} --language en
|
||||||
|
|
||||||
|
# When triggered by repository_dispatch, skip sync step.
|
||||||
|
- name: Skip sync step on non-push events
|
||||||
|
if: ${{ github.event_name != 'push' }}
|
||||||
|
run: echo "Skipping sync as the event is not a push."
|
||||||
|
|
||||||
|
create-pull-request:
|
||||||
|
name: Create Translation PR on Version Published
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
needs: sync-translations
|
||||||
|
permissions:
|
||||||
|
contents: write
|
||||||
|
pull-requests: write
|
||||||
|
steps:
|
||||||
|
# 1. Check out the repository.
|
||||||
|
- name: Checkout Repository
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
# 2. Download translation files from locize.
|
||||||
|
- name: Download Translations from locize
|
||||||
|
uses: locize/download@v1
|
||||||
|
with:
|
||||||
|
project-id: ${{ secrets.LOCIZE_PROJECT_ID }}
|
||||||
|
path: "client/src/locales"
|
||||||
|
|
||||||
|
# 3. Create a Pull Request using built-in functionality.
|
||||||
|
- name: Create Pull Request
|
||||||
|
uses: peter-evans/create-pull-request@v7
|
||||||
|
with:
|
||||||
|
token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
sign-commits: true
|
||||||
|
commit-message: "🌍 i18n: Update translation.json with latest translations"
|
||||||
|
base: main
|
||||||
|
branch: i18n/locize-translation-update
|
||||||
|
reviewers: danny-avila
|
||||||
|
title: "🌍 i18n: Update translation.json with latest translations"
|
||||||
|
body: |
|
||||||
|
**Description**:
|
||||||
|
- 🎯 **Objective**: Update `translation.json` with the latest translations from locize.
|
||||||
|
- 🔍 **Details**: This PR is automatically generated upon receiving a versionPublished event with version "latest". It reflects the newest translations provided by locize.
|
||||||
|
- ✅ **Status**: Ready for review.
|
||||||
|
labels: "🌍 i18n"
|
||||||
56
.github/workflows/main-image-workflow.yml
vendored
56
.github/workflows/main-image-workflow.yml
vendored
|
|
@ -1,12 +1,20 @@
|
||||||
name: Docker Compose Build Latest Main Image Tag (Manual Dispatch)
|
name: Docker Compose Build Latest Main Image Tag (Manual Dispatch)
|
||||||
|
|
||||||
# The workflow is manually triggered
|
|
||||||
on:
|
on:
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
include:
|
||||||
|
- target: api-build
|
||||||
|
file: Dockerfile.multi
|
||||||
|
image_name: librechat-api
|
||||||
|
- target: node
|
||||||
|
file: Dockerfile
|
||||||
|
image_name: librechat
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
|
|
@ -17,12 +25,15 @@ jobs:
|
||||||
git fetch --tags
|
git fetch --tags
|
||||||
echo "LATEST_TAG=$(git describe --tags `git rev-list --tags --max-count=1`)" >> $GITHUB_ENV
|
echo "LATEST_TAG=$(git describe --tags `git rev-list --tags --max-count=1`)" >> $GITHUB_ENV
|
||||||
|
|
||||||
- name: Set up Docker
|
# Set up QEMU
|
||||||
uses: docker/setup-buildx-action@v3
|
|
||||||
|
|
||||||
- name: Set up QEMU
|
- name: Set up QEMU
|
||||||
uses: docker/setup-qemu-action@v3
|
uses: docker/setup-qemu-action@v3
|
||||||
|
|
||||||
|
# Set up Docker Buildx
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v3
|
||||||
|
|
||||||
|
# Log in to GitHub Container Registry
|
||||||
- name: Log in to GitHub Container Registry
|
- name: Log in to GitHub Container Registry
|
||||||
uses: docker/login-action@v2
|
uses: docker/login-action@v2
|
||||||
with:
|
with:
|
||||||
|
|
@ -30,26 +41,29 @@ jobs:
|
||||||
username: ${{ github.actor }}
|
username: ${{ github.actor }}
|
||||||
password: ${{ secrets.GITHUB_TOKEN }}
|
password: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
# Docker metadata for librechat
|
# Login to Docker Hub
|
||||||
- name: Docker metadata for librechat
|
- name: Login to Docker Hub
|
||||||
id: meta-librechat
|
uses: docker/login-action@v3
|
||||||
uses: docker/metadata-action@v5
|
|
||||||
with:
|
with:
|
||||||
images: ghcr.io/${{ github.repository_owner }}/librechat
|
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
||||||
tags: |
|
password: ${{ secrets.DOCKERHUB_TOKEN }}
|
||||||
type=raw,value=${{ env.LATEST_TAG }},enable=true
|
|
||||||
type=raw,value=latest,enable=true
|
|
||||||
type=semver,pattern={{version}}
|
|
||||||
type=semver,pattern={{major}}
|
|
||||||
type=semver,pattern={{major}}.{{minor}}
|
|
||||||
|
|
||||||
# Build and push librechat with only linux/amd64 platform
|
# Prepare the environment
|
||||||
- name: Build and push librechat
|
- name: Prepare environment
|
||||||
|
run: |
|
||||||
|
cp .env.example .env
|
||||||
|
|
||||||
|
# Build and push Docker images for each target
|
||||||
|
- name: Build and push Docker images
|
||||||
uses: docker/build-push-action@v5
|
uses: docker/build-push-action@v5
|
||||||
with:
|
with:
|
||||||
file: Dockerfile
|
|
||||||
context: .
|
context: .
|
||||||
|
file: ${{ matrix.file }}
|
||||||
push: true
|
push: true
|
||||||
tags: ${{ steps.meta-librechat.outputs.tags }}
|
tags: |
|
||||||
platforms: linux/amd64
|
ghcr.io/${{ github.repository_owner }}/${{ matrix.image_name }}:${{ env.LATEST_TAG }}
|
||||||
target: node
|
ghcr.io/${{ github.repository_owner }}/${{ matrix.image_name }}:latest
|
||||||
|
${{ secrets.DOCKERHUB_USERNAME }}/${{ matrix.image_name }}:${{ env.LATEST_TAG }}
|
||||||
|
${{ secrets.DOCKERHUB_USERNAME }}/${{ matrix.image_name }}:latest
|
||||||
|
platforms: linux/amd64,linux/arm64
|
||||||
|
target: ${{ matrix.target }}
|
||||||
|
|
|
||||||
27
.github/workflows/mkdocs.yaml
vendored
27
.github/workflows/mkdocs.yaml
vendored
|
|
@ -1,27 +0,0 @@
|
||||||
name: mkdocs
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- main
|
|
||||||
permissions:
|
|
||||||
contents: write
|
|
||||||
jobs:
|
|
||||||
deploy:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
- uses: actions/setup-python@v4
|
|
||||||
with:
|
|
||||||
python-version: 3.x
|
|
||||||
- run: echo "cache_id=$(date --utc '+%V')" >> $GITHUB_ENV
|
|
||||||
- uses: actions/cache@v3
|
|
||||||
with:
|
|
||||||
key: mkdocs-material-${{ env.cache_id }}
|
|
||||||
path: .cache
|
|
||||||
restore-keys: |
|
|
||||||
mkdocs-material-
|
|
||||||
- run: pip install mkdocs-material
|
|
||||||
- run: pip install mkdocs-nav-weight
|
|
||||||
- run: pip install mkdocs-publisher
|
|
||||||
- run: pip install mkdocs-exclude
|
|
||||||
- run: mkdocs gh-deploy --force
|
|
||||||
67
.github/workflows/tag-images.yml
vendored
Normal file
67
.github/workflows/tag-images.yml
vendored
Normal file
|
|
@ -0,0 +1,67 @@
|
||||||
|
name: Docker Images Build on Tag
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- '*'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
include:
|
||||||
|
- target: api-build
|
||||||
|
file: Dockerfile.multi
|
||||||
|
image_name: librechat-api
|
||||||
|
- target: node
|
||||||
|
file: Dockerfile
|
||||||
|
image_name: librechat
|
||||||
|
|
||||||
|
steps:
|
||||||
|
# Check out the repository
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
# Set up QEMU
|
||||||
|
- name: Set up QEMU
|
||||||
|
uses: docker/setup-qemu-action@v3
|
||||||
|
|
||||||
|
# Set up Docker Buildx
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v3
|
||||||
|
|
||||||
|
# Log in to GitHub Container Registry
|
||||||
|
- name: Log in to GitHub Container Registry
|
||||||
|
uses: docker/login-action@v2
|
||||||
|
with:
|
||||||
|
registry: ghcr.io
|
||||||
|
username: ${{ github.actor }}
|
||||||
|
password: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
|
# Login to Docker Hub
|
||||||
|
- name: Login to Docker Hub
|
||||||
|
uses: docker/login-action@v3
|
||||||
|
with:
|
||||||
|
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
||||||
|
password: ${{ secrets.DOCKERHUB_TOKEN }}
|
||||||
|
|
||||||
|
# Prepare the environment
|
||||||
|
- name: Prepare environment
|
||||||
|
run: |
|
||||||
|
cp .env.example .env
|
||||||
|
|
||||||
|
# Build and push Docker images for each target
|
||||||
|
- name: Build and push Docker images
|
||||||
|
uses: docker/build-push-action@v5
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
file: ${{ matrix.file }}
|
||||||
|
push: true
|
||||||
|
tags: |
|
||||||
|
ghcr.io/${{ github.repository_owner }}/${{ matrix.image_name }}:${{ github.ref_name }}
|
||||||
|
ghcr.io/${{ github.repository_owner }}/${{ matrix.image_name }}:latest
|
||||||
|
${{ secrets.DOCKERHUB_USERNAME }}/${{ matrix.image_name }}:${{ github.ref_name }}
|
||||||
|
${{ secrets.DOCKERHUB_USERNAME }}/${{ matrix.image_name }}:latest
|
||||||
|
platforms: linux/amd64,linux/arm64
|
||||||
|
target: ${{ matrix.target }}
|
||||||
147
.github/workflows/unused-packages.yml
vendored
Normal file
147
.github/workflows/unused-packages.yml
vendored
Normal file
|
|
@ -0,0 +1,147 @@
|
||||||
|
name: Detect Unused NPM Packages
|
||||||
|
|
||||||
|
on: [pull_request]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
detect-unused-packages:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
permissions:
|
||||||
|
pull-requests: write
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Use Node.js 20.x
|
||||||
|
uses: actions/setup-node@v4
|
||||||
|
with:
|
||||||
|
node-version: 20
|
||||||
|
cache: 'npm'
|
||||||
|
|
||||||
|
- name: Install depcheck
|
||||||
|
run: npm install -g depcheck
|
||||||
|
|
||||||
|
- name: Validate JSON files
|
||||||
|
run: |
|
||||||
|
for FILE in package.json client/package.json api/package.json; do
|
||||||
|
if [[ -f "$FILE" ]]; then
|
||||||
|
jq empty "$FILE" || (echo "::error title=Invalid JSON::$FILE is invalid" && exit 1)
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
- name: Extract Dependencies Used in Scripts
|
||||||
|
id: extract-used-scripts
|
||||||
|
run: |
|
||||||
|
extract_deps_from_scripts() {
|
||||||
|
local package_file=$1
|
||||||
|
if [[ -f "$package_file" ]]; then
|
||||||
|
jq -r '.scripts | to_entries[].value' "$package_file" | \
|
||||||
|
grep -oE '([a-zA-Z0-9_-]+)' | sort -u > used_scripts.txt
|
||||||
|
else
|
||||||
|
touch used_scripts.txt
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
extract_deps_from_scripts "package.json"
|
||||||
|
mv used_scripts.txt root_used_deps.txt
|
||||||
|
|
||||||
|
extract_deps_from_scripts "client/package.json"
|
||||||
|
mv used_scripts.txt client_used_deps.txt
|
||||||
|
|
||||||
|
extract_deps_from_scripts "api/package.json"
|
||||||
|
mv used_scripts.txt api_used_deps.txt
|
||||||
|
|
||||||
|
- name: Extract Dependencies Used in Source Code
|
||||||
|
id: extract-used-code
|
||||||
|
run: |
|
||||||
|
extract_deps_from_code() {
|
||||||
|
local folder=$1
|
||||||
|
local output_file=$2
|
||||||
|
if [[ -d "$folder" ]]; then
|
||||||
|
grep -rEho "require\\(['\"]([a-zA-Z0-9@/._-]+)['\"]\\)" "$folder" --include=\*.{js,ts,mjs,cjs} | \
|
||||||
|
sed -E "s/require\\(['\"]([a-zA-Z0-9@/._-]+)['\"]\\)/\1/" > "$output_file"
|
||||||
|
|
||||||
|
grep -rEho "import .* from ['\"]([a-zA-Z0-9@/._-]+)['\"]" "$folder" --include=\*.{js,ts,mjs,cjs} | \
|
||||||
|
sed -E "s/import .* from ['\"]([a-zA-Z0-9@/._-]+)['\"]/\1/" >> "$output_file"
|
||||||
|
|
||||||
|
sort -u "$output_file" -o "$output_file"
|
||||||
|
else
|
||||||
|
touch "$output_file"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
extract_deps_from_code "." root_used_code.txt
|
||||||
|
extract_deps_from_code "client" client_used_code.txt
|
||||||
|
extract_deps_from_code "api" api_used_code.txt
|
||||||
|
|
||||||
|
- name: Run depcheck for root package.json
|
||||||
|
id: check-root
|
||||||
|
run: |
|
||||||
|
if [[ -f "package.json" ]]; then
|
||||||
|
UNUSED=$(depcheck --json | jq -r '.dependencies | join("\n")' || echo "")
|
||||||
|
UNUSED=$(comm -23 <(echo "$UNUSED" | sort) <(cat root_used_deps.txt root_used_code.txt | sort) || echo "")
|
||||||
|
echo "ROOT_UNUSED<<EOF" >> $GITHUB_ENV
|
||||||
|
echo "$UNUSED" >> $GITHUB_ENV
|
||||||
|
echo "EOF" >> $GITHUB_ENV
|
||||||
|
fi
|
||||||
|
|
||||||
|
- name: Run depcheck for client/package.json
|
||||||
|
id: check-client
|
||||||
|
run: |
|
||||||
|
if [[ -f "client/package.json" ]]; then
|
||||||
|
chmod -R 755 client
|
||||||
|
cd client
|
||||||
|
UNUSED=$(depcheck --json | jq -r '.dependencies | join("\n")' || echo "")
|
||||||
|
UNUSED=$(comm -23 <(echo "$UNUSED" | sort) <(cat ../client_used_deps.txt ../client_used_code.txt | sort) || echo "")
|
||||||
|
echo "CLIENT_UNUSED<<EOF" >> $GITHUB_ENV
|
||||||
|
echo "$UNUSED" >> $GITHUB_ENV
|
||||||
|
echo "EOF" >> $GITHUB_ENV
|
||||||
|
cd ..
|
||||||
|
fi
|
||||||
|
|
||||||
|
- name: Run depcheck for api/package.json
|
||||||
|
id: check-api
|
||||||
|
run: |
|
||||||
|
if [[ -f "api/package.json" ]]; then
|
||||||
|
chmod -R 755 api
|
||||||
|
cd api
|
||||||
|
UNUSED=$(depcheck --json | jq -r '.dependencies | join("\n")' || echo "")
|
||||||
|
UNUSED=$(comm -23 <(echo "$UNUSED" | sort) <(cat ../api_used_deps.txt ../api_used_code.txt | sort) || echo "")
|
||||||
|
echo "API_UNUSED<<EOF" >> $GITHUB_ENV
|
||||||
|
echo "$UNUSED" >> $GITHUB_ENV
|
||||||
|
echo "EOF" >> $GITHUB_ENV
|
||||||
|
cd ..
|
||||||
|
fi
|
||||||
|
|
||||||
|
- name: Post comment on PR if unused dependencies are found
|
||||||
|
if: env.ROOT_UNUSED != '' || env.CLIENT_UNUSED != '' || env.API_UNUSED != ''
|
||||||
|
run: |
|
||||||
|
PR_NUMBER=$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH")
|
||||||
|
|
||||||
|
ROOT_LIST=$(echo "$ROOT_UNUSED" | awk '{print "- `" $0 "`"}')
|
||||||
|
CLIENT_LIST=$(echo "$CLIENT_UNUSED" | awk '{print "- `" $0 "`"}')
|
||||||
|
API_LIST=$(echo "$API_UNUSED" | awk '{print "- `" $0 "`"}')
|
||||||
|
|
||||||
|
COMMENT_BODY=$(cat <<EOF
|
||||||
|
### 🚨 Unused NPM Packages Detected
|
||||||
|
|
||||||
|
The following **unused dependencies** were found:
|
||||||
|
|
||||||
|
$(if [[ ! -z "$ROOT_UNUSED" ]]; then echo "#### 📂 Root \`package.json\`"; echo ""; echo "$ROOT_LIST"; echo ""; fi)
|
||||||
|
|
||||||
|
$(if [[ ! -z "$CLIENT_UNUSED" ]]; then echo "#### 📂 Client \`client/package.json\`"; echo ""; echo "$CLIENT_LIST"; echo ""; fi)
|
||||||
|
|
||||||
|
$(if [[ ! -z "$API_UNUSED" ]]; then echo "#### 📂 API \`api/package.json\`"; echo ""; echo "$API_LIST"; echo ""; fi)
|
||||||
|
|
||||||
|
⚠️ **Please remove these unused dependencies to keep your project clean.**
|
||||||
|
EOF
|
||||||
|
)
|
||||||
|
|
||||||
|
gh api "repos/${{ github.repository }}/issues/${PR_NUMBER}/comments" \
|
||||||
|
-f body="$COMMENT_BODY" \
|
||||||
|
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}"
|
||||||
|
env:
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
|
- name: Fail workflow if unused dependencies found
|
||||||
|
if: env.ROOT_UNUSED != '' || env.CLIENT_UNUSED != '' || env.API_UNUSED != ''
|
||||||
|
run: exit 1
|
||||||
14
.gitignore
vendored
14
.gitignore
vendored
|
|
@ -11,6 +11,7 @@ logs
|
||||||
pids
|
pids
|
||||||
*.pid
|
*.pid
|
||||||
*.seed
|
*.seed
|
||||||
|
.git
|
||||||
|
|
||||||
# Directory for instrumented libs generated by jscoverage/JSCover
|
# Directory for instrumented libs generated by jscoverage/JSCover
|
||||||
lib-cov
|
lib-cov
|
||||||
|
|
@ -21,6 +22,10 @@ coverage
|
||||||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
|
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
|
||||||
.grunt
|
.grunt
|
||||||
|
|
||||||
|
# translation services
|
||||||
|
config/translations/stores/*
|
||||||
|
client/src/localization/languages/*_missing_keys.json
|
||||||
|
|
||||||
# Compiled Dirs (http://nodejs.org/api/addons.html)
|
# Compiled Dirs (http://nodejs.org/api/addons.html)
|
||||||
build/
|
build/
|
||||||
dist/
|
dist/
|
||||||
|
|
@ -41,6 +46,7 @@ api/node_modules/
|
||||||
client/node_modules/
|
client/node_modules/
|
||||||
bower_components/
|
bower_components/
|
||||||
*.d.ts
|
*.d.ts
|
||||||
|
!vite-env.d.ts
|
||||||
|
|
||||||
# Floobits
|
# Floobits
|
||||||
.floo
|
.floo
|
||||||
|
|
@ -50,6 +56,7 @@ bower_components/
|
||||||
|
|
||||||
#config file
|
#config file
|
||||||
librechat.yaml
|
librechat.yaml
|
||||||
|
librechat.yml
|
||||||
|
|
||||||
# Environment
|
# Environment
|
||||||
.npmrc
|
.npmrc
|
||||||
|
|
@ -68,12 +75,16 @@ src/style - official.css
|
||||||
/playwright/.cache/
|
/playwright/.cache/
|
||||||
.DS_Store
|
.DS_Store
|
||||||
*.code-workspace
|
*.code-workspace
|
||||||
|
.idx
|
||||||
|
monospace.json
|
||||||
.idea
|
.idea
|
||||||
*.iml
|
*.iml
|
||||||
*.pem
|
*.pem
|
||||||
config.local.ts
|
config.local.ts
|
||||||
**/storageState.json
|
**/storageState.json
|
||||||
junit.xml
|
junit.xml
|
||||||
|
**/.venv/
|
||||||
|
**/venv/
|
||||||
|
|
||||||
# docker override file
|
# docker override file
|
||||||
docker-compose.override.yaml
|
docker-compose.override.yaml
|
||||||
|
|
@ -92,3 +103,6 @@ auth.json
|
||||||
|
|
||||||
# User uploads
|
# User uploads
|
||||||
uploads/
|
uploads/
|
||||||
|
|
||||||
|
# owner
|
||||||
|
release/
|
||||||
19
.prettierrc
Normal file
19
.prettierrc
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
{
|
||||||
|
"tailwindConfig": "./client/tailwind.config.cjs",
|
||||||
|
"printWidth": 100,
|
||||||
|
"tabWidth": 2,
|
||||||
|
"useTabs": false,
|
||||||
|
"semi": true,
|
||||||
|
"singleQuote": true,
|
||||||
|
"trailingComma": "all",
|
||||||
|
"arrowParens": "always",
|
||||||
|
"embeddedLanguageFormatting": "auto",
|
||||||
|
"insertPragma": false,
|
||||||
|
"proseWrap": "preserve",
|
||||||
|
"quoteProps": "as-needed",
|
||||||
|
"requirePragma": false,
|
||||||
|
"rangeStart": 0,
|
||||||
|
"endOfLine": "auto",
|
||||||
|
"jsxSingleQuote": false,
|
||||||
|
"plugins": ["prettier-plugin-tailwindcss"]
|
||||||
|
}
|
||||||
17
.vscode/launch.json
vendored
Normal file
17
.vscode/launch.json
vendored
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
{
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"type": "node",
|
||||||
|
"request": "launch",
|
||||||
|
"name": "Launch LibreChat (debug)",
|
||||||
|
"skipFiles": ["<node_internals>/**"],
|
||||||
|
"program": "${workspaceFolder}/api/server/index.js",
|
||||||
|
"env": {
|
||||||
|
"NODE_ENV": "production"
|
||||||
|
},
|
||||||
|
"console": "integratedTerminal",
|
||||||
|
"envFile": "${workspaceFolder}/.env"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
39
Dockerfile
39
Dockerfile
|
|
@ -1,21 +1,32 @@
|
||||||
# Base node image
|
# v0.7.7-rc1
|
||||||
FROM node:18-alpine AS node
|
|
||||||
|
|
||||||
COPY . /app
|
# Base node image
|
||||||
|
FROM node:20-alpine AS node
|
||||||
|
|
||||||
|
RUN apk --no-cache add curl
|
||||||
|
|
||||||
|
RUN mkdir -p /app && chown node:node /app
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
# Allow mounting of these files, which have no default
|
USER node
|
||||||
# values.
|
|
||||||
RUN touch .env
|
|
||||||
RUN npm config set fetch-retry-maxtimeout 300000
|
|
||||||
RUN apk add --no-cache g++ make python3 py3-pip
|
|
||||||
RUN npm install -g node-gyp
|
|
||||||
RUN apk --no-cache add curl && \
|
|
||||||
npm install
|
|
||||||
|
|
||||||
# React client build
|
COPY --chown=node:node . .
|
||||||
ENV NODE_OPTIONS="--max-old-space-size=2048"
|
|
||||||
RUN npm run frontend
|
RUN \
|
||||||
|
# Allow mounting of these files, which have no default
|
||||||
|
touch .env ; \
|
||||||
|
# Create directories for the volumes to inherit the correct permissions
|
||||||
|
mkdir -p /app/client/public/images /app/api/logs ; \
|
||||||
|
npm config set fetch-retry-maxtimeout 600000 ; \
|
||||||
|
npm config set fetch-retries 5 ; \
|
||||||
|
npm config set fetch-retry-mintimeout 15000 ; \
|
||||||
|
npm install --no-audit; \
|
||||||
|
# React client build
|
||||||
|
NODE_OPTIONS="--max-old-space-size=2048" npm run frontend; \
|
||||||
|
npm prune --production; \
|
||||||
|
npm cache clean --force
|
||||||
|
|
||||||
|
RUN mkdir -p /app/client/public/images /app/api/logs
|
||||||
|
|
||||||
# Node API setup
|
# Node API setup
|
||||||
EXPOSE 3080
|
EXPOSE 3080
|
||||||
|
|
|
||||||
|
|
@ -1,39 +1,56 @@
|
||||||
# Build API, Client and Data Provider
|
# Dockerfile.multi
|
||||||
FROM node:20-alpine AS base
|
# v0.7.7-rc1
|
||||||
|
|
||||||
|
# Base for all builds
|
||||||
|
FROM node:20-alpine AS base-min
|
||||||
|
WORKDIR /app
|
||||||
|
RUN apk --no-cache add curl
|
||||||
|
RUN npm config set fetch-retry-maxtimeout 600000 && \
|
||||||
|
npm config set fetch-retries 5 && \
|
||||||
|
npm config set fetch-retry-mintimeout 15000
|
||||||
|
COPY package*.json ./
|
||||||
|
COPY packages/data-provider/package*.json ./packages/data-provider/
|
||||||
|
COPY packages/mcp/package*.json ./packages/mcp/
|
||||||
|
COPY client/package*.json ./client/
|
||||||
|
COPY api/package*.json ./api/
|
||||||
|
|
||||||
|
# Install all dependencies for every build
|
||||||
|
FROM base-min AS base
|
||||||
|
WORKDIR /app
|
||||||
|
RUN npm ci
|
||||||
|
|
||||||
# Build data-provider
|
# Build data-provider
|
||||||
FROM base AS data-provider-build
|
FROM base AS data-provider-build
|
||||||
WORKDIR /app/packages/data-provider
|
WORKDIR /app/packages/data-provider
|
||||||
COPY ./packages/data-provider ./
|
COPY packages/data-provider ./
|
||||||
RUN npm install
|
|
||||||
RUN npm run build
|
RUN npm run build
|
||||||
|
|
||||||
# React client build
|
# Build mcp package
|
||||||
FROM data-provider-build AS client-build
|
FROM base AS mcp-build
|
||||||
|
WORKDIR /app/packages/mcp
|
||||||
|
COPY packages/mcp ./
|
||||||
|
COPY --from=data-provider-build /app/packages/data-provider/dist /app/packages/data-provider/dist
|
||||||
|
RUN npm run build
|
||||||
|
|
||||||
|
# Client build
|
||||||
|
FROM base AS client-build
|
||||||
WORKDIR /app/client
|
WORKDIR /app/client
|
||||||
COPY ./client/ ./
|
COPY client ./
|
||||||
# Copy data-provider to client's node_modules
|
COPY --from=data-provider-build /app/packages/data-provider/dist /app/packages/data-provider/dist
|
||||||
RUN mkdir -p /app/client/node_modules/librechat-data-provider/
|
|
||||||
RUN cp -R /app/packages/data-provider/* /app/client/node_modules/librechat-data-provider/
|
|
||||||
RUN npm install
|
|
||||||
ENV NODE_OPTIONS="--max-old-space-size=2048"
|
ENV NODE_OPTIONS="--max-old-space-size=2048"
|
||||||
RUN npm run build
|
RUN npm run build
|
||||||
|
|
||||||
# Node API setup
|
# API setup (including client dist)
|
||||||
FROM data-provider-build AS api-build
|
FROM base-min AS api-build
|
||||||
|
WORKDIR /app
|
||||||
|
# Install only production deps
|
||||||
|
RUN npm ci --omit=dev
|
||||||
|
COPY api ./api
|
||||||
|
COPY config ./config
|
||||||
|
COPY --from=data-provider-build /app/packages/data-provider/dist ./packages/data-provider/dist
|
||||||
|
COPY --from=mcp-build /app/packages/mcp/dist ./packages/mcp/dist
|
||||||
|
COPY --from=client-build /app/client/dist ./client/dist
|
||||||
WORKDIR /app/api
|
WORKDIR /app/api
|
||||||
COPY api/package*.json ./
|
|
||||||
COPY api/ ./
|
|
||||||
# Copy data-provider to API's node_modules
|
|
||||||
RUN mkdir -p /app/api/node_modules/librechat-data-provider/
|
|
||||||
RUN cp -R /app/packages/data-provider/* /app/api/node_modules/librechat-data-provider/
|
|
||||||
RUN npm install
|
|
||||||
COPY --from=client-build /app/client/dist /app/client/dist
|
|
||||||
EXPOSE 3080
|
EXPOSE 3080
|
||||||
ENV HOST=0.0.0.0
|
ENV HOST=0.0.0.0
|
||||||
CMD ["node", "server/index.js"]
|
CMD ["node", "server/index.js"]
|
||||||
|
|
||||||
# Nginx setup
|
|
||||||
FROM nginx:1.21.1-alpine AS prod-stage
|
|
||||||
COPY ./client/nginx.conf /etc/nginx/conf.d/default.conf
|
|
||||||
CMD ["nginx", "-g", "daemon off;"]
|
|
||||||
|
|
|
||||||
147
README.md
147
README.md
|
|
@ -1,6 +1,6 @@
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://librechat.ai">
|
<a href="https://librechat.ai">
|
||||||
<img src="docs/assets/LibreChat.svg" height="256">
|
<img src="client/public/assets/logo.svg" height="256">
|
||||||
</a>
|
</a>
|
||||||
<h1 align="center">
|
<h1 align="center">
|
||||||
<a href="https://librechat.ai">LibreChat</a>
|
<a href="https://librechat.ai">LibreChat</a>
|
||||||
|
|
@ -38,25 +38,87 @@
|
||||||
</a>
|
</a>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
# 📃 Features
|
<p align="center">
|
||||||
|
<a href="https://www.librechat.ai/docs/translation">
|
||||||
|
<img
|
||||||
|
src="https://img.shields.io/badge/dynamic/json.svg?style=for-the-badge&color=2096F3&label=locize&query=%24.translatedPercentage&url=https://api.locize.app/badgedata/4cb2598b-ed4d-469c-9b04-2ed531a8cb45&suffix=%+translated"
|
||||||
|
alt="Translation Progress">
|
||||||
|
</a>
|
||||||
|
</p>
|
||||||
|
|
||||||
- 🖥️ UI matching ChatGPT, including Dark mode, Streaming, and 11-2023 updates
|
|
||||||
- 💬 Multimodal Chat:
|
# ✨ Features
|
||||||
- Upload and analyze images with GPT-4 and Gemini Vision 📸
|
|
||||||
- More filetypes and Assistants API integration in Active Development 🚧
|
- 🖥️ **UI & Experience** inspired by ChatGPT with enhanced design and features
|
||||||
- 🌎 Multilingual UI:
|
|
||||||
- English, 中文, Deutsch, Español, Français, Italiano, Polski, Português Brasileiro,
|
- 🤖 **AI Model Selection**:
|
||||||
|
- Anthropic (Claude), AWS Bedrock, OpenAI, Azure OpenAI, Google, Vertex AI, OpenAI Assistants API (incl. Azure)
|
||||||
|
- [Custom Endpoints](https://www.librechat.ai/docs/quick_start/custom_endpoints): Use any OpenAI-compatible API with LibreChat, no proxy required
|
||||||
|
- Compatible with [Local & Remote AI Providers](https://www.librechat.ai/docs/configuration/librechat_yaml/ai_endpoints):
|
||||||
|
- Ollama, groq, Cohere, Mistral AI, Apple MLX, koboldcpp, together.ai,
|
||||||
|
- OpenRouter, Perplexity, ShuttleAI, Deepseek, Qwen, and more
|
||||||
|
|
||||||
|
- 🔧 **[Code Interpreter API](https://www.librechat.ai/docs/features/code_interpreter)**:
|
||||||
|
- Secure, Sandboxed Execution in Python, Node.js (JS/TS), Go, C/C++, Java, PHP, Rust, and Fortran
|
||||||
|
- Seamless File Handling: Upload, process, and download files directly
|
||||||
|
- No Privacy Concerns: Fully isolated and secure execution
|
||||||
|
|
||||||
|
- 🔦 **Agents & Tools Integration**:
|
||||||
|
- **[LibreChat Agents](https://www.librechat.ai/docs/features/agents)**:
|
||||||
|
- No-Code Custom Assistants: Build specialized, AI-driven helpers without coding
|
||||||
|
- Flexible & Extensible: Attach tools like DALL-E-3, file search, code execution, and more
|
||||||
|
- Compatible with Custom Endpoints, OpenAI, Azure, Anthropic, AWS Bedrock, and more
|
||||||
|
- [Model Context Protocol (MCP) Support](https://modelcontextprotocol.io/clients#librechat) for Tools
|
||||||
|
- Use LibreChat Agents and OpenAI Assistants with Files, Code Interpreter, Tools, and API Actions
|
||||||
|
|
||||||
|
- 🪄 **Generative UI with Code Artifacts**:
|
||||||
|
- [Code Artifacts](https://youtu.be/GfTj7O4gmd0?si=WJbdnemZpJzBrJo3) allow creation of React, HTML, and Mermaid diagrams directly in chat
|
||||||
|
|
||||||
|
- 💾 **Presets & Context Management**:
|
||||||
|
- Create, Save, & Share Custom Presets
|
||||||
|
- Switch between AI Endpoints and Presets mid-chat
|
||||||
|
- Edit, Resubmit, and Continue Messages with Conversation branching
|
||||||
|
- [Fork Messages & Conversations](https://www.librechat.ai/docs/features/fork) for Advanced Context control
|
||||||
|
|
||||||
|
- 💬 **Multimodal & File Interactions**:
|
||||||
|
- Upload and analyze images with Claude 3, GPT-4o, o1, Llama-Vision, and Gemini 📸
|
||||||
|
- Chat with Files using Custom Endpoints, OpenAI, Azure, Anthropic, AWS Bedrock, & Google 🗃️
|
||||||
|
|
||||||
|
- 🌎 **Multilingual UI**:
|
||||||
|
- English, 中文, Deutsch, Español, Français, Italiano, Polski, Português Brasileiro
|
||||||
- Русский, 日本語, Svenska, 한국어, Tiếng Việt, 繁體中文, العربية, Türkçe, Nederlands, עברית
|
- Русский, 日本語, Svenska, 한국어, Tiếng Việt, 繁體中文, العربية, Türkçe, Nederlands, עברית
|
||||||
- 🤖 AI model selection: OpenAI API, Azure, BingAI, ChatGPT, Google Vertex AI, Anthropic (Claude), Plugins
|
|
||||||
- 💾 Create, Save, & Share Custom Presets
|
|
||||||
- 🔄 Edit, Resubmit, and Continue messages with conversation branching
|
|
||||||
- 📤 Export conversations as screenshots, markdown, text, json.
|
|
||||||
- 🔍 Search all messages/conversations
|
|
||||||
- 🔌 Plugins, including web access, image generation with DALL-E-3 and more
|
|
||||||
- 👥 Multi-User, Secure Authentication with Moderation and Token spend tools
|
|
||||||
- ⚙️ Configure Proxy, Reverse Proxy, Docker, many Deployment options, and completely Open-Source
|
|
||||||
|
|
||||||
[For a thorough review of our features, see our docs here](https://docs.librechat.ai/features/plugins/introduction.html) 📚
|
- 🧠 **Reasoning UI**:
|
||||||
|
- Dynamic Reasoning UI for Chain-of-Thought/Reasoning AI models like DeepSeek-R1
|
||||||
|
|
||||||
|
- 🎨 **Customizable Interface**:
|
||||||
|
- Customizable Dropdown & Interface that adapts to both power users and newcomers
|
||||||
|
|
||||||
|
- 🗣️ **Speech & Audio**:
|
||||||
|
- Chat hands-free with Speech-to-Text and Text-to-Speech
|
||||||
|
- Automatically send and play Audio
|
||||||
|
- Supports OpenAI, Azure OpenAI, and Elevenlabs
|
||||||
|
|
||||||
|
- 📥 **Import & Export Conversations**:
|
||||||
|
- Import Conversations from LibreChat, ChatGPT, Chatbot UI
|
||||||
|
- Export conversations as screenshots, markdown, text, json
|
||||||
|
|
||||||
|
- 🔍 **Search & Discovery**:
|
||||||
|
- Search all messages/conversations
|
||||||
|
|
||||||
|
- 👥 **Multi-User & Secure Access**:
|
||||||
|
- Multi-User, Secure Authentication with OAuth2, LDAP, & Email Login Support
|
||||||
|
- Built-in Moderation, and Token spend tools
|
||||||
|
|
||||||
|
- ⚙️ **Configuration & Deployment**:
|
||||||
|
- Configure Proxy, Reverse Proxy, Docker, & many Deployment options
|
||||||
|
- Use completely local or deploy on the cloud
|
||||||
|
|
||||||
|
- 📖 **Open-Source & Community**:
|
||||||
|
- Completely Open-Source & Built in Public
|
||||||
|
- Community-driven development, support, and feedback
|
||||||
|
|
||||||
|
[For a thorough review of our features, see our docs here](https://docs.librechat.ai/) 📚
|
||||||
|
|
||||||
## 🪶 All-In-One AI Conversations with LibreChat
|
## 🪶 All-In-One AI Conversations with LibreChat
|
||||||
|
|
||||||
|
|
@ -64,37 +126,50 @@ LibreChat brings together the future of assistant AIs with the revolutionary tec
|
||||||
|
|
||||||
With LibreChat, you no longer need to opt for ChatGPT Plus and can instead use free or pay-per-call APIs. We welcome contributions, cloning, and forking to enhance the capabilities of this advanced chatbot platform.
|
With LibreChat, you no longer need to opt for ChatGPT Plus and can instead use free or pay-per-call APIs. We welcome contributions, cloning, and forking to enhance the capabilities of this advanced chatbot platform.
|
||||||
|
|
||||||
<!-- https://github.com/danny-avila/LibreChat/assets/110412045/c1eb0c0f-41f6-4335-b982-84b278b53d59 -->
|
[](https://www.youtube.com/watch?v=ilfwGQtJNlI)
|
||||||
|
|
||||||
[](https://youtu.be/pNIOs1ovsXw)
|
|
||||||
Click on the thumbnail to open the video☝️
|
Click on the thumbnail to open the video☝️
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 📚 Documentation
|
## 🌐 Resources
|
||||||
|
|
||||||
For more information on how to use our advanced features, install and configure our software, and access our guidelines and tutorials, please check out our documentation at [docs.librechat.ai](https://docs.librechat.ai)
|
**GitHub Repo:**
|
||||||
|
- **RAG API:** [github.com/danny-avila/rag_api](https://github.com/danny-avila/rag_api)
|
||||||
|
- **Website:** [github.com/LibreChat-AI/librechat.ai](https://github.com/LibreChat-AI/librechat.ai)
|
||||||
|
|
||||||
|
**Other:**
|
||||||
|
- **Website:** [librechat.ai](https://librechat.ai)
|
||||||
|
- **Documentation:** [docs.librechat.ai](https://docs.librechat.ai)
|
||||||
|
- **Blog:** [blog.librechat.ai](https://blog.librechat.ai)
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 📝 Changelog
|
## 📝 Changelog
|
||||||
|
|
||||||
Keep up with the latest updates by visiting the releases page - [Releases](https://github.com/danny-avila/LibreChat/releases)
|
Keep up with the latest updates by visiting the releases page and notes:
|
||||||
|
- [Releases](https://github.com/danny-avila/LibreChat/releases)
|
||||||
|
- [Changelog](https://www.librechat.ai/changelog)
|
||||||
|
|
||||||
**⚠️ [Breaking Changes](docs/general_info/breaking_changes.md)**
|
**⚠️ Please consult the [changelog](https://www.librechat.ai/changelog) for breaking changes before updating.**
|
||||||
Please consult the breaking changes before updating.
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## ⭐ Star History
|
## ⭐ Star History
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://trendshift.io/repositories/4685" target="_blank"><img src="https://trendshift.io/api/badge/repositories/4685" alt="danny-avila%2FLibreChat | Trendshift" style="width: 250px; height: 55px;" width="250" height="55"/></a>
|
<a href="https://star-history.com/#danny-avila/LibreChat&Date">
|
||||||
|
<img alt="Star History Chart" src="https://api.star-history.com/svg?repos=danny-avila/LibreChat&type=Date&theme=dark" onerror="this.src='https://api.star-history.com/svg?repos=danny-avila/LibreChat&type=Date'" />
|
||||||
|
</a>
|
||||||
|
</p>
|
||||||
|
<p align="center">
|
||||||
|
<a href="https://trendshift.io/repositories/4685" target="_blank" style="padding: 10px;">
|
||||||
|
<img src="https://trendshift.io/api/badge/repositories/4685" alt="danny-avila%2FLibreChat | Trendshift" style="width: 250px; height: 55px;" width="250" height="55"/>
|
||||||
|
</a>
|
||||||
|
<a href="https://runacap.com/ross-index/q1-24/" target="_blank" rel="noopener" style="margin-left: 20px;">
|
||||||
|
<img style="width: 260px; height: 56px" src="https://runacap.com/wp-content/uploads/2024/04/ROSS_badge_white_Q1_2024.svg" alt="ROSS Index - Fastest Growing Open-Source Startups in Q1 2024 | Runa Capital" width="260" height="56"/>
|
||||||
|
</a>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<a href="https://star-history.com/#danny-avila/LibreChat&Date">
|
|
||||||
<img alt="Star History Chart" src="https://api.star-history.com/svg?repos=danny-avila/LibreChat&type=Date&theme=dark" onerror="this.src='https://api.star-history.com/svg?repos=danny-avila/LibreChat&type=Date'" />
|
|
||||||
</a>
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
@ -104,6 +179,8 @@ Contributions, suggestions, bug reports and fixes are welcome!
|
||||||
|
|
||||||
For new features, components, or extensions, please open an issue and discuss before sending a PR.
|
For new features, components, or extensions, please open an issue and discuss before sending a PR.
|
||||||
|
|
||||||
|
If you'd like to help translate LibreChat into your language, we'd love your contribution! Improving our translations not only makes LibreChat more accessible to users around the world but also enhances the overall user experience. Please check out our [Translation Guide](https://www.librechat.ai/docs/translation).
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 💖 This project exists in its current state thanks to all the people who contribute
|
## 💖 This project exists in its current state thanks to all the people who contribute
|
||||||
|
|
@ -111,3 +188,15 @@ For new features, components, or extensions, please open an issue and discuss be
|
||||||
<a href="https://github.com/danny-avila/LibreChat/graphs/contributors">
|
<a href="https://github.com/danny-avila/LibreChat/graphs/contributors">
|
||||||
<img src="https://contrib.rocks/image?repo=danny-avila/LibreChat" />
|
<img src="https://contrib.rocks/image?repo=danny-avila/LibreChat" />
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 🎉 Special Thanks
|
||||||
|
|
||||||
|
We thank [Locize](https://locize.com) for their translation management tools that support multiple languages in LibreChat.
|
||||||
|
|
||||||
|
<p align="center">
|
||||||
|
<a href="https://locize.com" target="_blank" rel="noopener noreferrer">
|
||||||
|
<img src="https://locize.com/img/locize_color.svg" alt="Locize Logo" height="50">
|
||||||
|
</a>
|
||||||
|
</p>
|
||||||
|
|
|
||||||
|
|
@ -1,114 +0,0 @@
|
||||||
require('dotenv').config();
|
|
||||||
const { KeyvFile } = require('keyv-file');
|
|
||||||
const { getUserKey, checkUserKeyExpiry } = require('~/server/services/UserService');
|
|
||||||
const { logger } = require('~/config');
|
|
||||||
|
|
||||||
const askBing = async ({
|
|
||||||
text,
|
|
||||||
parentMessageId,
|
|
||||||
conversationId,
|
|
||||||
jailbreak,
|
|
||||||
jailbreakConversationId,
|
|
||||||
context,
|
|
||||||
systemMessage,
|
|
||||||
conversationSignature,
|
|
||||||
clientId,
|
|
||||||
invocationId,
|
|
||||||
toneStyle,
|
|
||||||
key: expiresAt,
|
|
||||||
onProgress,
|
|
||||||
userId,
|
|
||||||
}) => {
|
|
||||||
const isUserProvided = process.env.BINGAI_TOKEN === 'user_provided';
|
|
||||||
|
|
||||||
let key = null;
|
|
||||||
if (expiresAt && isUserProvided) {
|
|
||||||
checkUserKeyExpiry(
|
|
||||||
expiresAt,
|
|
||||||
'Your BingAI Cookies have expired. Please provide your cookies again.',
|
|
||||||
);
|
|
||||||
key = await getUserKey({ userId, name: 'bingAI' });
|
|
||||||
}
|
|
||||||
|
|
||||||
const { BingAIClient } = await import('nodejs-gpt');
|
|
||||||
const store = {
|
|
||||||
store: new KeyvFile({ filename: './data/cache.json' }),
|
|
||||||
};
|
|
||||||
|
|
||||||
const bingAIClient = new BingAIClient({
|
|
||||||
// "_U" cookie from bing.com
|
|
||||||
// userToken:
|
|
||||||
// isUserProvided ? key : process.env.BINGAI_TOKEN ?? null,
|
|
||||||
// If the above doesn't work, provide all your cookies as a string instead
|
|
||||||
cookies: isUserProvided ? key : process.env.BINGAI_TOKEN ?? null,
|
|
||||||
debug: false,
|
|
||||||
cache: store,
|
|
||||||
host: process.env.BINGAI_HOST || null,
|
|
||||||
proxy: process.env.PROXY || null,
|
|
||||||
});
|
|
||||||
|
|
||||||
let options = {};
|
|
||||||
|
|
||||||
if (jailbreakConversationId == 'false') {
|
|
||||||
jailbreakConversationId = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (jailbreak) {
|
|
||||||
options = {
|
|
||||||
jailbreakConversationId: jailbreakConversationId || jailbreak,
|
|
||||||
context,
|
|
||||||
systemMessage,
|
|
||||||
parentMessageId,
|
|
||||||
toneStyle,
|
|
||||||
onProgress,
|
|
||||||
clientOptions: {
|
|
||||||
features: {
|
|
||||||
genImage: {
|
|
||||||
server: {
|
|
||||||
enable: true,
|
|
||||||
type: 'markdown_list',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
options = {
|
|
||||||
conversationId,
|
|
||||||
context,
|
|
||||||
systemMessage,
|
|
||||||
parentMessageId,
|
|
||||||
toneStyle,
|
|
||||||
onProgress,
|
|
||||||
clientOptions: {
|
|
||||||
features: {
|
|
||||||
genImage: {
|
|
||||||
server: {
|
|
||||||
enable: true,
|
|
||||||
type: 'markdown_list',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
// don't give those parameters for new conversation
|
|
||||||
// for new conversation, conversationSignature always is null
|
|
||||||
if (conversationSignature) {
|
|
||||||
options.encryptedConversationSignature = conversationSignature;
|
|
||||||
options.clientId = clientId;
|
|
||||||
options.invocationId = invocationId;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.debug('bing options', options);
|
|
||||||
|
|
||||||
const res = await bingAIClient.sendMessage(text, options);
|
|
||||||
|
|
||||||
return res;
|
|
||||||
|
|
||||||
// for reference:
|
|
||||||
// https://github.com/waylaidwanderer/node-chatgpt-api/blob/main/demos/use-bing-client.js
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = { askBing };
|
|
||||||
|
|
@ -1,60 +0,0 @@
|
||||||
require('dotenv').config();
|
|
||||||
const { KeyvFile } = require('keyv-file');
|
|
||||||
const { Constants } = require('librechat-data-provider');
|
|
||||||
const { getUserKey, checkUserKeyExpiry } = require('../server/services/UserService');
|
|
||||||
|
|
||||||
const browserClient = async ({
|
|
||||||
text,
|
|
||||||
parentMessageId,
|
|
||||||
conversationId,
|
|
||||||
model,
|
|
||||||
key: expiresAt,
|
|
||||||
onProgress,
|
|
||||||
onEventMessage,
|
|
||||||
abortController,
|
|
||||||
userId,
|
|
||||||
}) => {
|
|
||||||
const isUserProvided = process.env.CHATGPT_TOKEN === 'user_provided';
|
|
||||||
|
|
||||||
let key = null;
|
|
||||||
if (expiresAt && isUserProvided) {
|
|
||||||
checkUserKeyExpiry(
|
|
||||||
expiresAt,
|
|
||||||
'Your ChatGPT Access Token has expired. Please provide your token again.',
|
|
||||||
);
|
|
||||||
key = await getUserKey({ userId, name: 'chatGPTBrowser' });
|
|
||||||
}
|
|
||||||
|
|
||||||
const { ChatGPTBrowserClient } = await import('nodejs-gpt');
|
|
||||||
const store = {
|
|
||||||
store: new KeyvFile({ filename: './data/cache.json' }),
|
|
||||||
};
|
|
||||||
|
|
||||||
const clientOptions = {
|
|
||||||
// Warning: This will expose your access token to a third party. Consider the risks before using this.
|
|
||||||
reverseProxyUrl:
|
|
||||||
process.env.CHATGPT_REVERSE_PROXY ?? 'https://ai.fakeopen.com/api/conversation',
|
|
||||||
// Access token from https://chat.openai.com/api/auth/session
|
|
||||||
accessToken: isUserProvided ? key : process.env.CHATGPT_TOKEN ?? null,
|
|
||||||
model: model,
|
|
||||||
debug: false,
|
|
||||||
proxy: process.env.PROXY ?? null,
|
|
||||||
user: userId,
|
|
||||||
};
|
|
||||||
|
|
||||||
const client = new ChatGPTBrowserClient(clientOptions, store);
|
|
||||||
let options = { onProgress, onEventMessage, abortController };
|
|
||||||
|
|
||||||
if (!!parentMessageId && !!conversationId) {
|
|
||||||
options = { ...options, parentMessageId, conversationId };
|
|
||||||
}
|
|
||||||
|
|
||||||
if (parentMessageId === Constants.NO_PARENT) {
|
|
||||||
delete options.conversationId;
|
|
||||||
}
|
|
||||||
|
|
||||||
const res = await client.sendMessage(text, options);
|
|
||||||
return res;
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = { browserClient };
|
|
||||||
|
|
@ -1,28 +1,39 @@
|
||||||
const Anthropic = require('@anthropic-ai/sdk');
|
const Anthropic = require('@anthropic-ai/sdk');
|
||||||
const { encoding_for_model: encodingForModel, get_encoding: getEncoding } = require('tiktoken');
|
const { HttpsProxyAgent } = require('https-proxy-agent');
|
||||||
const {
|
const {
|
||||||
getResponseSender,
|
Constants,
|
||||||
EModelEndpoint,
|
EModelEndpoint,
|
||||||
|
anthropicSettings,
|
||||||
|
getResponseSender,
|
||||||
validateVisionModel,
|
validateVisionModel,
|
||||||
} = require('librechat-data-provider');
|
} = require('librechat-data-provider');
|
||||||
const { encodeAndFormat } = require('~/server/services/Files/images/encode');
|
const { encodeAndFormat } = require('~/server/services/Files/images/encode');
|
||||||
const spendTokens = require('~/models/spendTokens');
|
const {
|
||||||
const { getModelMaxTokens } = require('~/utils');
|
truncateText,
|
||||||
const { formatMessage } = require('./prompts');
|
formatMessage,
|
||||||
const { getFiles } = require('~/models/File');
|
addCacheControl,
|
||||||
|
titleFunctionPrompt,
|
||||||
|
parseParamFromPrompt,
|
||||||
|
createContextHandlers,
|
||||||
|
} = require('./prompts');
|
||||||
|
const { getModelMaxTokens, getModelMaxOutputTokens, matchModelName } = require('~/utils');
|
||||||
|
const { spendTokens, spendStructuredTokens } = require('~/models/spendTokens');
|
||||||
|
const Tokenizer = require('~/server/services/Tokenizer');
|
||||||
|
const { sleep } = require('~/server/utils');
|
||||||
const BaseClient = require('./BaseClient');
|
const BaseClient = require('./BaseClient');
|
||||||
const { logger } = require('~/config');
|
const { logger } = require('~/config');
|
||||||
|
|
||||||
const HUMAN_PROMPT = '\n\nHuman:';
|
const HUMAN_PROMPT = '\n\nHuman:';
|
||||||
const AI_PROMPT = '\n\nAssistant:';
|
const AI_PROMPT = '\n\nAssistant:';
|
||||||
|
|
||||||
const tokenizersCache = {};
|
|
||||||
|
|
||||||
/** Helper function to introduce a delay before retrying */
|
/** Helper function to introduce a delay before retrying */
|
||||||
function delayBeforeRetry(attempts, baseDelay = 1000) {
|
function delayBeforeRetry(attempts, baseDelay = 1000) {
|
||||||
return new Promise((resolve) => setTimeout(resolve, baseDelay * attempts));
|
return new Promise((resolve) => setTimeout(resolve, baseDelay * attempts));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const tokenEventTypes = new Set(['message_start', 'message_delta']);
|
||||||
|
const { legacy } = anthropicSettings;
|
||||||
|
|
||||||
class AnthropicClient extends BaseClient {
|
class AnthropicClient extends BaseClient {
|
||||||
constructor(apiKey, options = {}) {
|
constructor(apiKey, options = {}) {
|
||||||
super(apiKey, options);
|
super(apiKey, options);
|
||||||
|
|
@ -33,6 +44,30 @@ class AnthropicClient extends BaseClient {
|
||||||
? options.contextStrategy.toLowerCase()
|
? options.contextStrategy.toLowerCase()
|
||||||
: 'discard';
|
: 'discard';
|
||||||
this.setOptions(options);
|
this.setOptions(options);
|
||||||
|
/** @type {string | undefined} */
|
||||||
|
this.systemMessage;
|
||||||
|
/** @type {AnthropicMessageStartEvent| undefined} */
|
||||||
|
this.message_start;
|
||||||
|
/** @type {AnthropicMessageDeltaEvent| undefined} */
|
||||||
|
this.message_delta;
|
||||||
|
/** Whether the model is part of the Claude 3 Family
|
||||||
|
* @type {boolean} */
|
||||||
|
this.isClaude3;
|
||||||
|
/** Whether to use Messages API or Completions API
|
||||||
|
* @type {boolean} */
|
||||||
|
this.useMessages;
|
||||||
|
/** Whether or not the model is limited to the legacy amount of output tokens
|
||||||
|
* @type {boolean} */
|
||||||
|
this.isLegacyOutput;
|
||||||
|
/** Whether or not the model supports Prompt Caching
|
||||||
|
* @type {boolean} */
|
||||||
|
this.supportsCacheControl;
|
||||||
|
/** The key for the usage object's input tokens
|
||||||
|
* @type {string} */
|
||||||
|
this.inputTokensKey = 'input_tokens';
|
||||||
|
/** The key for the usage object's output tokens
|
||||||
|
* @type {string} */
|
||||||
|
this.outputTokensKey = 'output_tokens';
|
||||||
}
|
}
|
||||||
|
|
||||||
setOptions(options) {
|
setOptions(options) {
|
||||||
|
|
@ -52,26 +87,45 @@ class AnthropicClient extends BaseClient {
|
||||||
this.options = options;
|
this.options = options;
|
||||||
}
|
}
|
||||||
|
|
||||||
const modelOptions = this.options.modelOptions || {};
|
this.modelOptions = Object.assign(
|
||||||
this.modelOptions = {
|
{
|
||||||
...modelOptions,
|
model: anthropicSettings.model.default,
|
||||||
// set some good defaults (check for undefined in some cases because they may be 0)
|
},
|
||||||
model: modelOptions.model || 'claude-1',
|
this.modelOptions,
|
||||||
temperature: typeof modelOptions.temperature === 'undefined' ? 1 : modelOptions.temperature, // 0 - 1, 1 is default
|
this.options.modelOptions,
|
||||||
topP: typeof modelOptions.topP === 'undefined' ? 0.7 : modelOptions.topP, // 0 - 1, default: 0.7
|
);
|
||||||
topK: typeof modelOptions.topK === 'undefined' ? 40 : modelOptions.topK, // 1-40, default: 40
|
|
||||||
stop: modelOptions.stop, // no stop method for now
|
const modelMatch = matchModelName(this.modelOptions.model, EModelEndpoint.anthropic);
|
||||||
};
|
this.isClaude3 = modelMatch.includes('claude-3');
|
||||||
|
this.isLegacyOutput = !modelMatch.includes('claude-3-5-sonnet');
|
||||||
|
this.supportsCacheControl =
|
||||||
|
this.options.promptCache && this.checkPromptCacheSupport(modelMatch);
|
||||||
|
|
||||||
|
if (
|
||||||
|
this.isLegacyOutput &&
|
||||||
|
this.modelOptions.maxOutputTokens &&
|
||||||
|
this.modelOptions.maxOutputTokens > legacy.maxOutputTokens.default
|
||||||
|
) {
|
||||||
|
this.modelOptions.maxOutputTokens = legacy.maxOutputTokens.default;
|
||||||
|
}
|
||||||
|
|
||||||
this.isClaude3 = this.modelOptions.model.includes('claude-3');
|
|
||||||
this.useMessages = this.isClaude3 || !!this.options.attachments;
|
this.useMessages = this.isClaude3 || !!this.options.attachments;
|
||||||
|
|
||||||
this.defaultVisionModel = this.options.visionModel ?? 'claude-3-sonnet-20240229';
|
this.defaultVisionModel = this.options.visionModel ?? 'claude-3-sonnet-20240229';
|
||||||
this.checkVisionRequest(this.options.attachments);
|
this.options.attachments?.then((attachments) => this.checkVisionRequest(attachments));
|
||||||
|
|
||||||
this.maxContextTokens =
|
this.maxContextTokens =
|
||||||
getModelMaxTokens(this.modelOptions.model, EModelEndpoint.anthropic) ?? 100000;
|
this.options.maxContextTokens ??
|
||||||
this.maxResponseTokens = this.modelOptions.maxOutputTokens || 1500;
|
getModelMaxTokens(this.modelOptions.model, EModelEndpoint.anthropic) ??
|
||||||
|
100000;
|
||||||
|
this.maxResponseTokens =
|
||||||
|
this.modelOptions.maxOutputTokens ??
|
||||||
|
getModelMaxOutputTokens(
|
||||||
|
this.modelOptions.model,
|
||||||
|
this.options.endpointType ?? this.options.endpoint,
|
||||||
|
this.options.endpointTokenConfig,
|
||||||
|
) ??
|
||||||
|
1500;
|
||||||
this.maxPromptTokens =
|
this.maxPromptTokens =
|
||||||
this.options.maxPromptTokens || this.maxContextTokens - this.maxResponseTokens;
|
this.options.maxPromptTokens || this.maxContextTokens - this.maxResponseTokens;
|
||||||
|
|
||||||
|
|
@ -93,38 +147,98 @@ class AnthropicClient extends BaseClient {
|
||||||
|
|
||||||
this.startToken = '||>';
|
this.startToken = '||>';
|
||||||
this.endToken = '';
|
this.endToken = '';
|
||||||
this.gptEncoder = this.constructor.getTokenizer('cl100k_base');
|
|
||||||
|
|
||||||
if (!this.modelOptions.stop) {
|
|
||||||
const stopTokens = [this.startToken];
|
|
||||||
if (this.endToken && this.endToken !== this.startToken) {
|
|
||||||
stopTokens.push(this.endToken);
|
|
||||||
}
|
|
||||||
stopTokens.push(`${this.userLabel}`);
|
|
||||||
stopTokens.push('<|diff_marker|>');
|
|
||||||
|
|
||||||
this.modelOptions.stop = stopTokens;
|
|
||||||
}
|
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
getClient() {
|
/**
|
||||||
|
* Get the initialized Anthropic client.
|
||||||
|
* @param {Partial<Anthropic.ClientOptions>} requestOptions - The options for the client.
|
||||||
|
* @returns {Anthropic} The Anthropic client instance.
|
||||||
|
*/
|
||||||
|
getClient(requestOptions) {
|
||||||
|
/** @type {Anthropic.ClientOptions} */
|
||||||
const options = {
|
const options = {
|
||||||
|
fetch: this.fetch,
|
||||||
apiKey: this.apiKey,
|
apiKey: this.apiKey,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (this.options.proxy) {
|
||||||
|
options.httpAgent = new HttpsProxyAgent(this.options.proxy);
|
||||||
|
}
|
||||||
|
|
||||||
if (this.options.reverseProxyUrl) {
|
if (this.options.reverseProxyUrl) {
|
||||||
options.baseURL = this.options.reverseProxyUrl;
|
options.baseURL = this.options.reverseProxyUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
this.supportsCacheControl &&
|
||||||
|
requestOptions?.model &&
|
||||||
|
requestOptions.model.includes('claude-3-5-sonnet')
|
||||||
|
) {
|
||||||
|
options.defaultHeaders = {
|
||||||
|
'anthropic-beta': 'max-tokens-3-5-sonnet-2024-07-15,prompt-caching-2024-07-31',
|
||||||
|
};
|
||||||
|
} else if (this.supportsCacheControl) {
|
||||||
|
options.defaultHeaders = {
|
||||||
|
'anthropic-beta': 'prompt-caching-2024-07-31',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
return new Anthropic(options);
|
return new Anthropic(options);
|
||||||
}
|
}
|
||||||
|
|
||||||
getTokenCountForResponse(response) {
|
/**
|
||||||
|
* Get stream usage as returned by this client's API response.
|
||||||
|
* @returns {AnthropicStreamUsage} The stream usage object.
|
||||||
|
*/
|
||||||
|
getStreamUsage() {
|
||||||
|
const inputUsage = this.message_start?.message?.usage ?? {};
|
||||||
|
const outputUsage = this.message_delta?.usage ?? {};
|
||||||
|
return Object.assign({}, inputUsage, outputUsage);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the correct token count for the current user message based on the token count map and API usage.
|
||||||
|
* Edge case: If the calculation results in a negative value, it returns the original estimate.
|
||||||
|
* If revisiting a conversation with a chat history entirely composed of token estimates,
|
||||||
|
* the cumulative token count going forward should become more accurate as the conversation progresses.
|
||||||
|
* @param {Object} params - The parameters for the calculation.
|
||||||
|
* @param {Record<string, number>} params.tokenCountMap - A map of message IDs to their token counts.
|
||||||
|
* @param {string} params.currentMessageId - The ID of the current message to calculate.
|
||||||
|
* @param {AnthropicStreamUsage} params.usage - The usage object returned by the API.
|
||||||
|
* @returns {number} The correct token count for the current user message.
|
||||||
|
*/
|
||||||
|
calculateCurrentTokenCount({ tokenCountMap, currentMessageId, usage }) {
|
||||||
|
const originalEstimate = tokenCountMap[currentMessageId] || 0;
|
||||||
|
|
||||||
|
if (!usage || typeof usage.input_tokens !== 'number') {
|
||||||
|
return originalEstimate;
|
||||||
|
}
|
||||||
|
|
||||||
|
tokenCountMap[currentMessageId] = 0;
|
||||||
|
const totalTokensFromMap = Object.values(tokenCountMap).reduce((sum, count) => {
|
||||||
|
const numCount = Number(count);
|
||||||
|
return sum + (isNaN(numCount) ? 0 : numCount);
|
||||||
|
}, 0);
|
||||||
|
const totalInputTokens =
|
||||||
|
(usage.input_tokens ?? 0) +
|
||||||
|
(usage.cache_creation_input_tokens ?? 0) +
|
||||||
|
(usage.cache_read_input_tokens ?? 0);
|
||||||
|
|
||||||
|
const currentMessageTokens = totalInputTokens - totalTokensFromMap;
|
||||||
|
return currentMessageTokens > 0 ? currentMessageTokens : originalEstimate;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get Token Count for LibreChat Message
|
||||||
|
* @param {TMessage} responseMessage
|
||||||
|
* @returns {number}
|
||||||
|
*/
|
||||||
|
getTokenCountForResponse(responseMessage) {
|
||||||
return this.getTokenCountForMessage({
|
return this.getTokenCountForMessage({
|
||||||
role: 'assistant',
|
role: 'assistant',
|
||||||
content: response.text,
|
content: responseMessage.text,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -134,14 +248,19 @@ class AnthropicClient extends BaseClient {
|
||||||
* - Sets `this.modelOptions.model` to `gpt-4-vision-preview` if the request is a vision request.
|
* - Sets `this.modelOptions.model` to `gpt-4-vision-preview` if the request is a vision request.
|
||||||
* - Sets `this.isVisionModel` to `true` if vision request.
|
* - Sets `this.isVisionModel` to `true` if vision request.
|
||||||
* - Deletes `this.modelOptions.stop` if vision request.
|
* - Deletes `this.modelOptions.stop` if vision request.
|
||||||
* @param {Array<Promise<MongoFile[]> | MongoFile[]> | Record<string, MongoFile[]>} attachments
|
* @param {MongoFile[]} attachments
|
||||||
*/
|
*/
|
||||||
checkVisionRequest(attachments) {
|
checkVisionRequest(attachments) {
|
||||||
const availableModels = this.options.modelsConfig?.[EModelEndpoint.anthropic];
|
const availableModels = this.options.modelsConfig?.[EModelEndpoint.anthropic];
|
||||||
this.isVisionModel = validateVisionModel({ model: this.modelOptions.model, availableModels });
|
this.isVisionModel = validateVisionModel({ model: this.modelOptions.model, availableModels });
|
||||||
|
|
||||||
const visionModelAvailable = availableModels?.includes(this.defaultVisionModel);
|
const visionModelAvailable = availableModels?.includes(this.defaultVisionModel);
|
||||||
if (attachments && visionModelAvailable && !this.isVisionModel) {
|
if (
|
||||||
|
attachments &&
|
||||||
|
attachments.some((file) => file?.type && file?.type?.includes('image')) &&
|
||||||
|
visionModelAvailable &&
|
||||||
|
!this.isVisionModel
|
||||||
|
) {
|
||||||
this.modelOptions.model = this.defaultVisionModel;
|
this.modelOptions.model = this.defaultVisionModel;
|
||||||
this.isVisionModel = true;
|
this.isVisionModel = true;
|
||||||
}
|
}
|
||||||
|
|
@ -168,72 +287,54 @@ class AnthropicClient extends BaseClient {
|
||||||
attachments,
|
attachments,
|
||||||
EModelEndpoint.anthropic,
|
EModelEndpoint.anthropic,
|
||||||
);
|
);
|
||||||
message.image_urls = image_urls;
|
message.image_urls = image_urls.length ? image_urls : undefined;
|
||||||
return files;
|
return files;
|
||||||
}
|
}
|
||||||
|
|
||||||
async recordTokenUsage({ promptTokens, completionTokens }) {
|
/**
|
||||||
logger.debug('[AnthropicClient] recordTokenUsage:', { promptTokens, completionTokens });
|
* @param {object} params
|
||||||
|
* @param {number} params.promptTokens
|
||||||
|
* @param {number} params.completionTokens
|
||||||
|
* @param {AnthropicStreamUsage} [params.usage]
|
||||||
|
* @param {string} [params.model]
|
||||||
|
* @param {string} [params.context='message']
|
||||||
|
* @returns {Promise<void>}
|
||||||
|
*/
|
||||||
|
async recordTokenUsage({ promptTokens, completionTokens, usage, model, context = 'message' }) {
|
||||||
|
if (usage != null && usage?.input_tokens != null) {
|
||||||
|
const input = usage.input_tokens ?? 0;
|
||||||
|
const write = usage.cache_creation_input_tokens ?? 0;
|
||||||
|
const read = usage.cache_read_input_tokens ?? 0;
|
||||||
|
|
||||||
|
await spendStructuredTokens(
|
||||||
|
{
|
||||||
|
context,
|
||||||
|
user: this.user,
|
||||||
|
conversationId: this.conversationId,
|
||||||
|
model: model ?? this.modelOptions.model,
|
||||||
|
endpointTokenConfig: this.options.endpointTokenConfig,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
promptTokens: { input, write, read },
|
||||||
|
completionTokens,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
await spendTokens(
|
await spendTokens(
|
||||||
{
|
{
|
||||||
|
context,
|
||||||
user: this.user,
|
user: this.user,
|
||||||
model: this.modelOptions.model,
|
|
||||||
context: 'message',
|
|
||||||
conversationId: this.conversationId,
|
conversationId: this.conversationId,
|
||||||
|
model: model ?? this.modelOptions.model,
|
||||||
endpointTokenConfig: this.options.endpointTokenConfig,
|
endpointTokenConfig: this.options.endpointTokenConfig,
|
||||||
},
|
},
|
||||||
{ promptTokens, completionTokens },
|
{ promptTokens, completionTokens },
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param {TMessage[]} _messages
|
|
||||||
* @returns {TMessage[]}
|
|
||||||
*/
|
|
||||||
async addPreviousAttachments(_messages) {
|
|
||||||
if (!this.options.resendImages) {
|
|
||||||
return _messages;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param {TMessage} message
|
|
||||||
*/
|
|
||||||
const processMessage = async (message) => {
|
|
||||||
if (!this.message_file_map) {
|
|
||||||
/** @type {Record<string, MongoFile[]> */
|
|
||||||
this.message_file_map = {};
|
|
||||||
}
|
|
||||||
|
|
||||||
const fileIds = message.files.map((file) => file.file_id);
|
|
||||||
const files = await getFiles({
|
|
||||||
file_id: { $in: fileIds },
|
|
||||||
});
|
|
||||||
|
|
||||||
await this.addImageURLs(message, files);
|
|
||||||
|
|
||||||
this.message_file_map[message.messageId] = files;
|
|
||||||
return message;
|
|
||||||
};
|
|
||||||
|
|
||||||
const promises = [];
|
|
||||||
|
|
||||||
for (const message of _messages) {
|
|
||||||
if (!message.files) {
|
|
||||||
promises.push(message);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
promises.push(processMessage(message));
|
|
||||||
}
|
|
||||||
|
|
||||||
const messages = await Promise.all(promises);
|
|
||||||
|
|
||||||
this.checkVisionRequest(this.message_file_map);
|
|
||||||
return messages;
|
|
||||||
}
|
|
||||||
|
|
||||||
async buildMessages(messages, parentMessageId) {
|
async buildMessages(messages, parentMessageId) {
|
||||||
const orderedMessages = this.constructor.getMessagesForConversation({
|
const orderedMessages = this.constructor.getMessagesForConversation({
|
||||||
messages,
|
messages,
|
||||||
|
|
@ -242,12 +343,13 @@ class AnthropicClient extends BaseClient {
|
||||||
|
|
||||||
logger.debug('[AnthropicClient] orderedMessages', { orderedMessages, parentMessageId });
|
logger.debug('[AnthropicClient] orderedMessages', { orderedMessages, parentMessageId });
|
||||||
|
|
||||||
if (!this.isVisionModel && this.options.attachments) {
|
if (this.options.attachments) {
|
||||||
throw new Error('Attachments are only supported with the Claude 3 family of models');
|
const attachments = await this.options.attachments;
|
||||||
} else if (this.options.attachments) {
|
const images = attachments.filter((file) => file.type.includes('image'));
|
||||||
const attachments = (await this.options.attachments).filter((file) =>
|
|
||||||
file.type.includes('image'),
|
if (images.length && !this.isVisionModel) {
|
||||||
);
|
throw new Error('Images are only supported with the Claude 3 family of models');
|
||||||
|
}
|
||||||
|
|
||||||
const latestMessage = orderedMessages[orderedMessages.length - 1];
|
const latestMessage = orderedMessages[orderedMessages.length - 1];
|
||||||
|
|
||||||
|
|
@ -264,6 +366,13 @@ class AnthropicClient extends BaseClient {
|
||||||
this.options.attachments = files;
|
this.options.attachments = files;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.message_file_map) {
|
||||||
|
this.contextHandlers = createContextHandlers(
|
||||||
|
this.options.req,
|
||||||
|
orderedMessages[orderedMessages.length - 1].text,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const formattedMessages = orderedMessages.map((message, i) => {
|
const formattedMessages = orderedMessages.map((message, i) => {
|
||||||
const formattedMessage = this.useMessages
|
const formattedMessage = this.useMessages
|
||||||
? formatMessage({
|
? formatMessage({
|
||||||
|
|
@ -285,6 +394,11 @@ class AnthropicClient extends BaseClient {
|
||||||
if (this.message_file_map && this.message_file_map[message.messageId]) {
|
if (this.message_file_map && this.message_file_map[message.messageId]) {
|
||||||
const attachments = this.message_file_map[message.messageId];
|
const attachments = this.message_file_map[message.messageId];
|
||||||
for (const file of attachments) {
|
for (const file of attachments) {
|
||||||
|
if (file.embedded) {
|
||||||
|
this.contextHandlers?.processFile(file);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
orderedMessages[i].tokenCount += this.calculateImageTokenCost({
|
orderedMessages[i].tokenCount += this.calculateImageTokenCost({
|
||||||
width: file.width,
|
width: file.width,
|
||||||
height: file.height,
|
height: file.height,
|
||||||
|
|
@ -296,8 +410,13 @@ class AnthropicClient extends BaseClient {
|
||||||
return formattedMessage;
|
return formattedMessage;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (this.contextHandlers) {
|
||||||
|
this.augmentedPrompt = await this.contextHandlers.createContext();
|
||||||
|
this.options.promptPrefix = this.augmentedPrompt + (this.options.promptPrefix ?? '');
|
||||||
|
}
|
||||||
|
|
||||||
let { context: messagesInWindow, remainingContextTokens } =
|
let { context: messagesInWindow, remainingContextTokens } =
|
||||||
await this.getMessagesWithinTokenLimit(formattedMessages);
|
await this.getMessagesWithinTokenLimit({ messages: formattedMessages });
|
||||||
|
|
||||||
const tokenCountMap = orderedMessages
|
const tokenCountMap = orderedMessages
|
||||||
.slice(orderedMessages.length - messagesInWindow.length)
|
.slice(orderedMessages.length - messagesInWindow.length)
|
||||||
|
|
@ -372,7 +491,10 @@ class AnthropicClient extends BaseClient {
|
||||||
identityPrefix = `${identityPrefix}\nYou are ${this.options.modelLabel}`;
|
identityPrefix = `${identityPrefix}\nYou are ${this.options.modelLabel}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
let promptPrefix = (this.options.promptPrefix || '').trim();
|
let promptPrefix = (this.options.promptPrefix ?? '').trim();
|
||||||
|
if (typeof this.options.artifactsPrompt === 'string' && this.options.artifactsPrompt) {
|
||||||
|
promptPrefix = `${promptPrefix ?? ''}\n${this.options.artifactsPrompt}`.trim();
|
||||||
|
}
|
||||||
if (promptPrefix) {
|
if (promptPrefix) {
|
||||||
// If the prompt prefix doesn't end with the end token, add it.
|
// If the prompt prefix doesn't end with the end token, add it.
|
||||||
if (!promptPrefix.endsWith(`${this.endToken}`)) {
|
if (!promptPrefix.endsWith(`${this.endToken}`)) {
|
||||||
|
|
@ -389,7 +511,7 @@ class AnthropicClient extends BaseClient {
|
||||||
let isEdited = lastAuthor === this.assistantLabel;
|
let isEdited = lastAuthor === this.assistantLabel;
|
||||||
const promptSuffix = isEdited ? '' : `${promptPrefix}${this.assistantLabel}\n`;
|
const promptSuffix = isEdited ? '' : `${promptPrefix}${this.assistantLabel}\n`;
|
||||||
let currentTokenCount =
|
let currentTokenCount =
|
||||||
isEdited || this.useMEssages
|
isEdited || this.useMessages
|
||||||
? this.getTokenCount(promptPrefix)
|
? this.getTokenCount(promptPrefix)
|
||||||
: this.getTokenCount(promptSuffix);
|
: this.getTokenCount(promptSuffix);
|
||||||
|
|
||||||
|
|
@ -509,7 +631,7 @@ class AnthropicClient extends BaseClient {
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
if (this.modelOptions.model.startsWith('claude-3')) {
|
if (this.modelOptions.model.includes('claude-3')) {
|
||||||
await buildMessagesPayload();
|
await buildMessagesPayload();
|
||||||
processTokens();
|
processTokens();
|
||||||
return {
|
return {
|
||||||
|
|
@ -538,12 +660,39 @@ class AnthropicClient extends BaseClient {
|
||||||
logger.debug('AnthropicClient doesn\'t use getCompletion (all handled in sendCompletion)');
|
logger.debug('AnthropicClient doesn\'t use getCompletion (all handled in sendCompletion)');
|
||||||
}
|
}
|
||||||
|
|
||||||
async createResponse(client, options) {
|
/**
|
||||||
return this.useMessages
|
* Creates a message or completion response using the Anthropic client.
|
||||||
|
* @param {Anthropic} client - The Anthropic client instance.
|
||||||
|
* @param {Anthropic.default.MessageCreateParams | Anthropic.default.CompletionCreateParams} options - The options for the message or completion.
|
||||||
|
* @param {boolean} useMessages - Whether to use messages or completions. Defaults to `this.useMessages`.
|
||||||
|
* @returns {Promise<Anthropic.default.Message | Anthropic.default.Completion>} The response from the Anthropic client.
|
||||||
|
*/
|
||||||
|
async createResponse(client, options, useMessages) {
|
||||||
|
return useMessages ?? this.useMessages
|
||||||
? await client.messages.create(options)
|
? await client.messages.create(options)
|
||||||
: await client.completions.create(options);
|
: await client.completions.create(options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} modelName
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
checkPromptCacheSupport(modelName) {
|
||||||
|
const modelMatch = matchModelName(modelName, EModelEndpoint.anthropic);
|
||||||
|
if (modelMatch.includes('claude-3-5-sonnet-latest')) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
modelMatch === 'claude-3-5-sonnet' ||
|
||||||
|
modelMatch === 'claude-3-5-haiku' ||
|
||||||
|
modelMatch === 'claude-3-haiku' ||
|
||||||
|
modelMatch === 'claude-3-opus'
|
||||||
|
) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
async sendCompletion(payload, { onProgress, abortController }) {
|
async sendCompletion(payload, { onProgress, abortController }) {
|
||||||
if (!abortController) {
|
if (!abortController) {
|
||||||
abortController = new AbortController();
|
abortController = new AbortController();
|
||||||
|
|
@ -557,8 +706,6 @@ class AnthropicClient extends BaseClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.debug('modelOptions', { modelOptions });
|
logger.debug('modelOptions', { modelOptions });
|
||||||
|
|
||||||
const client = this.getClient();
|
|
||||||
const metadata = {
|
const metadata = {
|
||||||
user_id: this.user,
|
user_id: this.user,
|
||||||
};
|
};
|
||||||
|
|
@ -586,16 +733,28 @@ class AnthropicClient extends BaseClient {
|
||||||
|
|
||||||
if (this.useMessages) {
|
if (this.useMessages) {
|
||||||
requestOptions.messages = payload;
|
requestOptions.messages = payload;
|
||||||
requestOptions.max_tokens = maxOutputTokens || 1500;
|
requestOptions.max_tokens = maxOutputTokens || legacy.maxOutputTokens.default;
|
||||||
} else {
|
} else {
|
||||||
requestOptions.prompt = payload;
|
requestOptions.prompt = payload;
|
||||||
requestOptions.max_tokens_to_sample = maxOutputTokens || 1500;
|
requestOptions.max_tokens_to_sample = maxOutputTokens || 1500;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.systemMessage) {
|
if (this.systemMessage && this.supportsCacheControl === true) {
|
||||||
|
requestOptions.system = [
|
||||||
|
{
|
||||||
|
type: 'text',
|
||||||
|
text: this.systemMessage,
|
||||||
|
cache_control: { type: 'ephemeral' },
|
||||||
|
},
|
||||||
|
];
|
||||||
|
} else if (this.systemMessage) {
|
||||||
requestOptions.system = this.systemMessage;
|
requestOptions.system = this.systemMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.supportsCacheControl === true && this.useMessages) {
|
||||||
|
requestOptions.messages = addCacheControl(requestOptions.messages);
|
||||||
|
}
|
||||||
|
|
||||||
logger.debug('[AnthropicClient]', { ...requestOptions });
|
logger.debug('[AnthropicClient]', { ...requestOptions });
|
||||||
|
|
||||||
const handleChunk = (currentChunk) => {
|
const handleChunk = (currentChunk) => {
|
||||||
|
|
@ -606,12 +765,14 @@ class AnthropicClient extends BaseClient {
|
||||||
};
|
};
|
||||||
|
|
||||||
const maxRetries = 3;
|
const maxRetries = 3;
|
||||||
|
const streamRate = this.options.streamRate ?? Constants.DEFAULT_STREAM_RATE;
|
||||||
async function processResponse() {
|
async function processResponse() {
|
||||||
let attempts = 0;
|
let attempts = 0;
|
||||||
|
|
||||||
while (attempts < maxRetries) {
|
while (attempts < maxRetries) {
|
||||||
let response;
|
let response;
|
||||||
try {
|
try {
|
||||||
|
const client = this.getClient(requestOptions);
|
||||||
response = await this.createResponse(client, requestOptions);
|
response = await this.createResponse(client, requestOptions);
|
||||||
|
|
||||||
signal.addEventListener('abort', () => {
|
signal.addEventListener('abort', () => {
|
||||||
|
|
@ -623,11 +784,18 @@ class AnthropicClient extends BaseClient {
|
||||||
|
|
||||||
for await (const completion of response) {
|
for await (const completion of response) {
|
||||||
// Handle each completion as before
|
// Handle each completion as before
|
||||||
|
const type = completion?.type ?? '';
|
||||||
|
if (tokenEventTypes.has(type)) {
|
||||||
|
logger.debug(`[AnthropicClient] ${type}`, completion);
|
||||||
|
this[type] = completion;
|
||||||
|
}
|
||||||
if (completion?.delta?.text) {
|
if (completion?.delta?.text) {
|
||||||
handleChunk(completion.delta.text);
|
handleChunk(completion.delta.text);
|
||||||
} else if (completion.completion) {
|
} else if (completion.completion) {
|
||||||
handleChunk(completion.completion);
|
handleChunk(completion.completion);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await sleep(streamRate);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Successful processing, exit loop
|
// Successful processing, exit loop
|
||||||
|
|
@ -661,8 +829,15 @@ class AnthropicClient extends BaseClient {
|
||||||
|
|
||||||
getSaveOptions() {
|
getSaveOptions() {
|
||||||
return {
|
return {
|
||||||
|
maxContextTokens: this.options.maxContextTokens,
|
||||||
|
artifacts: this.options.artifacts,
|
||||||
promptPrefix: this.options.promptPrefix,
|
promptPrefix: this.options.promptPrefix,
|
||||||
modelLabel: this.options.modelLabel,
|
modelLabel: this.options.modelLabel,
|
||||||
|
promptCache: this.options.promptCache,
|
||||||
|
resendFiles: this.options.resendFiles,
|
||||||
|
iconURL: this.options.iconURL,
|
||||||
|
greeting: this.options.greeting,
|
||||||
|
spec: this.options.spec,
|
||||||
...this.modelOptions,
|
...this.modelOptions,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
@ -671,22 +846,96 @@ class AnthropicClient extends BaseClient {
|
||||||
logger.debug('AnthropicClient doesn\'t use getBuildMessagesOptions');
|
logger.debug('AnthropicClient doesn\'t use getBuildMessagesOptions');
|
||||||
}
|
}
|
||||||
|
|
||||||
static getTokenizer(encoding, isModelName = false, extendSpecialTokens = {}) {
|
getEncoding() {
|
||||||
if (tokenizersCache[encoding]) {
|
return 'cl100k_base';
|
||||||
return tokenizersCache[encoding];
|
|
||||||
}
|
|
||||||
let tokenizer;
|
|
||||||
if (isModelName) {
|
|
||||||
tokenizer = encodingForModel(encoding, extendSpecialTokens);
|
|
||||||
} else {
|
|
||||||
tokenizer = getEncoding(encoding, extendSpecialTokens);
|
|
||||||
}
|
|
||||||
tokenizersCache[encoding] = tokenizer;
|
|
||||||
return tokenizer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the token count of a given text. It also checks and resets the tokenizers if necessary.
|
||||||
|
* @param {string} text - The text to get the token count for.
|
||||||
|
* @returns {number} The token count of the given text.
|
||||||
|
*/
|
||||||
getTokenCount(text) {
|
getTokenCount(text) {
|
||||||
return this.gptEncoder.encode(text, 'all').length;
|
const encoding = this.getEncoding();
|
||||||
|
return Tokenizer.getTokenCount(text, encoding);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates a concise title for a conversation based on the user's input text and response.
|
||||||
|
* Involves sending a chat completion request with specific instructions for title generation.
|
||||||
|
*
|
||||||
|
* This function capitlizes on [Anthropic's function calling training](https://docs.anthropic.com/claude/docs/functions-external-tools).
|
||||||
|
*
|
||||||
|
* @param {Object} params - The parameters for the conversation title generation.
|
||||||
|
* @param {string} params.text - The user's input.
|
||||||
|
* @param {string} [params.responseText=''] - The AI's immediate response to the user.
|
||||||
|
*
|
||||||
|
* @returns {Promise<string | 'New Chat'>} A promise that resolves to the generated conversation title.
|
||||||
|
* In case of failure, it will return the default title, "New Chat".
|
||||||
|
*/
|
||||||
|
async titleConvo({ text, responseText = '' }) {
|
||||||
|
let title = 'New Chat';
|
||||||
|
this.message_delta = undefined;
|
||||||
|
this.message_start = undefined;
|
||||||
|
const convo = `<initial_message>
|
||||||
|
${truncateText(text)}
|
||||||
|
</initial_message>
|
||||||
|
<response>
|
||||||
|
${JSON.stringify(truncateText(responseText))}
|
||||||
|
</response>`;
|
||||||
|
|
||||||
|
const { ANTHROPIC_TITLE_MODEL } = process.env ?? {};
|
||||||
|
const model = this.options.titleModel ?? ANTHROPIC_TITLE_MODEL ?? 'claude-3-haiku-20240307';
|
||||||
|
const system = titleFunctionPrompt;
|
||||||
|
|
||||||
|
const titleChatCompletion = async () => {
|
||||||
|
const content = `<conversation_context>
|
||||||
|
${convo}
|
||||||
|
</conversation_context>
|
||||||
|
|
||||||
|
Please generate a title for this conversation.`;
|
||||||
|
|
||||||
|
const titleMessage = { role: 'user', content };
|
||||||
|
const requestOptions = {
|
||||||
|
model,
|
||||||
|
temperature: 0.3,
|
||||||
|
max_tokens: 1024,
|
||||||
|
system,
|
||||||
|
stop_sequences: ['\n\nHuman:', '\n\nAssistant', '</function_calls>'],
|
||||||
|
messages: [titleMessage],
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await this.createResponse(
|
||||||
|
this.getClient(requestOptions),
|
||||||
|
requestOptions,
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
let promptTokens = response?.usage?.input_tokens;
|
||||||
|
let completionTokens = response?.usage?.output_tokens;
|
||||||
|
if (!promptTokens) {
|
||||||
|
promptTokens = this.getTokenCountForMessage(titleMessage);
|
||||||
|
promptTokens += this.getTokenCountForMessage({ role: 'system', content: system });
|
||||||
|
}
|
||||||
|
if (!completionTokens) {
|
||||||
|
completionTokens = this.getTokenCountForMessage(response.content[0]);
|
||||||
|
}
|
||||||
|
await this.recordTokenUsage({
|
||||||
|
model,
|
||||||
|
promptTokens,
|
||||||
|
completionTokens,
|
||||||
|
context: 'title',
|
||||||
|
});
|
||||||
|
const text = response.content[0].text;
|
||||||
|
title = parseParamFromPrompt(text, 'title');
|
||||||
|
} catch (e) {
|
||||||
|
logger.error('[AnthropicClient] There was an issue generating the title', e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
await titleChatCompletion();
|
||||||
|
logger.debug('[AnthropicClient] Convo Title: ' + title);
|
||||||
|
return title;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,18 @@
|
||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
const { supportsBalanceCheck, Constants } = require('librechat-data-provider');
|
const fetch = require('node-fetch');
|
||||||
const { getConvo, getMessages, saveMessage, updateMessage, saveConvo } = require('~/models');
|
const {
|
||||||
|
supportsBalanceCheck,
|
||||||
|
isAgentsEndpoint,
|
||||||
|
isParamEndpoint,
|
||||||
|
EModelEndpoint,
|
||||||
|
ErrorTypes,
|
||||||
|
Constants,
|
||||||
|
} = require('librechat-data-provider');
|
||||||
|
const { getMessages, saveMessage, updateMessage, saveConvo } = require('~/models');
|
||||||
const { addSpaceIfNeeded, isEnabled } = require('~/server/utils');
|
const { addSpaceIfNeeded, isEnabled } = require('~/server/utils');
|
||||||
|
const { truncateToolCallOutputs } = require('./prompts');
|
||||||
const checkBalance = require('~/models/checkBalance');
|
const checkBalance = require('~/models/checkBalance');
|
||||||
|
const { getFiles } = require('~/models/File');
|
||||||
const TextStream = require('./TextStream');
|
const TextStream = require('./TextStream');
|
||||||
const { logger } = require('~/config');
|
const { logger } = require('~/config');
|
||||||
|
|
||||||
|
|
@ -16,13 +26,46 @@ class BaseClient {
|
||||||
month: 'long',
|
month: 'long',
|
||||||
day: 'numeric',
|
day: 'numeric',
|
||||||
});
|
});
|
||||||
|
this.fetch = this.fetch.bind(this);
|
||||||
|
/** @type {boolean} */
|
||||||
|
this.skipSaveConvo = false;
|
||||||
|
/** @type {boolean} */
|
||||||
|
this.skipSaveUserMessage = false;
|
||||||
|
/** @type {ClientDatabaseSavePromise} */
|
||||||
|
this.userMessagePromise;
|
||||||
|
/** @type {ClientDatabaseSavePromise} */
|
||||||
|
this.responsePromise;
|
||||||
|
/** @type {string} */
|
||||||
|
this.user;
|
||||||
|
/** @type {string} */
|
||||||
|
this.conversationId;
|
||||||
|
/** @type {string} */
|
||||||
|
this.responseMessageId;
|
||||||
|
/** @type {TAttachment[]} */
|
||||||
|
this.attachments;
|
||||||
|
/** The key for the usage object's input tokens
|
||||||
|
* @type {string} */
|
||||||
|
this.inputTokensKey = 'prompt_tokens';
|
||||||
|
/** The key for the usage object's output tokens
|
||||||
|
* @type {string} */
|
||||||
|
this.outputTokensKey = 'completion_tokens';
|
||||||
|
/** @type {Set<string>} */
|
||||||
|
this.savedMessageIds = new Set();
|
||||||
|
/**
|
||||||
|
* Flag to determine if the client re-submitted the latest assistant message.
|
||||||
|
* @type {boolean | undefined} */
|
||||||
|
this.continued;
|
||||||
|
/** @type {TMessage[]} */
|
||||||
|
this.currentMessages = [];
|
||||||
|
/** @type {import('librechat-data-provider').VisionModes | undefined} */
|
||||||
|
this.visionMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
setOptions() {
|
setOptions() {
|
||||||
throw new Error('Method \'setOptions\' must be implemented.');
|
throw new Error('Method \'setOptions\' must be implemented.');
|
||||||
}
|
}
|
||||||
|
|
||||||
getCompletion() {
|
async getCompletion() {
|
||||||
throw new Error('Method \'getCompletion\' must be implemented.');
|
throw new Error('Method \'getCompletion\' must be implemented.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -42,21 +85,59 @@ class BaseClient {
|
||||||
throw new Error('Subclasses attempted to call summarizeMessages without implementing it');
|
throw new Error('Subclasses attempted to call summarizeMessages without implementing it');
|
||||||
}
|
}
|
||||||
|
|
||||||
async getTokenCountForResponse(response) {
|
/**
|
||||||
logger.debug('`[BaseClient] recordTokenUsage` not implemented.', response);
|
* @returns {string}
|
||||||
|
*/
|
||||||
|
getResponseModel() {
|
||||||
|
if (isAgentsEndpoint(this.options.endpoint) && this.options.agent && this.options.agent.id) {
|
||||||
|
return this.options.agent.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.modelOptions?.model ?? this.model;
|
||||||
}
|
}
|
||||||
|
|
||||||
async addPreviousAttachments(messages) {
|
/**
|
||||||
return messages;
|
* Abstract method to get the token count for a message. Subclasses must implement this method.
|
||||||
|
* @param {TMessage} responseMessage
|
||||||
|
* @returns {number}
|
||||||
|
*/
|
||||||
|
getTokenCountForResponse(responseMessage) {
|
||||||
|
logger.debug('[BaseClient] `recordTokenUsage` not implemented.', responseMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Abstract method to record token usage. Subclasses must implement this method.
|
||||||
|
* If a correction to the token usage is needed, the method should return an object with the corrected token counts.
|
||||||
|
* @param {number} promptTokens
|
||||||
|
* @param {number} completionTokens
|
||||||
|
* @returns {Promise<void>}
|
||||||
|
*/
|
||||||
async recordTokenUsage({ promptTokens, completionTokens }) {
|
async recordTokenUsage({ promptTokens, completionTokens }) {
|
||||||
logger.debug('`[BaseClient] recordTokenUsage` not implemented.', {
|
logger.debug('[BaseClient] `recordTokenUsage` not implemented.', {
|
||||||
promptTokens,
|
promptTokens,
|
||||||
completionTokens,
|
completionTokens,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Makes an HTTP request and logs the process.
|
||||||
|
*
|
||||||
|
* @param {RequestInfo} url - The URL to make the request to. Can be a string or a Request object.
|
||||||
|
* @param {RequestInit} [init] - Optional init options for the request.
|
||||||
|
* @returns {Promise<Response>} - A promise that resolves to the response of the fetch request.
|
||||||
|
*/
|
||||||
|
async fetch(_url, init) {
|
||||||
|
let url = _url;
|
||||||
|
if (this.options.directEndpoint) {
|
||||||
|
url = this.options.reverseProxyUrl;
|
||||||
|
}
|
||||||
|
logger.debug(`Making request to ${url}`);
|
||||||
|
if (typeof Bun !== 'undefined') {
|
||||||
|
return await fetch(url, init);
|
||||||
|
}
|
||||||
|
return await fetch(url, init);
|
||||||
|
}
|
||||||
|
|
||||||
getBuildMessagesOptions() {
|
getBuildMessagesOptions() {
|
||||||
throw new Error('Subclasses must implement getBuildMessagesOptions');
|
throw new Error('Subclasses must implement getBuildMessagesOptions');
|
||||||
}
|
}
|
||||||
|
|
@ -66,19 +147,45 @@ class BaseClient {
|
||||||
await stream.processTextStream(onProgress);
|
await stream.processTextStream(onProgress);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @returns {[string|undefined, string|undefined]}
|
||||||
|
*/
|
||||||
|
processOverideIds() {
|
||||||
|
/** @type {Record<string, string | undefined>} */
|
||||||
|
let { overrideConvoId, overrideUserMessageId } = this.options?.req?.body ?? {};
|
||||||
|
if (overrideConvoId) {
|
||||||
|
const [conversationId, index] = overrideConvoId.split(Constants.COMMON_DIVIDER);
|
||||||
|
overrideConvoId = conversationId;
|
||||||
|
if (index !== '0') {
|
||||||
|
this.skipSaveConvo = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (overrideUserMessageId) {
|
||||||
|
const [userMessageId, index] = overrideUserMessageId.split(Constants.COMMON_DIVIDER);
|
||||||
|
overrideUserMessageId = userMessageId;
|
||||||
|
if (index !== '0') {
|
||||||
|
this.skipSaveUserMessage = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return [overrideConvoId, overrideUserMessageId];
|
||||||
|
}
|
||||||
|
|
||||||
async setMessageOptions(opts = {}) {
|
async setMessageOptions(opts = {}) {
|
||||||
if (opts && opts.replaceOptions) {
|
if (opts && opts.replaceOptions) {
|
||||||
this.setOptions(opts);
|
this.setOptions(opts);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const [overrideConvoId, overrideUserMessageId] = this.processOverideIds();
|
||||||
const { isEdited, isContinued } = opts;
|
const { isEdited, isContinued } = opts;
|
||||||
const user = opts.user ?? null;
|
const user = opts.user ?? null;
|
||||||
this.user = user;
|
this.user = user;
|
||||||
const saveOptions = this.getSaveOptions();
|
const saveOptions = this.getSaveOptions();
|
||||||
this.abortController = opts.abortController ?? new AbortController();
|
this.abortController = opts.abortController ?? new AbortController();
|
||||||
const conversationId = opts.conversationId ?? crypto.randomUUID();
|
const conversationId = overrideConvoId ?? opts.conversationId ?? crypto.randomUUID();
|
||||||
const parentMessageId = opts.parentMessageId ?? Constants.NO_PARENT;
|
const parentMessageId = opts.parentMessageId ?? Constants.NO_PARENT;
|
||||||
const userMessageId = opts.overrideParentMessageId ?? crypto.randomUUID();
|
const userMessageId =
|
||||||
|
overrideUserMessageId ?? opts.overrideParentMessageId ?? crypto.randomUUID();
|
||||||
let responseMessageId = opts.responseMessageId ?? crypto.randomUUID();
|
let responseMessageId = opts.responseMessageId ?? crypto.randomUUID();
|
||||||
let head = isEdited ? responseMessageId : parentMessageId;
|
let head = isEdited ? responseMessageId : parentMessageId;
|
||||||
this.currentMessages = (await this.loadHistory(conversationId, head)) ?? [];
|
this.currentMessages = (await this.loadHistory(conversationId, head)) ?? [];
|
||||||
|
|
@ -90,6 +197,8 @@ class BaseClient {
|
||||||
this.currentMessages[this.currentMessages.length - 1].messageId = head;
|
this.currentMessages[this.currentMessages.length - 1].messageId = head;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.responseMessageId = responseMessageId;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...opts,
|
...opts,
|
||||||
user,
|
user,
|
||||||
|
|
@ -138,11 +247,12 @@ class BaseClient {
|
||||||
userMessage,
|
userMessage,
|
||||||
conversationId,
|
conversationId,
|
||||||
responseMessageId,
|
responseMessageId,
|
||||||
|
sender: this.sender,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof opts?.onStart === 'function') {
|
if (typeof opts?.onStart === 'function') {
|
||||||
opts.onStart(userMessage);
|
opts.onStart(userMessage, responseMessageId);
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
@ -159,17 +269,24 @@ class BaseClient {
|
||||||
/**
|
/**
|
||||||
* Adds instructions to the messages array. If the instructions object is empty or undefined,
|
* Adds instructions to the messages array. If the instructions object is empty or undefined,
|
||||||
* the original messages array is returned. Otherwise, the instructions are added to the messages
|
* the original messages array is returned. Otherwise, the instructions are added to the messages
|
||||||
* array, preserving the last message at the end.
|
* array either at the beginning (default) or preserving the last message at the end.
|
||||||
*
|
*
|
||||||
* @param {Array} messages - An array of messages.
|
* @param {Array} messages - An array of messages.
|
||||||
* @param {Object} instructions - An object containing instructions to be added to the messages.
|
* @param {Object} instructions - An object containing instructions to be added to the messages.
|
||||||
|
* @param {boolean} [beforeLast=false] - If true, adds instructions before the last message; if false, adds at the beginning.
|
||||||
* @returns {Array} An array containing messages and instructions, or the original messages if instructions are empty.
|
* @returns {Array} An array containing messages and instructions, or the original messages if instructions are empty.
|
||||||
*/
|
*/
|
||||||
addInstructions(messages, instructions) {
|
addInstructions(messages, instructions, beforeLast = false) {
|
||||||
const payload = [];
|
|
||||||
if (!instructions || Object.keys(instructions).length === 0) {
|
if (!instructions || Object.keys(instructions).length === 0) {
|
||||||
return messages;
|
return messages;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!beforeLast) {
|
||||||
|
return [instructions, ...messages];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Legacy behavior: add instructions before the last message
|
||||||
|
const payload = [];
|
||||||
if (messages.length > 1) {
|
if (messages.length > 1) {
|
||||||
payload.push(...messages.slice(0, -1));
|
payload.push(...messages.slice(0, -1));
|
||||||
}
|
}
|
||||||
|
|
@ -184,6 +301,9 @@ class BaseClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
async handleTokenCountMap(tokenCountMap) {
|
async handleTokenCountMap(tokenCountMap) {
|
||||||
|
if (this.clientName === EModelEndpoint.agents) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (this.currentMessages.length === 0) {
|
if (this.currentMessages.length === 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -232,25 +352,38 @@ class BaseClient {
|
||||||
* If the token limit would be exceeded by adding a message, that message is not added to the context and remains in the original array.
|
* If the token limit would be exceeded by adding a message, that message is not added to the context and remains in the original array.
|
||||||
* The method uses `push` and `pop` operations for efficient array manipulation, and reverses the context array at the end to maintain the original order of the messages.
|
* The method uses `push` and `pop` operations for efficient array manipulation, and reverses the context array at the end to maintain the original order of the messages.
|
||||||
*
|
*
|
||||||
* @param {Array} _messages - An array of messages, each with a `tokenCount` property. The messages should be ordered from oldest to newest.
|
* @param {Object} params
|
||||||
* @param {number} [maxContextTokens] - The max number of tokens allowed in the context. If not provided, defaults to `this.maxContextTokens`.
|
* @param {TMessage[]} params.messages - An array of messages, each with a `tokenCount` property. The messages should be ordered from oldest to newest.
|
||||||
* @returns {Object} An object with four properties: `context`, `summaryIndex`, `remainingContextTokens`, and `messagesToRefine`.
|
* @param {number} [params.maxContextTokens] - The max number of tokens allowed in the context. If not provided, defaults to `this.maxContextTokens`.
|
||||||
|
* @param {{ role: 'system', content: text, tokenCount: number }} [params.instructions] - Instructions already added to the context at index 0.
|
||||||
|
* @returns {Promise<{
|
||||||
|
* context: TMessage[],
|
||||||
|
* remainingContextTokens: number,
|
||||||
|
* messagesToRefine: TMessage[],
|
||||||
|
* summaryIndex: number,
|
||||||
|
* }>} An object with four properties: `context`, `summaryIndex`, `remainingContextTokens`, and `messagesToRefine`.
|
||||||
* `context` is an array of messages that fit within the token limit.
|
* `context` is an array of messages that fit within the token limit.
|
||||||
* `summaryIndex` is the index of the first message in the `messagesToRefine` array.
|
* `summaryIndex` is the index of the first message in the `messagesToRefine` array.
|
||||||
* `remainingContextTokens` is the number of tokens remaining within the limit after adding the messages to the context.
|
* `remainingContextTokens` is the number of tokens remaining within the limit after adding the messages to the context.
|
||||||
* `messagesToRefine` is an array of messages that were not added to the context because they would have exceeded the token limit.
|
* `messagesToRefine` is an array of messages that were not added to the context because they would have exceeded the token limit.
|
||||||
*/
|
*/
|
||||||
async getMessagesWithinTokenLimit(_messages, maxContextTokens) {
|
async getMessagesWithinTokenLimit({ messages: _messages, maxContextTokens, instructions }) {
|
||||||
// Every reply is primed with <|start|>assistant<|message|>, so we
|
// Every reply is primed with <|start|>assistant<|message|>, so we
|
||||||
// start with 3 tokens for the label after all messages have been counted.
|
// start with 3 tokens for the label after all messages have been counted.
|
||||||
let currentTokenCount = 3;
|
|
||||||
let summaryIndex = -1;
|
let summaryIndex = -1;
|
||||||
let remainingContextTokens = maxContextTokens ?? this.maxContextTokens;
|
let currentTokenCount = 3;
|
||||||
|
const instructionsTokenCount = instructions?.tokenCount ?? 0;
|
||||||
|
let remainingContextTokens =
|
||||||
|
(maxContextTokens ?? this.maxContextTokens) - instructionsTokenCount;
|
||||||
const messages = [..._messages];
|
const messages = [..._messages];
|
||||||
|
|
||||||
const context = [];
|
const context = [];
|
||||||
|
|
||||||
if (currentTokenCount < remainingContextTokens) {
|
if (currentTokenCount < remainingContextTokens) {
|
||||||
while (messages.length > 0 && currentTokenCount < remainingContextTokens) {
|
while (messages.length > 0 && currentTokenCount < remainingContextTokens) {
|
||||||
|
if (messages.length === 1 && instructions) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
const poppedMessage = messages.pop();
|
const poppedMessage = messages.pop();
|
||||||
const { tokenCount } = poppedMessage;
|
const { tokenCount } = poppedMessage;
|
||||||
|
|
||||||
|
|
@ -264,6 +397,11 @@ class BaseClient {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (instructions) {
|
||||||
|
context.push(_messages[0]);
|
||||||
|
messages.shift();
|
||||||
|
}
|
||||||
|
|
||||||
const prunedMemory = messages;
|
const prunedMemory = messages;
|
||||||
summaryIndex = prunedMemory.length - 1;
|
summaryIndex = prunedMemory.length - 1;
|
||||||
remainingContextTokens -= currentTokenCount;
|
remainingContextTokens -= currentTokenCount;
|
||||||
|
|
@ -276,19 +414,50 @@ class BaseClient {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async handleContextStrategy({ instructions, orderedMessages, formattedMessages }) {
|
async handleContextStrategy({
|
||||||
|
instructions,
|
||||||
|
orderedMessages,
|
||||||
|
formattedMessages,
|
||||||
|
buildTokenMap = true,
|
||||||
|
}) {
|
||||||
let _instructions;
|
let _instructions;
|
||||||
let tokenCount;
|
let tokenCount;
|
||||||
|
|
||||||
if (instructions) {
|
if (instructions) {
|
||||||
({ tokenCount, ..._instructions } = instructions);
|
({ tokenCount, ..._instructions } = instructions);
|
||||||
}
|
}
|
||||||
|
|
||||||
_instructions && logger.debug('[BaseClient] instructions tokenCount: ' + tokenCount);
|
_instructions && logger.debug('[BaseClient] instructions tokenCount: ' + tokenCount);
|
||||||
let payload = this.addInstructions(formattedMessages, _instructions);
|
if (tokenCount && tokenCount > this.maxContextTokens) {
|
||||||
|
const info = `${tokenCount} / ${this.maxContextTokens}`;
|
||||||
|
const errorMessage = `{ "type": "${ErrorTypes.INPUT_LENGTH}", "info": "${info}" }`;
|
||||||
|
logger.warn(`Instructions token count exceeds max token count (${info}).`);
|
||||||
|
throw new Error(errorMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.clientName === EModelEndpoint.agents) {
|
||||||
|
const { dbMessages, editedIndices } = truncateToolCallOutputs(
|
||||||
|
orderedMessages,
|
||||||
|
this.maxContextTokens,
|
||||||
|
this.getTokenCountForMessage.bind(this),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (editedIndices.length > 0) {
|
||||||
|
logger.debug('[BaseClient] Truncated tool call outputs:', editedIndices);
|
||||||
|
for (const index of editedIndices) {
|
||||||
|
formattedMessages[index].content = dbMessages[index].content;
|
||||||
|
}
|
||||||
|
orderedMessages = dbMessages;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let orderedWithInstructions = this.addInstructions(orderedMessages, instructions);
|
let orderedWithInstructions = this.addInstructions(orderedMessages, instructions);
|
||||||
|
|
||||||
let { context, remainingContextTokens, messagesToRefine, summaryIndex } =
|
let { context, remainingContextTokens, messagesToRefine, summaryIndex } =
|
||||||
await this.getMessagesWithinTokenLimit(orderedWithInstructions);
|
await this.getMessagesWithinTokenLimit({
|
||||||
|
messages: orderedWithInstructions,
|
||||||
|
instructions,
|
||||||
|
});
|
||||||
|
|
||||||
logger.debug('[BaseClient] Context Count (1/2)', {
|
logger.debug('[BaseClient] Context Count (1/2)', {
|
||||||
remainingContextTokens,
|
remainingContextTokens,
|
||||||
|
|
@ -300,7 +469,9 @@ class BaseClient {
|
||||||
let { shouldSummarize } = this;
|
let { shouldSummarize } = this;
|
||||||
|
|
||||||
// Calculate the difference in length to determine how many messages were discarded if any
|
// Calculate the difference in length to determine how many messages were discarded if any
|
||||||
const { length } = payload;
|
let payload;
|
||||||
|
let { length } = formattedMessages;
|
||||||
|
length += instructions != null ? 1 : 0;
|
||||||
const diff = length - context.length;
|
const diff = length - context.length;
|
||||||
const firstMessage = orderedWithInstructions[0];
|
const firstMessage = orderedWithInstructions[0];
|
||||||
const usePrevSummary =
|
const usePrevSummary =
|
||||||
|
|
@ -310,17 +481,31 @@ class BaseClient {
|
||||||
this.previous_summary.messageId === firstMessage.messageId;
|
this.previous_summary.messageId === firstMessage.messageId;
|
||||||
|
|
||||||
if (diff > 0) {
|
if (diff > 0) {
|
||||||
payload = payload.slice(diff);
|
payload = formattedMessages.slice(diff);
|
||||||
logger.debug(
|
logger.debug(
|
||||||
`[BaseClient] Difference between original payload (${length}) and context (${context.length}): ${diff}`,
|
`[BaseClient] Difference between original payload (${length}) and context (${context.length}): ${diff}`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
payload = this.addInstructions(payload ?? formattedMessages, _instructions);
|
||||||
|
|
||||||
const latestMessage = orderedWithInstructions[orderedWithInstructions.length - 1];
|
const latestMessage = orderedWithInstructions[orderedWithInstructions.length - 1];
|
||||||
if (payload.length === 0 && !shouldSummarize && latestMessage) {
|
if (payload.length === 0 && !shouldSummarize && latestMessage) {
|
||||||
throw new Error(
|
const info = `${latestMessage.tokenCount} / ${this.maxContextTokens}`;
|
||||||
`Prompt token count of ${latestMessage.tokenCount} exceeds max token count of ${this.maxContextTokens}.`,
|
const errorMessage = `{ "type": "${ErrorTypes.INPUT_LENGTH}", "info": "${info}" }`;
|
||||||
|
logger.warn(`Prompt token count exceeds max token count (${info}).`);
|
||||||
|
throw new Error(errorMessage);
|
||||||
|
} else if (
|
||||||
|
_instructions &&
|
||||||
|
payload.length === 1 &&
|
||||||
|
payload[0].content === _instructions.content
|
||||||
|
) {
|
||||||
|
const info = `${tokenCount + 3} / ${this.maxContextTokens}`;
|
||||||
|
const errorMessage = `{ "type": "${ErrorTypes.INPUT_LENGTH}", "info": "${info}" }`;
|
||||||
|
logger.warn(
|
||||||
|
`Including instructions, the prompt token count exceeds remaining max token count (${info}).`,
|
||||||
);
|
);
|
||||||
|
throw new Error(errorMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (usePrevSummary) {
|
if (usePrevSummary) {
|
||||||
|
|
@ -345,19 +530,23 @@ class BaseClient {
|
||||||
maxContextTokens: this.maxContextTokens,
|
maxContextTokens: this.maxContextTokens,
|
||||||
});
|
});
|
||||||
|
|
||||||
let tokenCountMap = orderedWithInstructions.reduce((map, message, index) => {
|
/** @type {Record<string, number> | undefined} */
|
||||||
const { messageId } = message;
|
let tokenCountMap;
|
||||||
if (!messageId) {
|
if (buildTokenMap) {
|
||||||
|
tokenCountMap = orderedWithInstructions.reduce((map, message, index) => {
|
||||||
|
const { messageId } = message;
|
||||||
|
if (!messageId) {
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (shouldSummarize && index === summaryIndex && !usePrevSummary) {
|
||||||
|
map.summaryMessage = { ...summaryMessage, messageId, tokenCount: summaryTokenCount };
|
||||||
|
}
|
||||||
|
|
||||||
|
map[messageId] = orderedWithInstructions[index].tokenCount;
|
||||||
return map;
|
return map;
|
||||||
}
|
}, {});
|
||||||
|
}
|
||||||
if (shouldSummarize && index === summaryIndex && !usePrevSummary) {
|
|
||||||
map.summaryMessage = { ...summaryMessage, messageId, tokenCount: summaryTokenCount };
|
|
||||||
}
|
|
||||||
|
|
||||||
map[messageId] = orderedWithInstructions[index].tokenCount;
|
|
||||||
return map;
|
|
||||||
}, {});
|
|
||||||
|
|
||||||
const promptTokens = this.maxContextTokens - remainingContextTokens;
|
const promptTokens = this.maxContextTokens - remainingContextTokens;
|
||||||
|
|
||||||
|
|
@ -376,6 +565,14 @@ class BaseClient {
|
||||||
const { user, head, isEdited, conversationId, responseMessageId, saveOptions, userMessage } =
|
const { user, head, isEdited, conversationId, responseMessageId, saveOptions, userMessage } =
|
||||||
await this.handleStartMethods(message, opts);
|
await this.handleStartMethods(message, opts);
|
||||||
|
|
||||||
|
if (opts.progressCallback) {
|
||||||
|
opts.onProgress = opts.progressCallback.call(null, {
|
||||||
|
...(opts.progressOptions ?? {}),
|
||||||
|
parentMessageId: userMessage.messageId,
|
||||||
|
messageId: responseMessageId,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const { generation = '' } = opts;
|
const { generation = '' } = opts;
|
||||||
|
|
||||||
// It's not necessary to push to currentMessages
|
// It's not necessary to push to currentMessages
|
||||||
|
|
@ -389,7 +586,7 @@ class BaseClient {
|
||||||
conversationId,
|
conversationId,
|
||||||
parentMessageId: userMessage.messageId,
|
parentMessageId: userMessage.messageId,
|
||||||
isCreatedByUser: false,
|
isCreatedByUser: false,
|
||||||
model: this.modelOptions.model,
|
model: this.modelOptions?.model ?? this.model,
|
||||||
sender: this.sender,
|
sender: this.sender,
|
||||||
text: generation,
|
text: generation,
|
||||||
};
|
};
|
||||||
|
|
@ -397,6 +594,7 @@ class BaseClient {
|
||||||
} else {
|
} else {
|
||||||
latestMessage.text = generation;
|
latestMessage.text = generation;
|
||||||
}
|
}
|
||||||
|
this.continued = true;
|
||||||
} else {
|
} else {
|
||||||
this.currentMessages.push(userMessage);
|
this.currentMessages.push(userMessage);
|
||||||
}
|
}
|
||||||
|
|
@ -424,8 +622,14 @@ class BaseClient {
|
||||||
this.handleTokenCountMap(tokenCountMap);
|
this.handleTokenCountMap(tokenCountMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isEdited) {
|
if (!isEdited && !this.skipSaveUserMessage) {
|
||||||
await this.saveMessageToDatabase(userMessage, saveOptions, user);
|
this.userMessagePromise = this.saveMessageToDatabase(userMessage, saveOptions, user);
|
||||||
|
this.savedMessageIds.add(userMessage.messageId);
|
||||||
|
if (typeof opts?.getReqData === 'function') {
|
||||||
|
opts.getReqData({
|
||||||
|
userMessagePromise: this.userMessagePromise,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
|
|
@ -439,45 +643,151 @@ class BaseClient {
|
||||||
user: this.user,
|
user: this.user,
|
||||||
tokenType: 'prompt',
|
tokenType: 'prompt',
|
||||||
amount: promptTokens,
|
amount: promptTokens,
|
||||||
model: this.modelOptions.model,
|
|
||||||
endpoint: this.options.endpoint,
|
endpoint: this.options.endpoint,
|
||||||
|
model: this.modelOptions?.model ?? this.model,
|
||||||
endpointTokenConfig: this.options.endpointTokenConfig,
|
endpointTokenConfig: this.options.endpointTokenConfig,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @type {string|string[]|undefined} */
|
||||||
const completion = await this.sendCompletion(payload, opts);
|
const completion = await this.sendCompletion(payload, opts);
|
||||||
this.abortController.requestCompleted = true;
|
this.abortController.requestCompleted = true;
|
||||||
|
|
||||||
|
/** @type {TMessage} */
|
||||||
const responseMessage = {
|
const responseMessage = {
|
||||||
messageId: responseMessageId,
|
messageId: responseMessageId,
|
||||||
conversationId,
|
conversationId,
|
||||||
parentMessageId: userMessage.messageId,
|
parentMessageId: userMessage.messageId,
|
||||||
isCreatedByUser: false,
|
isCreatedByUser: false,
|
||||||
isEdited,
|
isEdited,
|
||||||
model: this.modelOptions.model,
|
model: this.getResponseModel(),
|
||||||
sender: this.sender,
|
sender: this.sender,
|
||||||
text: addSpaceIfNeeded(generation) + completion,
|
|
||||||
promptTokens,
|
promptTokens,
|
||||||
|
iconURL: this.options.iconURL,
|
||||||
|
endpoint: this.options.endpoint,
|
||||||
|
...(this.metadata ?? {}),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (typeof completion === 'string') {
|
||||||
|
responseMessage.text = addSpaceIfNeeded(generation) + completion;
|
||||||
|
} else if (
|
||||||
|
Array.isArray(completion) &&
|
||||||
|
isParamEndpoint(this.options.endpoint, this.options.endpointType)
|
||||||
|
) {
|
||||||
|
responseMessage.text = '';
|
||||||
|
responseMessage.content = completion;
|
||||||
|
} else if (Array.isArray(completion)) {
|
||||||
|
responseMessage.text = addSpaceIfNeeded(generation) + completion.join('');
|
||||||
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
tokenCountMap &&
|
tokenCountMap &&
|
||||||
this.recordTokenUsage &&
|
this.recordTokenUsage &&
|
||||||
this.getTokenCountForResponse &&
|
this.getTokenCountForResponse &&
|
||||||
this.getTokenCount
|
this.getTokenCount
|
||||||
) {
|
) {
|
||||||
responseMessage.tokenCount = this.getTokenCountForResponse(responseMessage);
|
let completionTokens;
|
||||||
const completionTokens = this.getTokenCount(completion);
|
|
||||||
await this.recordTokenUsage({ promptTokens, completionTokens });
|
/**
|
||||||
|
* Metadata about input/output costs for the current message. The client
|
||||||
|
* should provide a function to get the current stream usage metadata; if not,
|
||||||
|
* use the legacy token estimations.
|
||||||
|
* @type {StreamUsage | null} */
|
||||||
|
const usage = this.getStreamUsage != null ? this.getStreamUsage() : null;
|
||||||
|
|
||||||
|
if (usage != null && Number(usage[this.outputTokensKey]) > 0) {
|
||||||
|
responseMessage.tokenCount = usage[this.outputTokensKey];
|
||||||
|
completionTokens = responseMessage.tokenCount;
|
||||||
|
await this.updateUserMessageTokenCount({ usage, tokenCountMap, userMessage, opts });
|
||||||
|
} else {
|
||||||
|
responseMessage.tokenCount = this.getTokenCountForResponse(responseMessage);
|
||||||
|
completionTokens = responseMessage.tokenCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.recordTokenUsage({ promptTokens, completionTokens, usage });
|
||||||
}
|
}
|
||||||
await this.saveMessageToDatabase(responseMessage, saveOptions, user);
|
|
||||||
|
if (this.userMessagePromise) {
|
||||||
|
await this.userMessagePromise;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.artifactPromises) {
|
||||||
|
responseMessage.attachments = (await Promise.all(this.artifactPromises)).filter((a) => a);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.options.attachments) {
|
||||||
|
try {
|
||||||
|
saveOptions.files = this.options.attachments.map((attachments) => attachments.file_id);
|
||||||
|
} catch (error) {
|
||||||
|
logger.error('[BaseClient] Error mapping attachments for conversation', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.responsePromise = this.saveMessageToDatabase(responseMessage, saveOptions, user);
|
||||||
|
this.savedMessageIds.add(responseMessage.messageId);
|
||||||
delete responseMessage.tokenCount;
|
delete responseMessage.tokenCount;
|
||||||
return responseMessage;
|
return responseMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getConversation(conversationId, user = null) {
|
/**
|
||||||
return await getConvo(user, conversationId);
|
* Stream usage should only be used for user message token count re-calculation if:
|
||||||
|
* - The stream usage is available, with input tokens greater than 0,
|
||||||
|
* - the client provides a function to calculate the current token count,
|
||||||
|
* - files are being resent with every message (default behavior; or if `false`, with no attachments),
|
||||||
|
* - the `promptPrefix` (custom instructions) is not set.
|
||||||
|
*
|
||||||
|
* In these cases, the legacy token estimations would be more accurate.
|
||||||
|
*
|
||||||
|
* TODO: included system messages in the `orderedMessages` accounting, potentially as a
|
||||||
|
* separate message in the UI. ChatGPT does this through "hidden" system messages.
|
||||||
|
* @param {object} params
|
||||||
|
* @param {StreamUsage} params.usage
|
||||||
|
* @param {Record<string, number>} params.tokenCountMap
|
||||||
|
* @param {TMessage} params.userMessage
|
||||||
|
* @param {object} params.opts
|
||||||
|
*/
|
||||||
|
async updateUserMessageTokenCount({ usage, tokenCountMap, userMessage, opts }) {
|
||||||
|
/** @type {boolean} */
|
||||||
|
const shouldUpdateCount =
|
||||||
|
this.calculateCurrentTokenCount != null &&
|
||||||
|
Number(usage[this.inputTokensKey]) > 0 &&
|
||||||
|
(this.options.resendFiles ||
|
||||||
|
(!this.options.resendFiles && !this.options.attachments?.length)) &&
|
||||||
|
!this.options.promptPrefix;
|
||||||
|
|
||||||
|
if (!shouldUpdateCount) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const userMessageTokenCount = this.calculateCurrentTokenCount({
|
||||||
|
currentMessageId: userMessage.messageId,
|
||||||
|
tokenCountMap,
|
||||||
|
usage,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (userMessageTokenCount === userMessage.tokenCount) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
userMessage.tokenCount = userMessageTokenCount;
|
||||||
|
/*
|
||||||
|
Note: `AskController` saves the user message, so we update the count of its `userMessage` reference
|
||||||
|
*/
|
||||||
|
if (typeof opts?.getReqData === 'function') {
|
||||||
|
opts.getReqData({
|
||||||
|
userMessage,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
Note: we update the user message to be sure it gets the calculated token count;
|
||||||
|
though `AskController` saves the user message, EditController does not
|
||||||
|
*/
|
||||||
|
await this.userMessagePromise;
|
||||||
|
await this.updateMessageInDatabase({
|
||||||
|
messageId: userMessage.messageId,
|
||||||
|
tokenCount: userMessageTokenCount,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async loadHistory(conversationId, parentMessageId = null) {
|
async loadHistory(conversationId, parentMessageId = null) {
|
||||||
|
|
@ -527,18 +837,52 @@ class BaseClient {
|
||||||
return _messages;
|
return _messages;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Save a message to the database.
|
||||||
|
* @param {TMessage} message
|
||||||
|
* @param {Partial<TConversation>} endpointOptions
|
||||||
|
* @param {string | null} user
|
||||||
|
*/
|
||||||
async saveMessageToDatabase(message, endpointOptions, user = null) {
|
async saveMessageToDatabase(message, endpointOptions, user = null) {
|
||||||
await saveMessage({ ...message, endpoint: this.options.endpoint, user, unfinished: false });
|
if (this.user && user !== this.user) {
|
||||||
await saveConvo(user, {
|
throw new Error('User mismatch.');
|
||||||
conversationId: message.conversationId,
|
}
|
||||||
endpoint: this.options.endpoint,
|
|
||||||
endpointType: this.options.endpointType,
|
const savedMessage = await saveMessage(
|
||||||
...endpointOptions,
|
this.options.req,
|
||||||
});
|
{
|
||||||
|
...message,
|
||||||
|
endpoint: this.options.endpoint,
|
||||||
|
unfinished: false,
|
||||||
|
user,
|
||||||
|
},
|
||||||
|
{ context: 'api/app/clients/BaseClient.js - saveMessageToDatabase #saveMessage' },
|
||||||
|
);
|
||||||
|
|
||||||
|
if (this.skipSaveConvo) {
|
||||||
|
return { message: savedMessage };
|
||||||
|
}
|
||||||
|
|
||||||
|
const conversation = await saveConvo(
|
||||||
|
this.options.req,
|
||||||
|
{
|
||||||
|
conversationId: message.conversationId,
|
||||||
|
endpoint: this.options.endpoint,
|
||||||
|
endpointType: this.options.endpointType,
|
||||||
|
...endpointOptions,
|
||||||
|
},
|
||||||
|
{ context: 'api/app/clients/BaseClient.js - saveMessageToDatabase #saveConvo' },
|
||||||
|
);
|
||||||
|
|
||||||
|
return { message: savedMessage, conversation };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update a message in the database.
|
||||||
|
* @param {Partial<TMessage>} message
|
||||||
|
*/
|
||||||
async updateMessageInDatabase(message) {
|
async updateMessageInDatabase(message) {
|
||||||
await updateMessage(message);
|
await updateMessage(this.options.req, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -558,11 +902,11 @@ class BaseClient {
|
||||||
* the message is considered a root message.
|
* the message is considered a root message.
|
||||||
*
|
*
|
||||||
* @param {Object} options - The options for the function.
|
* @param {Object} options - The options for the function.
|
||||||
* @param {Array} options.messages - An array of message objects. Each object should have either an 'id' or 'messageId' property, and may have a 'parentMessageId' property.
|
* @param {TMessage[]} options.messages - An array of message objects. Each object should have either an 'id' or 'messageId' property, and may have a 'parentMessageId' property.
|
||||||
* @param {string} options.parentMessageId - The ID of the parent message to start the traversal from.
|
* @param {string} options.parentMessageId - The ID of the parent message to start the traversal from.
|
||||||
* @param {Function} [options.mapMethod] - An optional function to map over the ordered messages. If provided, it will be applied to each message in the resulting array.
|
* @param {Function} [options.mapMethod] - An optional function to map over the ordered messages. If provided, it will be applied to each message in the resulting array.
|
||||||
* @param {boolean} [options.summary=false] - If set to true, the traversal modifies messages with 'summary' and 'summaryTokenCount' properties and stops at the message with a 'summary' property.
|
* @param {boolean} [options.summary=false] - If set to true, the traversal modifies messages with 'summary' and 'summaryTokenCount' properties and stops at the message with a 'summary' property.
|
||||||
* @returns {Array} An array containing the messages in the order they should be displayed, starting with the most recent message with a 'summary' property if the 'summary' option is true, and ending with the message identified by 'parentMessageId'.
|
* @returns {TMessage[]} An array containing the messages in the order they should be displayed, starting with the most recent message with a 'summary' property if the 'summary' option is true, and ending with the message identified by 'parentMessageId'.
|
||||||
*/
|
*/
|
||||||
static getMessagesForConversation({
|
static getMessagesForConversation({
|
||||||
messages,
|
messages,
|
||||||
|
|
@ -639,8 +983,9 @@ class BaseClient {
|
||||||
// Note: gpt-3.5-turbo and gpt-4 may update over time. Use default for these as well as for unknown models
|
// Note: gpt-3.5-turbo and gpt-4 may update over time. Use default for these as well as for unknown models
|
||||||
let tokensPerMessage = 3;
|
let tokensPerMessage = 3;
|
||||||
let tokensPerName = 1;
|
let tokensPerName = 1;
|
||||||
|
const model = this.modelOptions?.model ?? this.model;
|
||||||
|
|
||||||
if (this.modelOptions.model === 'gpt-3.5-turbo-0301') {
|
if (model === 'gpt-3.5-turbo-0301') {
|
||||||
tokensPerMessage = 4;
|
tokensPerMessage = 4;
|
||||||
tokensPerName = -1;
|
tokensPerName = -1;
|
||||||
}
|
}
|
||||||
|
|
@ -652,6 +997,24 @@ class BaseClient {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (item.type === 'tool_call' && item.tool_call != null) {
|
||||||
|
const toolName = item.tool_call?.name || '';
|
||||||
|
if (toolName != null && toolName && typeof toolName === 'string') {
|
||||||
|
numTokens += this.getTokenCount(toolName);
|
||||||
|
}
|
||||||
|
|
||||||
|
const args = item.tool_call?.args || '';
|
||||||
|
if (args != null && args && typeof args === 'string') {
|
||||||
|
numTokens += this.getTokenCount(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
const output = item.tool_call?.output || '';
|
||||||
|
if (output != null && output && typeof output === 'string') {
|
||||||
|
numTokens += this.getTokenCount(output);
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
const nestedValue = item[item.type];
|
const nestedValue = item[item.type];
|
||||||
|
|
||||||
if (!nestedValue) {
|
if (!nestedValue) {
|
||||||
|
|
@ -660,8 +1023,12 @@ class BaseClient {
|
||||||
|
|
||||||
processValue(nestedValue);
|
processValue(nestedValue);
|
||||||
}
|
}
|
||||||
} else {
|
} else if (typeof value === 'string') {
|
||||||
numTokens += this.getTokenCount(value);
|
numTokens += this.getTokenCount(value);
|
||||||
|
} else if (typeof value === 'number') {
|
||||||
|
numTokens += this.getTokenCount(value.toString());
|
||||||
|
} else if (typeof value === 'boolean') {
|
||||||
|
numTokens += this.getTokenCount(value.toString());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -683,6 +1050,75 @@ class BaseClient {
|
||||||
|
|
||||||
return await this.sendCompletion(payload, opts);
|
return await this.sendCompletion(payload, opts);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {TMessage[]} _messages
|
||||||
|
* @returns {Promise<TMessage[]>}
|
||||||
|
*/
|
||||||
|
async addPreviousAttachments(_messages) {
|
||||||
|
if (!this.options.resendFiles) {
|
||||||
|
return _messages;
|
||||||
|
}
|
||||||
|
|
||||||
|
const seen = new Set();
|
||||||
|
const attachmentsProcessed =
|
||||||
|
this.options.attachments && !(this.options.attachments instanceof Promise);
|
||||||
|
if (attachmentsProcessed) {
|
||||||
|
for (const attachment of this.options.attachments) {
|
||||||
|
seen.add(attachment.file_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {TMessage} message
|
||||||
|
*/
|
||||||
|
const processMessage = async (message) => {
|
||||||
|
if (!this.message_file_map) {
|
||||||
|
/** @type {Record<string, MongoFile[]> */
|
||||||
|
this.message_file_map = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
const fileIds = [];
|
||||||
|
for (const file of message.files) {
|
||||||
|
if (seen.has(file.file_id)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
fileIds.push(file.file_id);
|
||||||
|
seen.add(file.file_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fileIds.length === 0) {
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
const files = await getFiles({
|
||||||
|
file_id: { $in: fileIds },
|
||||||
|
});
|
||||||
|
|
||||||
|
await this.addImageURLs(message, files, this.visionMode);
|
||||||
|
|
||||||
|
this.message_file_map[message.messageId] = files;
|
||||||
|
return message;
|
||||||
|
};
|
||||||
|
|
||||||
|
const promises = [];
|
||||||
|
|
||||||
|
for (const message of _messages) {
|
||||||
|
if (!message.files) {
|
||||||
|
promises.push(message);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
promises.push(processMessage(message));
|
||||||
|
}
|
||||||
|
|
||||||
|
const messages = await Promise.all(promises);
|
||||||
|
|
||||||
|
this.checkVisionRequest(Object.values(this.message_file_map ?? {}).flat());
|
||||||
|
return messages;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = BaseClient;
|
module.exports = BaseClient;
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,20 @@
|
||||||
const Keyv = require('keyv');
|
const Keyv = require('keyv');
|
||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
|
const { CohereClient } = require('cohere-ai');
|
||||||
|
const { fetchEventSource } = require('@waylaidwanderer/fetch-event-source');
|
||||||
|
const { encoding_for_model: encodingForModel, get_encoding: getEncoding } = require('tiktoken');
|
||||||
const {
|
const {
|
||||||
|
ImageDetail,
|
||||||
EModelEndpoint,
|
EModelEndpoint,
|
||||||
resolveHeaders,
|
resolveHeaders,
|
||||||
|
CohereConstants,
|
||||||
mapModelToAzureConfig,
|
mapModelToAzureConfig,
|
||||||
} = require('librechat-data-provider');
|
} = require('librechat-data-provider');
|
||||||
const { encoding_for_model: encodingForModel, get_encoding: getEncoding } = require('tiktoken');
|
const { extractBaseURL, constructAzureURL, genAzureChatCompletion } = require('~/utils');
|
||||||
const { fetchEventSource } = require('@waylaidwanderer/fetch-event-source');
|
const { createContextHandlers } = require('./prompts');
|
||||||
const { Agent, ProxyAgent } = require('undici');
|
const { createCoherePayload } = require('./llm');
|
||||||
const BaseClient = require('./BaseClient');
|
const BaseClient = require('./BaseClient');
|
||||||
const { logger } = require('~/config');
|
const { logger } = require('~/config');
|
||||||
const { extractBaseURL, constructAzureURL, genAzureChatCompletion } = require('~/utils');
|
|
||||||
|
|
||||||
const CHATGPT_MODEL = 'gpt-3.5-turbo';
|
const CHATGPT_MODEL = 'gpt-3.5-turbo';
|
||||||
const tokenizersCache = {};
|
const tokenizersCache = {};
|
||||||
|
|
@ -147,7 +151,8 @@ class ChatGPTClient extends BaseClient {
|
||||||
return tokenizer;
|
return tokenizer;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getCompletion(input, onProgress, abortController = null) {
|
/** @type {getCompletion} */
|
||||||
|
async getCompletion(input, onProgress, onTokenProgress, abortController = null) {
|
||||||
if (!abortController) {
|
if (!abortController) {
|
||||||
abortController = new AbortController();
|
abortController = new AbortController();
|
||||||
}
|
}
|
||||||
|
|
@ -180,10 +185,6 @@ class ChatGPTClient extends BaseClient {
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
},
|
},
|
||||||
dispatcher: new Agent({
|
|
||||||
bodyTimeout: 0,
|
|
||||||
headersTimeout: 0,
|
|
||||||
}),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (this.isVisionModel) {
|
if (this.isVisionModel) {
|
||||||
|
|
@ -221,6 +222,16 @@ class ChatGPTClient extends BaseClient {
|
||||||
this.azure = !serverless && azureOptions;
|
this.azure = !serverless && azureOptions;
|
||||||
this.azureEndpoint =
|
this.azureEndpoint =
|
||||||
!serverless && genAzureChatCompletion(this.azure, modelOptions.model, this);
|
!serverless && genAzureChatCompletion(this.azure, modelOptions.model, this);
|
||||||
|
if (serverless === true) {
|
||||||
|
this.options.defaultQuery = azureOptions.azureOpenAIApiVersion
|
||||||
|
? { 'api-version': azureOptions.azureOpenAIApiVersion }
|
||||||
|
: undefined;
|
||||||
|
this.options.headers['api-key'] = this.apiKey;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.options.defaultQuery) {
|
||||||
|
opts.defaultQuery = this.options.defaultQuery;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.options.headers) {
|
if (this.options.headers) {
|
||||||
|
|
@ -234,9 +245,9 @@ class ChatGPTClient extends BaseClient {
|
||||||
baseURL = this.langchainProxy
|
baseURL = this.langchainProxy
|
||||||
? constructAzureURL({
|
? constructAzureURL({
|
||||||
baseURL: this.langchainProxy,
|
baseURL: this.langchainProxy,
|
||||||
azure: this.azure,
|
azureOptions: this.azure,
|
||||||
})
|
})
|
||||||
: this.azureEndpoint.split(/\/(chat|completion)/)[0];
|
: this.azureEndpoint.split(/(?<!\/)\/(chat|completion)\//)[0];
|
||||||
|
|
||||||
if (this.options.forcePrompt) {
|
if (this.options.forcePrompt) {
|
||||||
baseURL += '/completions';
|
baseURL += '/completions';
|
||||||
|
|
@ -259,10 +270,6 @@ class ChatGPTClient extends BaseClient {
|
||||||
opts.headers['X-Title'] = 'LibreChat';
|
opts.headers['X-Title'] = 'LibreChat';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.options.proxy) {
|
|
||||||
opts.dispatcher = new ProxyAgent(this.options.proxy);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* hacky fixes for Mistral AI API:
|
/* hacky fixes for Mistral AI API:
|
||||||
- Re-orders system message to the top of the messages payload, as not allowed anywhere else
|
- Re-orders system message to the top of the messages payload, as not allowed anywhere else
|
||||||
- If there is only one message and it's a system message, change the role to user
|
- If there is only one message and it's a system message, change the role to user
|
||||||
|
|
@ -305,6 +312,11 @@ class ChatGPTClient extends BaseClient {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (baseURL.startsWith(CohereConstants.API_URL)) {
|
||||||
|
const payload = createCoherePayload({ modelOptions });
|
||||||
|
return await this.cohereChatCompletion({ payload, onTokenProgress });
|
||||||
|
}
|
||||||
|
|
||||||
if (baseURL.includes('v1') && !baseURL.includes('/completions') && !this.isChatCompletion) {
|
if (baseURL.includes('v1') && !baseURL.includes('/completions') && !this.isChatCompletion) {
|
||||||
baseURL = baseURL.split('v1')[0] + 'v1/completions';
|
baseURL = baseURL.split('v1')[0] + 'v1/completions';
|
||||||
} else if (
|
} else if (
|
||||||
|
|
@ -408,6 +420,43 @@ class ChatGPTClient extends BaseClient {
|
||||||
return response.json();
|
return response.json();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @type {cohereChatCompletion} */
|
||||||
|
async cohereChatCompletion({ payload, onTokenProgress }) {
|
||||||
|
const cohere = new CohereClient({
|
||||||
|
token: this.apiKey,
|
||||||
|
environment: this.completionsUrl,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!payload.stream) {
|
||||||
|
const chatResponse = await cohere.chat(payload);
|
||||||
|
return chatResponse.text;
|
||||||
|
}
|
||||||
|
|
||||||
|
const chatStream = await cohere.chatStream(payload);
|
||||||
|
let reply = '';
|
||||||
|
for await (const message of chatStream) {
|
||||||
|
if (!message) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (message.eventType === 'text-generation' && message.text) {
|
||||||
|
onTokenProgress(message.text);
|
||||||
|
reply += message.text;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
Cohere API Chinese Unicode character replacement hotfix.
|
||||||
|
Should be un-commented when the following issue is resolved:
|
||||||
|
https://github.com/cohere-ai/cohere-typescript/issues/151
|
||||||
|
|
||||||
|
else if (message.eventType === 'stream-end' && message.response) {
|
||||||
|
reply = message.response.text;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
return reply;
|
||||||
|
}
|
||||||
|
|
||||||
async generateTitle(userMessage, botMessage) {
|
async generateTitle(userMessage, botMessage) {
|
||||||
const instructionsPayload = {
|
const instructionsPayload = {
|
||||||
role: 'system',
|
role: 'system',
|
||||||
|
|
@ -566,26 +615,70 @@ ${botMessage.message}
|
||||||
|
|
||||||
async buildPrompt(messages, { isChatGptModel = false, promptPrefix = null }) {
|
async buildPrompt(messages, { isChatGptModel = false, promptPrefix = null }) {
|
||||||
promptPrefix = (promptPrefix || this.options.promptPrefix || '').trim();
|
promptPrefix = (promptPrefix || this.options.promptPrefix || '').trim();
|
||||||
|
|
||||||
|
// Handle attachments and create augmentedPrompt
|
||||||
|
if (this.options.attachments) {
|
||||||
|
const attachments = await this.options.attachments;
|
||||||
|
const lastMessage = messages[messages.length - 1];
|
||||||
|
|
||||||
|
if (this.message_file_map) {
|
||||||
|
this.message_file_map[lastMessage.messageId] = attachments;
|
||||||
|
} else {
|
||||||
|
this.message_file_map = {
|
||||||
|
[lastMessage.messageId]: attachments,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const files = await this.addImageURLs(lastMessage, attachments);
|
||||||
|
this.options.attachments = files;
|
||||||
|
|
||||||
|
this.contextHandlers = createContextHandlers(this.options.req, lastMessage.text);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.message_file_map) {
|
||||||
|
this.contextHandlers = createContextHandlers(
|
||||||
|
this.options.req,
|
||||||
|
messages[messages.length - 1].text,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate image token cost and process embedded files
|
||||||
|
messages.forEach((message, i) => {
|
||||||
|
if (this.message_file_map && this.message_file_map[message.messageId]) {
|
||||||
|
const attachments = this.message_file_map[message.messageId];
|
||||||
|
for (const file of attachments) {
|
||||||
|
if (file.embedded) {
|
||||||
|
this.contextHandlers?.processFile(file);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
messages[i].tokenCount =
|
||||||
|
(messages[i].tokenCount || 0) +
|
||||||
|
this.calculateImageTokenCost({
|
||||||
|
width: file.width,
|
||||||
|
height: file.height,
|
||||||
|
detail: this.options.imageDetail ?? ImageDetail.auto,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (this.contextHandlers) {
|
||||||
|
this.augmentedPrompt = await this.contextHandlers.createContext();
|
||||||
|
promptPrefix = this.augmentedPrompt + promptPrefix;
|
||||||
|
}
|
||||||
|
|
||||||
if (promptPrefix) {
|
if (promptPrefix) {
|
||||||
// If the prompt prefix doesn't end with the end token, add it.
|
// If the prompt prefix doesn't end with the end token, add it.
|
||||||
if (!promptPrefix.endsWith(`${this.endToken}`)) {
|
if (!promptPrefix.endsWith(`${this.endToken}`)) {
|
||||||
promptPrefix = `${promptPrefix.trim()}${this.endToken}\n\n`;
|
promptPrefix = `${promptPrefix.trim()}${this.endToken}\n\n`;
|
||||||
}
|
}
|
||||||
promptPrefix = `${this.startToken}Instructions:\n${promptPrefix}`;
|
promptPrefix = `${this.startToken}Instructions:\n${promptPrefix}`;
|
||||||
} else {
|
|
||||||
const currentDateString = new Date().toLocaleDateString('en-us', {
|
|
||||||
year: 'numeric',
|
|
||||||
month: 'long',
|
|
||||||
day: 'numeric',
|
|
||||||
});
|
|
||||||
promptPrefix = `${this.startToken}Instructions:\nYou are ChatGPT, a large language model trained by OpenAI. Respond conversationally.\nCurrent date: ${currentDateString}${this.endToken}\n\n`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const promptSuffix = `${this.startToken}${this.chatGptLabel}:\n`; // Prompt ChatGPT to respond.
|
const promptSuffix = `${this.startToken}${this.chatGptLabel}:\n`; // Prompt ChatGPT to respond.
|
||||||
|
|
||||||
const instructionsPayload = {
|
const instructionsPayload = {
|
||||||
role: 'system',
|
role: 'system',
|
||||||
name: 'instructions',
|
|
||||||
content: promptPrefix,
|
content: promptPrefix,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -668,10 +761,6 @@ ${botMessage.message}
|
||||||
this.maxResponseTokens,
|
this.maxResponseTokens,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (this.options.debug) {
|
|
||||||
console.debug(`Prompt : ${prompt}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isChatGptModel) {
|
if (isChatGptModel) {
|
||||||
return { prompt: [instructionsPayload, messagePayload], context };
|
return { prompt: [instructionsPayload, messagePayload], context };
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,30 +1,42 @@
|
||||||
const { google } = require('googleapis');
|
const { google } = require('googleapis');
|
||||||
const { Agent, ProxyAgent } = require('undici');
|
const { concat } = require('@langchain/core/utils/stream');
|
||||||
const { GoogleVertexAI } = require('langchain/llms/googlevertexai');
|
const { ChatVertexAI } = require('@langchain/google-vertexai');
|
||||||
const { ChatGoogleGenerativeAI } = require('@langchain/google-genai');
|
const { ChatGoogleGenerativeAI } = require('@langchain/google-genai');
|
||||||
const { ChatGoogleVertexAI } = require('langchain/chat_models/googlevertexai');
|
const { GoogleGenerativeAI: GenAI } = require('@google/generative-ai');
|
||||||
const { AIMessage, HumanMessage, SystemMessage } = require('langchain/schema');
|
const { HumanMessage, SystemMessage } = require('@langchain/core/messages');
|
||||||
const { encoding_for_model: encodingForModel, get_encoding: getEncoding } = require('tiktoken');
|
|
||||||
const {
|
const {
|
||||||
|
googleGenConfigSchema,
|
||||||
validateVisionModel,
|
validateVisionModel,
|
||||||
getResponseSender,
|
getResponseSender,
|
||||||
endpointSettings,
|
endpointSettings,
|
||||||
EModelEndpoint,
|
EModelEndpoint,
|
||||||
|
ContentTypes,
|
||||||
|
VisionModes,
|
||||||
|
ErrorTypes,
|
||||||
|
Constants,
|
||||||
AuthKeys,
|
AuthKeys,
|
||||||
} = require('librechat-data-provider');
|
} = require('librechat-data-provider');
|
||||||
|
const { getSafetySettings } = require('~/server/services/Endpoints/google/llm');
|
||||||
const { encodeAndFormat } = require('~/server/services/Files/images');
|
const { encodeAndFormat } = require('~/server/services/Files/images');
|
||||||
|
const Tokenizer = require('~/server/services/Tokenizer');
|
||||||
|
const { spendTokens } = require('~/models/spendTokens');
|
||||||
const { getModelMaxTokens } = require('~/utils');
|
const { getModelMaxTokens } = require('~/utils');
|
||||||
const { formatMessage } = require('./prompts');
|
const { sleep } = require('~/server/utils');
|
||||||
const BaseClient = require('./BaseClient');
|
|
||||||
const { logger } = require('~/config');
|
const { logger } = require('~/config');
|
||||||
|
const {
|
||||||
|
formatMessage,
|
||||||
|
createContextHandlers,
|
||||||
|
titleInstruction,
|
||||||
|
truncateText,
|
||||||
|
} = require('./prompts');
|
||||||
|
const BaseClient = require('./BaseClient');
|
||||||
|
|
||||||
const loc = 'us-central1';
|
const loc = process.env.GOOGLE_LOC || 'us-central1';
|
||||||
const publisher = 'google';
|
const publisher = 'google';
|
||||||
const endpointPrefix = `https://${loc}-aiplatform.googleapis.com`;
|
const endpointPrefix = `${loc}-aiplatform.googleapis.com`;
|
||||||
// const apiEndpoint = loc + '-aiplatform.googleapis.com';
|
|
||||||
const tokenizersCache = {};
|
|
||||||
|
|
||||||
const settings = endpointSettings[EModelEndpoint.google];
|
const settings = endpointSettings[EModelEndpoint.google];
|
||||||
|
const EXCLUDED_GENAI_MODELS = /gemini-(?:1\.0|1-0|pro)/;
|
||||||
|
|
||||||
class GoogleClient extends BaseClient {
|
class GoogleClient extends BaseClient {
|
||||||
constructor(credentials, options = {}) {
|
constructor(credentials, options = {}) {
|
||||||
|
|
@ -40,13 +52,27 @@ class GoogleClient extends BaseClient {
|
||||||
const serviceKey = creds[AuthKeys.GOOGLE_SERVICE_KEY] ?? {};
|
const serviceKey = creds[AuthKeys.GOOGLE_SERVICE_KEY] ?? {};
|
||||||
this.serviceKey =
|
this.serviceKey =
|
||||||
serviceKey && typeof serviceKey === 'string' ? JSON.parse(serviceKey) : serviceKey ?? {};
|
serviceKey && typeof serviceKey === 'string' ? JSON.parse(serviceKey) : serviceKey ?? {};
|
||||||
|
/** @type {string | null | undefined} */
|
||||||
|
this.project_id = this.serviceKey.project_id;
|
||||||
this.client_email = this.serviceKey.client_email;
|
this.client_email = this.serviceKey.client_email;
|
||||||
this.private_key = this.serviceKey.private_key;
|
this.private_key = this.serviceKey.private_key;
|
||||||
this.project_id = this.serviceKey.project_id;
|
|
||||||
this.access_token = null;
|
this.access_token = null;
|
||||||
|
|
||||||
this.apiKey = creds[AuthKeys.GOOGLE_API_KEY];
|
this.apiKey = creds[AuthKeys.GOOGLE_API_KEY];
|
||||||
|
|
||||||
|
this.reverseProxyUrl = options.reverseProxyUrl;
|
||||||
|
|
||||||
|
this.authHeader = options.authHeader;
|
||||||
|
|
||||||
|
/** @type {UsageMetadata | undefined} */
|
||||||
|
this.usage;
|
||||||
|
/** The key for the usage object's input tokens
|
||||||
|
* @type {string} */
|
||||||
|
this.inputTokensKey = 'input_tokens';
|
||||||
|
/** The key for the usage object's output tokens
|
||||||
|
* @type {string} */
|
||||||
|
this.outputTokensKey = 'output_tokens';
|
||||||
|
this.visionMode = VisionModes.generative;
|
||||||
if (options.skipSetOptions) {
|
if (options.skipSetOptions) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -55,7 +81,7 @@ class GoogleClient extends BaseClient {
|
||||||
|
|
||||||
/* Google specific methods */
|
/* Google specific methods */
|
||||||
constructUrl() {
|
constructUrl() {
|
||||||
return `${endpointPrefix}/v1/projects/${this.project_id}/locations/${loc}/publishers/${publisher}/models/${this.modelOptions.model}:serverStreamingPredict`;
|
return `https://${endpointPrefix}/v1/projects/${this.project_id}/locations/${loc}/publishers/${publisher}/models/${this.modelOptions.model}:serverStreamingPredict`;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getClient() {
|
async getClient() {
|
||||||
|
|
@ -106,53 +132,18 @@ class GoogleClient extends BaseClient {
|
||||||
this.options = options;
|
this.options = options;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.options.examples = (this.options.examples ?? [])
|
this.modelOptions = this.options.modelOptions || {};
|
||||||
.filter((ex) => ex)
|
|
||||||
.filter((obj) => obj.input.content !== '' && obj.output.content !== '');
|
|
||||||
|
|
||||||
const modelOptions = this.options.modelOptions || {};
|
this.options.attachments?.then((attachments) => this.checkVisionRequest(attachments));
|
||||||
this.modelOptions = {
|
|
||||||
...modelOptions,
|
|
||||||
// set some good defaults (check for undefined in some cases because they may be 0)
|
|
||||||
model: modelOptions.model || settings.model.default,
|
|
||||||
temperature:
|
|
||||||
typeof modelOptions.temperature === 'undefined'
|
|
||||||
? settings.temperature.default
|
|
||||||
: modelOptions.temperature,
|
|
||||||
topP: typeof modelOptions.topP === 'undefined' ? settings.topP.default : modelOptions.topP,
|
|
||||||
topK: typeof modelOptions.topK === 'undefined' ? settings.topK.default : modelOptions.topK,
|
|
||||||
// stop: modelOptions.stop // no stop method for now
|
|
||||||
};
|
|
||||||
|
|
||||||
/* Validation vision request */
|
/** @type {boolean} Whether using a "GenerativeAI" Model */
|
||||||
this.defaultVisionModel = this.options.visionModel ?? 'gemini-pro-vision';
|
this.isGenerativeModel =
|
||||||
const availableModels = this.options.modelsConfig?.[EModelEndpoint.google];
|
this.modelOptions.model.includes('gemini') || this.modelOptions.model.includes('learnlm');
|
||||||
this.isVisionModel = validateVisionModel({ model: this.modelOptions.model, availableModels });
|
|
||||||
|
|
||||||
if (
|
this.maxContextTokens =
|
||||||
this.options.attachments &&
|
this.options.maxContextTokens ??
|
||||||
availableModels?.includes(this.defaultVisionModel) &&
|
getModelMaxTokens(this.modelOptions.model, EModelEndpoint.google);
|
||||||
!this.isVisionModel
|
|
||||||
) {
|
|
||||||
this.modelOptions.model = this.defaultVisionModel;
|
|
||||||
this.isVisionModel = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.isVisionModel && !this.options.attachments) {
|
|
||||||
this.modelOptions.model = 'gemini-pro';
|
|
||||||
this.isVisionModel = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: as of 12/14/23, only gemini models are "Generative AI" models provided by Google
|
|
||||||
this.isGenerativeModel = this.modelOptions.model.includes('gemini');
|
|
||||||
const { isGenerativeModel } = this;
|
|
||||||
this.isChatModel = !isGenerativeModel && this.modelOptions.model.includes('chat');
|
|
||||||
const { isChatModel } = this;
|
|
||||||
this.isTextModel =
|
|
||||||
!isGenerativeModel && !isChatModel && /code|text/.test(this.modelOptions.model);
|
|
||||||
const { isTextModel } = this;
|
|
||||||
|
|
||||||
this.maxContextTokens = getModelMaxTokens(this.modelOptions.model, EModelEndpoint.google);
|
|
||||||
// The max prompt tokens is determined by the max context tokens minus the max response tokens.
|
// The max prompt tokens is determined by the max context tokens minus the max response tokens.
|
||||||
// Earlier messages will be dropped until the prompt is within the limit.
|
// Earlier messages will be dropped until the prompt is within the limit.
|
||||||
this.maxResponseTokens = this.modelOptions.maxOutputTokens || settings.maxOutputTokens.default;
|
this.maxResponseTokens = this.modelOptions.maxOutputTokens || settings.maxOutputTokens.default;
|
||||||
|
|
@ -183,72 +174,159 @@ class GoogleClient extends BaseClient {
|
||||||
this.userLabel = this.options.userLabel || 'User';
|
this.userLabel = this.options.userLabel || 'User';
|
||||||
this.modelLabel = this.options.modelLabel || 'Assistant';
|
this.modelLabel = this.options.modelLabel || 'Assistant';
|
||||||
|
|
||||||
if (isChatModel || isGenerativeModel) {
|
|
||||||
// Use these faux tokens to help the AI understand the context since we are building the chat log ourselves.
|
|
||||||
// Trying to use "<|im_start|>" causes the AI to still generate "<" or "<|" at the end sometimes for some reason,
|
|
||||||
// without tripping the stop sequences, so I'm using "||>" instead.
|
|
||||||
this.startToken = '||>';
|
|
||||||
this.endToken = '';
|
|
||||||
this.gptEncoder = this.constructor.getTokenizer('cl100k_base');
|
|
||||||
} else if (isTextModel) {
|
|
||||||
this.startToken = '||>';
|
|
||||||
this.endToken = '';
|
|
||||||
this.gptEncoder = this.constructor.getTokenizer('text-davinci-003', true, {
|
|
||||||
'<|im_start|>': 100264,
|
|
||||||
'<|im_end|>': 100265,
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
// Previously I was trying to use "<|endoftext|>" but there seems to be some bug with OpenAI's token counting
|
|
||||||
// system that causes only the first "<|endoftext|>" to be counted as 1 token, and the rest are not treated
|
|
||||||
// as a single token. So we're using this instead.
|
|
||||||
this.startToken = '||>';
|
|
||||||
this.endToken = '';
|
|
||||||
try {
|
|
||||||
this.gptEncoder = this.constructor.getTokenizer(this.modelOptions.model, true);
|
|
||||||
} catch {
|
|
||||||
this.gptEncoder = this.constructor.getTokenizer('text-davinci-003', true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!this.modelOptions.stop) {
|
|
||||||
const stopTokens = [this.startToken];
|
|
||||||
if (this.endToken && this.endToken !== this.startToken) {
|
|
||||||
stopTokens.push(this.endToken);
|
|
||||||
}
|
|
||||||
stopTokens.push(`\n${this.userLabel}:`);
|
|
||||||
stopTokens.push('<|diff_marker|>');
|
|
||||||
// I chose not to do one for `modelLabel` because I've never seen it happen
|
|
||||||
this.modelOptions.stop = stopTokens;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.options.reverseProxyUrl) {
|
if (this.options.reverseProxyUrl) {
|
||||||
this.completionsUrl = this.options.reverseProxyUrl;
|
this.completionsUrl = this.options.reverseProxyUrl;
|
||||||
} else {
|
} else {
|
||||||
this.completionsUrl = this.constructUrl();
|
this.completionsUrl = this.constructUrl();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let promptPrefix = (this.options.promptPrefix ?? '').trim();
|
||||||
|
if (typeof this.options.artifactsPrompt === 'string' && this.options.artifactsPrompt) {
|
||||||
|
promptPrefix = `${promptPrefix ?? ''}\n${this.options.artifactsPrompt}`.trim();
|
||||||
|
}
|
||||||
|
this.options.promptPrefix = promptPrefix;
|
||||||
|
this.initializeClient();
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Checks if the model is a vision model based on request attachments and sets the appropriate options:
|
||||||
|
* @param {MongoFile[]} attachments
|
||||||
|
*/
|
||||||
|
checkVisionRequest(attachments) {
|
||||||
|
/* Validation vision request */
|
||||||
|
this.defaultVisionModel = this.options.visionModel ?? 'gemini-pro-vision';
|
||||||
|
const availableModels = this.options.modelsConfig?.[EModelEndpoint.google];
|
||||||
|
this.isVisionModel = validateVisionModel({ model: this.modelOptions.model, availableModels });
|
||||||
|
|
||||||
|
if (
|
||||||
|
attachments &&
|
||||||
|
attachments.some((file) => file?.type && file?.type?.includes('image')) &&
|
||||||
|
availableModels?.includes(this.defaultVisionModel) &&
|
||||||
|
!this.isVisionModel
|
||||||
|
) {
|
||||||
|
this.modelOptions.model = this.defaultVisionModel;
|
||||||
|
this.isVisionModel = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.isVisionModel && !attachments && this.modelOptions.model.includes('gemini-pro')) {
|
||||||
|
this.modelOptions.model = 'gemini-pro';
|
||||||
|
this.isVisionModel = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
formatMessages() {
|
formatMessages() {
|
||||||
return ((message) => ({
|
return ((message) => {
|
||||||
author: message?.author ?? (message.isCreatedByUser ? this.userLabel : this.modelLabel),
|
const msg = {
|
||||||
content: message?.content ?? message.text,
|
author: message?.author ?? (message.isCreatedByUser ? this.userLabel : this.modelLabel),
|
||||||
})).bind(this);
|
content: message?.content ?? message.text,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!message.image_urls?.length) {
|
||||||
|
return msg;
|
||||||
|
}
|
||||||
|
|
||||||
|
msg.content = (
|
||||||
|
!Array.isArray(msg.content)
|
||||||
|
? [
|
||||||
|
{
|
||||||
|
type: ContentTypes.TEXT,
|
||||||
|
[ContentTypes.TEXT]: msg.content,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
: msg.content
|
||||||
|
).concat(message.image_urls);
|
||||||
|
|
||||||
|
return msg;
|
||||||
|
}).bind(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Formats messages for generative AI
|
||||||
|
* @param {TMessage[]} messages
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
async formatGenerativeMessages(messages) {
|
||||||
|
const formattedMessages = [];
|
||||||
|
const attachments = await this.options.attachments;
|
||||||
|
const latestMessage = { ...messages[messages.length - 1] };
|
||||||
|
const files = await this.addImageURLs(latestMessage, attachments, VisionModes.generative);
|
||||||
|
this.options.attachments = files;
|
||||||
|
messages[messages.length - 1] = latestMessage;
|
||||||
|
|
||||||
|
for (const _message of messages) {
|
||||||
|
const role = _message.isCreatedByUser ? this.userLabel : this.modelLabel;
|
||||||
|
const parts = [];
|
||||||
|
parts.push({ text: _message.text });
|
||||||
|
if (!_message.image_urls?.length) {
|
||||||
|
formattedMessages.push({ role, parts });
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const images of _message.image_urls) {
|
||||||
|
if (images.inlineData) {
|
||||||
|
parts.push({ inlineData: images.inlineData });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
formattedMessages.push({ role, parts });
|
||||||
|
}
|
||||||
|
|
||||||
|
return formattedMessages;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Adds image URLs to the message object and returns the files
|
||||||
|
*
|
||||||
|
* @param {TMessage[]} messages
|
||||||
|
* @param {MongoFile[]} files
|
||||||
|
* @returns {Promise<MongoFile[]>}
|
||||||
|
*/
|
||||||
|
async addImageURLs(message, attachments, mode = '') {
|
||||||
|
const { files, image_urls } = await encodeAndFormat(
|
||||||
|
this.options.req,
|
||||||
|
attachments,
|
||||||
|
EModelEndpoint.google,
|
||||||
|
mode,
|
||||||
|
);
|
||||||
|
message.image_urls = image_urls.length ? image_urls : undefined;
|
||||||
|
return files;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds the augmented prompt for attachments
|
||||||
|
* TODO: Add File API Support
|
||||||
|
* @param {TMessage[]} messages
|
||||||
|
*/
|
||||||
|
async buildAugmentedPrompt(messages = []) {
|
||||||
|
const attachments = await this.options.attachments;
|
||||||
|
const latestMessage = { ...messages[messages.length - 1] };
|
||||||
|
this.contextHandlers = createContextHandlers(this.options.req, latestMessage.text);
|
||||||
|
|
||||||
|
if (this.contextHandlers) {
|
||||||
|
for (const file of attachments) {
|
||||||
|
if (file.embedded) {
|
||||||
|
this.contextHandlers?.processFile(file);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.augmentedPrompt = await this.contextHandlers.createContext();
|
||||||
|
this.options.promptPrefix = this.augmentedPrompt + this.options.promptPrefix;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async buildVisionMessages(messages = [], parentMessageId) {
|
async buildVisionMessages(messages = [], parentMessageId) {
|
||||||
const { prompt } = await this.buildMessagesPrompt(messages, parentMessageId);
|
|
||||||
const attachments = await this.options.attachments;
|
const attachments = await this.options.attachments;
|
||||||
const { files, image_urls } = await encodeAndFormat(
|
|
||||||
this.options.req,
|
|
||||||
attachments.filter((file) => file.type.includes('image')),
|
|
||||||
EModelEndpoint.google,
|
|
||||||
);
|
|
||||||
|
|
||||||
const latestMessage = { ...messages[messages.length - 1] };
|
const latestMessage = { ...messages[messages.length - 1] };
|
||||||
|
await this.buildAugmentedPrompt(messages);
|
||||||
|
|
||||||
|
const { prompt } = await this.buildMessagesPrompt(messages, parentMessageId);
|
||||||
|
|
||||||
|
const files = await this.addImageURLs(latestMessage, attachments);
|
||||||
|
|
||||||
latestMessage.image_urls = image_urls;
|
|
||||||
this.options.attachments = files;
|
this.options.attachments = files;
|
||||||
|
|
||||||
latestMessage.text = prompt;
|
latestMessage.text = prompt;
|
||||||
|
|
@ -259,28 +337,73 @@ class GoogleClient extends BaseClient {
|
||||||
messages: [new HumanMessage(formatMessage({ message: latestMessage }))],
|
messages: [new HumanMessage(formatMessage({ message: latestMessage }))],
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
parameters: this.modelOptions,
|
|
||||||
};
|
};
|
||||||
return { prompt: payload };
|
return { prompt: payload };
|
||||||
}
|
}
|
||||||
|
|
||||||
async buildMessages(messages = [], parentMessageId) {
|
/** @param {TMessage[]} [messages=[]] */
|
||||||
|
async buildGenerativeMessages(messages = []) {
|
||||||
|
this.userLabel = 'user';
|
||||||
|
this.modelLabel = 'model';
|
||||||
|
const promises = [];
|
||||||
|
promises.push(await this.formatGenerativeMessages(messages));
|
||||||
|
promises.push(this.buildAugmentedPrompt(messages));
|
||||||
|
const [formattedMessages] = await Promise.all(promises);
|
||||||
|
return { prompt: formattedMessages };
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {TMessage[]} [messages=[]]
|
||||||
|
* @param {string} [parentMessageId]
|
||||||
|
*/
|
||||||
|
async buildMessages(_messages = [], parentMessageId) {
|
||||||
if (!this.isGenerativeModel && !this.project_id) {
|
if (!this.isGenerativeModel && !this.project_id) {
|
||||||
throw new Error(
|
throw new Error('[GoogleClient] PaLM 2 and Codey models are no longer supported.');
|
||||||
'[GoogleClient] a Service Account JSON Key is required for PaLM 2 and Codey models (Vertex AI)',
|
|
||||||
);
|
|
||||||
} else if (this.isGenerativeModel && (!this.apiKey || this.apiKey === 'user_provided')) {
|
|
||||||
throw new Error(
|
|
||||||
'[GoogleClient] an API Key is required for Gemini models (Generative Language API)',
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.options.attachments) {
|
if (this.options.promptPrefix) {
|
||||||
return this.buildVisionMessages(messages, parentMessageId);
|
const instructionsTokenCount = this.getTokenCount(this.options.promptPrefix);
|
||||||
|
|
||||||
|
this.maxContextTokens = this.maxContextTokens - instructionsTokenCount;
|
||||||
|
if (this.maxContextTokens < 0) {
|
||||||
|
const info = `${instructionsTokenCount} / ${this.maxContextTokens}`;
|
||||||
|
const errorMessage = `{ "type": "${ErrorTypes.INPUT_LENGTH}", "info": "${info}" }`;
|
||||||
|
logger.warn(`Instructions token count exceeds max context (${info}).`);
|
||||||
|
throw new Error(errorMessage);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.isTextModel) {
|
for (let i = 0; i < _messages.length; i++) {
|
||||||
return this.buildMessagesPrompt(messages, parentMessageId);
|
const message = _messages[i];
|
||||||
|
if (!message.tokenCount) {
|
||||||
|
_messages[i].tokenCount = this.getTokenCountForMessage({
|
||||||
|
role: message.isCreatedByUser ? 'user' : 'assistant',
|
||||||
|
content: message.content ?? message.text,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const {
|
||||||
|
payload: messages,
|
||||||
|
tokenCountMap,
|
||||||
|
promptTokens,
|
||||||
|
} = await this.handleContextStrategy({
|
||||||
|
orderedMessages: _messages,
|
||||||
|
formattedMessages: _messages,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!this.project_id && !EXCLUDED_GENAI_MODELS.test(this.modelOptions.model)) {
|
||||||
|
const result = await this.buildGenerativeMessages(messages);
|
||||||
|
result.tokenCountMap = tokenCountMap;
|
||||||
|
result.promptTokens = promptTokens;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.options.attachments && this.isGenerativeModel) {
|
||||||
|
const result = this.buildVisionMessages(messages, parentMessageId);
|
||||||
|
result.tokenCountMap = tokenCountMap;
|
||||||
|
result.promptTokens = promptTokens;
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
let payload = {
|
let payload = {
|
||||||
|
|
@ -292,20 +415,14 @@ class GoogleClient extends BaseClient {
|
||||||
.map((message) => formatMessage({ message, langChain: true })),
|
.map((message) => formatMessage({ message, langChain: true })),
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
parameters: this.modelOptions,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (this.options.promptPrefix) {
|
if (this.options.promptPrefix) {
|
||||||
payload.instances[0].context = this.options.promptPrefix;
|
payload.instances[0].context = this.options.promptPrefix;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.options.examples.length > 0) {
|
|
||||||
payload.instances[0].examples = this.options.examples;
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.debug('[GoogleClient] buildMessages', payload);
|
logger.debug('[GoogleClient] buildMessages', payload);
|
||||||
|
return { prompt: payload, tokenCountMap, promptTokens };
|
||||||
return { prompt: payload };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async buildMessagesPrompt(messages, parentMessageId) {
|
async buildMessagesPrompt(messages, parentMessageId) {
|
||||||
|
|
@ -319,10 +436,7 @@ class GoogleClient extends BaseClient {
|
||||||
parentMessageId,
|
parentMessageId,
|
||||||
});
|
});
|
||||||
|
|
||||||
const formattedMessages = orderedMessages.map((message) => ({
|
const formattedMessages = orderedMessages.map(this.formatMessages());
|
||||||
author: message.isCreatedByUser ? this.userLabel : this.modelLabel,
|
|
||||||
content: message?.content ?? message.text,
|
|
||||||
}));
|
|
||||||
|
|
||||||
let lastAuthor = '';
|
let lastAuthor = '';
|
||||||
let groupedMessages = [];
|
let groupedMessages = [];
|
||||||
|
|
@ -350,14 +464,7 @@ class GoogleClient extends BaseClient {
|
||||||
identityPrefix = `${identityPrefix}\nYou are ${this.options.modelLabel}`;
|
identityPrefix = `${identityPrefix}\nYou are ${this.options.modelLabel}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
let promptPrefix = (this.options.promptPrefix || '').trim();
|
let promptPrefix = (this.options.promptPrefix ?? '').trim();
|
||||||
if (promptPrefix) {
|
|
||||||
// If the prompt prefix doesn't end with the end token, add it.
|
|
||||||
if (!promptPrefix.endsWith(`${this.endToken}`)) {
|
|
||||||
promptPrefix = `${promptPrefix.trim()}${this.endToken}\n\n`;
|
|
||||||
}
|
|
||||||
promptPrefix = `\nContext:\n${promptPrefix}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (identityPrefix) {
|
if (identityPrefix) {
|
||||||
promptPrefix = `${identityPrefix}${promptPrefix}`;
|
promptPrefix = `${identityPrefix}${promptPrefix}`;
|
||||||
|
|
@ -394,7 +501,7 @@ class GoogleClient extends BaseClient {
|
||||||
isCreatedByUser || !isEdited
|
isCreatedByUser || !isEdited
|
||||||
? `\n\n${message.author}:`
|
? `\n\n${message.author}:`
|
||||||
: `${promptPrefix}\n\n${message.author}:`;
|
: `${promptPrefix}\n\n${message.author}:`;
|
||||||
const messageString = `${messagePrefix}\n${message.content}${this.endToken}\n`;
|
const messageString = `${messagePrefix}\n${message.content}\n`;
|
||||||
let newPromptBody = `${messageString}${promptBody}`;
|
let newPromptBody = `${messageString}${promptBody}`;
|
||||||
|
|
||||||
context.unshift(message);
|
context.unshift(message);
|
||||||
|
|
@ -460,54 +567,50 @@ class GoogleClient extends BaseClient {
|
||||||
return { prompt, context };
|
return { prompt, context };
|
||||||
}
|
}
|
||||||
|
|
||||||
async _getCompletion(payload, abortController = null) {
|
|
||||||
if (!abortController) {
|
|
||||||
abortController = new AbortController();
|
|
||||||
}
|
|
||||||
const { debug } = this.options;
|
|
||||||
const url = this.completionsUrl;
|
|
||||||
if (debug) {
|
|
||||||
logger.debug('GoogleClient _getCompletion', { url, payload });
|
|
||||||
}
|
|
||||||
const opts = {
|
|
||||||
method: 'POST',
|
|
||||||
agent: new Agent({
|
|
||||||
bodyTimeout: 0,
|
|
||||||
headersTimeout: 0,
|
|
||||||
}),
|
|
||||||
signal: abortController.signal,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (this.options.proxy) {
|
|
||||||
opts.agent = new ProxyAgent(this.options.proxy);
|
|
||||||
}
|
|
||||||
|
|
||||||
const client = await this.getClient();
|
|
||||||
const res = await client.request({ url, method: 'POST', data: payload });
|
|
||||||
logger.debug('GoogleClient _getCompletion', { res });
|
|
||||||
return res.data;
|
|
||||||
}
|
|
||||||
|
|
||||||
createLLM(clientOptions) {
|
createLLM(clientOptions) {
|
||||||
if (this.isGenerativeModel) {
|
const model = clientOptions.modelName ?? clientOptions.model;
|
||||||
return new ChatGoogleGenerativeAI({ ...clientOptions, apiKey: this.apiKey });
|
clientOptions.location = loc;
|
||||||
|
clientOptions.endpoint = endpointPrefix;
|
||||||
|
|
||||||
|
let requestOptions = null;
|
||||||
|
if (this.reverseProxyUrl) {
|
||||||
|
requestOptions = {
|
||||||
|
baseUrl: this.reverseProxyUrl,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (this.authHeader) {
|
||||||
|
requestOptions.customHeaders = {
|
||||||
|
Authorization: `Bearer ${this.apiKey}`,
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.isTextModel
|
if (this.project_id != null) {
|
||||||
? new GoogleVertexAI(clientOptions)
|
logger.debug('Creating VertexAI client');
|
||||||
: new ChatGoogleVertexAI(clientOptions);
|
this.visionMode = undefined;
|
||||||
|
clientOptions.streaming = true;
|
||||||
|
const client = new ChatVertexAI(clientOptions);
|
||||||
|
client.temperature = clientOptions.temperature;
|
||||||
|
client.topP = clientOptions.topP;
|
||||||
|
client.topK = clientOptions.topK;
|
||||||
|
client.topLogprobs = clientOptions.topLogprobs;
|
||||||
|
client.frequencyPenalty = clientOptions.frequencyPenalty;
|
||||||
|
client.presencePenalty = clientOptions.presencePenalty;
|
||||||
|
client.maxOutputTokens = clientOptions.maxOutputTokens;
|
||||||
|
return client;
|
||||||
|
} else if (!EXCLUDED_GENAI_MODELS.test(model)) {
|
||||||
|
logger.debug('Creating GenAI client');
|
||||||
|
return new GenAI(this.apiKey).getGenerativeModel({ model }, requestOptions);
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.debug('Creating Chat Google Generative AI client');
|
||||||
|
return new ChatGoogleGenerativeAI({ ...clientOptions, apiKey: this.apiKey });
|
||||||
}
|
}
|
||||||
|
|
||||||
async getCompletion(_payload, options = {}) {
|
initializeClient() {
|
||||||
const { onProgress, abortController } = options;
|
let clientOptions = { ...this.modelOptions };
|
||||||
const { parameters, instances } = _payload;
|
|
||||||
const { messages: _messages, context, examples: _examples } = instances?.[0] ?? {};
|
|
||||||
|
|
||||||
let examples;
|
if (this.project_id) {
|
||||||
|
|
||||||
let clientOptions = { ...parameters, maxRetries: 2 };
|
|
||||||
|
|
||||||
if (!this.isGenerativeModel) {
|
|
||||||
clientOptions['authOptions'] = {
|
clientOptions['authOptions'] = {
|
||||||
credentials: {
|
credentials: {
|
||||||
...this.serviceKey,
|
...this.serviceKey,
|
||||||
|
|
@ -516,60 +619,284 @@ class GoogleClient extends BaseClient {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!parameters) {
|
if (this.isGenerativeModel && !this.project_id) {
|
||||||
clientOptions = { ...clientOptions, ...this.modelOptions };
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.isGenerativeModel) {
|
|
||||||
clientOptions.modelName = clientOptions.model;
|
clientOptions.modelName = clientOptions.model;
|
||||||
delete clientOptions.model;
|
delete clientOptions.model;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_examples && _examples.length) {
|
this.client = this.createLLM(clientOptions);
|
||||||
examples = _examples
|
return this.client;
|
||||||
.map((ex) => {
|
}
|
||||||
const { input, output } = ex;
|
|
||||||
if (!input || !output) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
input: new HumanMessage(input.content),
|
|
||||||
output: new AIMessage(output.content),
|
|
||||||
};
|
|
||||||
})
|
|
||||||
.filter((ex) => ex);
|
|
||||||
|
|
||||||
clientOptions.examples = examples;
|
async getCompletion(_payload, options = {}) {
|
||||||
}
|
const { onProgress, abortController } = options;
|
||||||
|
const safetySettings = getSafetySettings(this.modelOptions.model);
|
||||||
const model = this.createLLM(clientOptions);
|
const streamRate = this.options.streamRate ?? Constants.DEFAULT_STREAM_RATE;
|
||||||
|
const modelName = this.modelOptions.modelName ?? this.modelOptions.model ?? '';
|
||||||
|
|
||||||
let reply = '';
|
let reply = '';
|
||||||
const messages = this.isTextModel ? _payload.trim() : _messages;
|
/** @type {Error} */
|
||||||
|
let error;
|
||||||
|
try {
|
||||||
|
if (!EXCLUDED_GENAI_MODELS.test(modelName) && !this.project_id) {
|
||||||
|
/** @type {GenAI} */
|
||||||
|
const client = this.client;
|
||||||
|
/** @type {GenerateContentRequest} */
|
||||||
|
const requestOptions = {
|
||||||
|
safetySettings,
|
||||||
|
contents: _payload,
|
||||||
|
generationConfig: googleGenConfigSchema.parse(this.modelOptions),
|
||||||
|
};
|
||||||
|
|
||||||
if (!this.isVisionModel && context && messages?.length > 0) {
|
const promptPrefix = (this.options.promptPrefix ?? '').trim();
|
||||||
messages.unshift(new SystemMessage(context));
|
if (promptPrefix.length) {
|
||||||
}
|
requestOptions.systemInstruction = {
|
||||||
|
parts: [
|
||||||
|
{
|
||||||
|
text: promptPrefix,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
const stream = await model.stream(messages, {
|
const delay = modelName.includes('flash') ? 8 : 15;
|
||||||
signal: abortController.signal,
|
/** @type {GenAIUsageMetadata} */
|
||||||
timeout: 7000,
|
let usageMetadata;
|
||||||
});
|
|
||||||
|
|
||||||
for await (const chunk of stream) {
|
const result = await client.generateContentStream(requestOptions);
|
||||||
await this.generateTextStream(chunk?.content ?? chunk, onProgress, {
|
for await (const chunk of result.stream) {
|
||||||
delay: this.isGenerativeModel ? 12 : 8,
|
usageMetadata = !usageMetadata
|
||||||
|
? chunk?.usageMetadata
|
||||||
|
: Object.assign(usageMetadata, chunk?.usageMetadata);
|
||||||
|
const chunkText = chunk.text();
|
||||||
|
await this.generateTextStream(chunkText, onProgress, {
|
||||||
|
delay,
|
||||||
|
});
|
||||||
|
reply += chunkText;
|
||||||
|
await sleep(streamRate);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (usageMetadata) {
|
||||||
|
this.usage = {
|
||||||
|
input_tokens: usageMetadata.promptTokenCount,
|
||||||
|
output_tokens: usageMetadata.candidatesTokenCount,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return reply;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { instances } = _payload;
|
||||||
|
const { messages: messages, context } = instances?.[0] ?? {};
|
||||||
|
|
||||||
|
if (!this.isVisionModel && context && messages?.length > 0) {
|
||||||
|
messages.unshift(new SystemMessage(context));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @type {import('@langchain/core/messages').AIMessageChunk['usage_metadata']} */
|
||||||
|
let usageMetadata;
|
||||||
|
/** @type {ChatVertexAI} */
|
||||||
|
const client = this.client;
|
||||||
|
const stream = await client.stream(messages, {
|
||||||
|
signal: abortController.signal,
|
||||||
|
streamUsage: true,
|
||||||
|
safetySettings,
|
||||||
});
|
});
|
||||||
reply += chunk?.content ?? chunk;
|
|
||||||
|
let delay = this.options.streamRate || 8;
|
||||||
|
|
||||||
|
if (!this.options.streamRate) {
|
||||||
|
if (this.isGenerativeModel) {
|
||||||
|
delay = 15;
|
||||||
|
}
|
||||||
|
if (modelName.includes('flash')) {
|
||||||
|
delay = 5;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for await (const chunk of stream) {
|
||||||
|
if (chunk?.usage_metadata) {
|
||||||
|
const metadata = chunk.usage_metadata;
|
||||||
|
for (const key in metadata) {
|
||||||
|
if (Number.isNaN(metadata[key])) {
|
||||||
|
delete metadata[key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
usageMetadata = !usageMetadata ? metadata : concat(usageMetadata, metadata);
|
||||||
|
}
|
||||||
|
|
||||||
|
const chunkText = chunk?.content ?? '';
|
||||||
|
await this.generateTextStream(chunkText, onProgress, {
|
||||||
|
delay,
|
||||||
|
});
|
||||||
|
reply += chunkText;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (usageMetadata) {
|
||||||
|
this.usage = usageMetadata;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
error = e;
|
||||||
|
logger.error('[GoogleClient] There was an issue generating the completion', e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (error != null && reply === '') {
|
||||||
|
const errorMessage = `{ "type": "${ErrorTypes.GoogleError}", "info": "${
|
||||||
|
error.message ?? 'The Google provider failed to generate content, please contact the Admin.'
|
||||||
|
}" }`;
|
||||||
|
throw new Error(errorMessage);
|
||||||
|
}
|
||||||
return reply;
|
return reply;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get stream usage as returned by this client's API response.
|
||||||
|
* @returns {UsageMetadata} The stream usage object.
|
||||||
|
*/
|
||||||
|
getStreamUsage() {
|
||||||
|
return this.usage;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the correct token count for the current user message based on the token count map and API usage.
|
||||||
|
* Edge case: If the calculation results in a negative value, it returns the original estimate.
|
||||||
|
* If revisiting a conversation with a chat history entirely composed of token estimates,
|
||||||
|
* the cumulative token count going forward should become more accurate as the conversation progresses.
|
||||||
|
* @param {Object} params - The parameters for the calculation.
|
||||||
|
* @param {Record<string, number>} params.tokenCountMap - A map of message IDs to their token counts.
|
||||||
|
* @param {string} params.currentMessageId - The ID of the current message to calculate.
|
||||||
|
* @param {UsageMetadata} params.usage - The usage object returned by the API.
|
||||||
|
* @returns {number} The correct token count for the current user message.
|
||||||
|
*/
|
||||||
|
calculateCurrentTokenCount({ tokenCountMap, currentMessageId, usage }) {
|
||||||
|
const originalEstimate = tokenCountMap[currentMessageId] || 0;
|
||||||
|
|
||||||
|
if (!usage || typeof usage.input_tokens !== 'number') {
|
||||||
|
return originalEstimate;
|
||||||
|
}
|
||||||
|
|
||||||
|
tokenCountMap[currentMessageId] = 0;
|
||||||
|
const totalTokensFromMap = Object.values(tokenCountMap).reduce((sum, count) => {
|
||||||
|
const numCount = Number(count);
|
||||||
|
return sum + (isNaN(numCount) ? 0 : numCount);
|
||||||
|
}, 0);
|
||||||
|
const totalInputTokens = usage.input_tokens ?? 0;
|
||||||
|
const currentMessageTokens = totalInputTokens - totalTokensFromMap;
|
||||||
|
return currentMessageTokens > 0 ? currentMessageTokens : originalEstimate;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {object} params
|
||||||
|
* @param {number} params.promptTokens
|
||||||
|
* @param {number} params.completionTokens
|
||||||
|
* @param {UsageMetadata} [params.usage]
|
||||||
|
* @param {string} [params.model]
|
||||||
|
* @param {string} [params.context='message']
|
||||||
|
* @returns {Promise<void>}
|
||||||
|
*/
|
||||||
|
async recordTokenUsage({ promptTokens, completionTokens, model, context = 'message' }) {
|
||||||
|
await spendTokens(
|
||||||
|
{
|
||||||
|
context,
|
||||||
|
user: this.user ?? this.options.req?.user?.id,
|
||||||
|
conversationId: this.conversationId,
|
||||||
|
model: model ?? this.modelOptions.model,
|
||||||
|
endpointTokenConfig: this.options.endpointTokenConfig,
|
||||||
|
},
|
||||||
|
{ promptTokens, completionTokens },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stripped-down logic for generating a title. This uses the non-streaming APIs, since the user does not see titles streaming
|
||||||
|
*/
|
||||||
|
async titleChatCompletion(_payload, options = {}) {
|
||||||
|
let reply = '';
|
||||||
|
const { abortController } = options;
|
||||||
|
|
||||||
|
const model = this.modelOptions.modelName ?? this.modelOptions.model ?? '';
|
||||||
|
const safetySettings = getSafetySettings(model);
|
||||||
|
if (!EXCLUDED_GENAI_MODELS.test(model) && !this.project_id) {
|
||||||
|
logger.debug('Identified titling model as GenAI version');
|
||||||
|
/** @type {GenerativeModel} */
|
||||||
|
const client = this.client;
|
||||||
|
const requestOptions = {
|
||||||
|
contents: _payload,
|
||||||
|
safetySettings,
|
||||||
|
generationConfig: {
|
||||||
|
temperature: 0.5,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = await client.generateContent(requestOptions);
|
||||||
|
reply = result.response?.text();
|
||||||
|
return reply;
|
||||||
|
} else {
|
||||||
|
const { instances } = _payload;
|
||||||
|
const { messages } = instances?.[0] ?? {};
|
||||||
|
const titleResponse = await this.client.invoke(messages, {
|
||||||
|
signal: abortController.signal,
|
||||||
|
timeout: 7000,
|
||||||
|
safetySettings,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (titleResponse.usage_metadata) {
|
||||||
|
await this.recordTokenUsage({
|
||||||
|
model,
|
||||||
|
promptTokens: titleResponse.usage_metadata.input_tokens,
|
||||||
|
completionTokens: titleResponse.usage_metadata.output_tokens,
|
||||||
|
context: 'title',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
reply = titleResponse.content;
|
||||||
|
return reply;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async titleConvo({ text, responseText = '' }) {
|
||||||
|
let title = 'New Chat';
|
||||||
|
const convo = `||>User:
|
||||||
|
"${truncateText(text)}"
|
||||||
|
||>Response:
|
||||||
|
"${JSON.stringify(truncateText(responseText))}"`;
|
||||||
|
|
||||||
|
let { prompt: payload } = await this.buildMessages([
|
||||||
|
{
|
||||||
|
text: `Please generate ${titleInstruction}
|
||||||
|
|
||||||
|
${convo}
|
||||||
|
|
||||||
|
||>Title:`,
|
||||||
|
isCreatedByUser: true,
|
||||||
|
author: this.userLabel,
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
try {
|
||||||
|
this.initializeClient();
|
||||||
|
title = await this.titleChatCompletion(payload, {
|
||||||
|
abortController: new AbortController(),
|
||||||
|
onProgress: () => {},
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
logger.error('[GoogleClient] There was an issue generating the title', e);
|
||||||
|
}
|
||||||
|
logger.debug(`Title response: ${title}`);
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
getSaveOptions() {
|
getSaveOptions() {
|
||||||
return {
|
return {
|
||||||
|
endpointType: null,
|
||||||
|
artifacts: this.options.artifacts,
|
||||||
promptPrefix: this.options.promptPrefix,
|
promptPrefix: this.options.promptPrefix,
|
||||||
|
maxContextTokens: this.options.maxContextTokens,
|
||||||
modelLabel: this.options.modelLabel,
|
modelLabel: this.options.modelLabel,
|
||||||
|
iconURL: this.options.iconURL,
|
||||||
|
greeting: this.options.greeting,
|
||||||
|
spec: this.options.spec,
|
||||||
...this.modelOptions,
|
...this.modelOptions,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
@ -584,23 +911,34 @@ class GoogleClient extends BaseClient {
|
||||||
return reply.trim();
|
return reply.trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* TO-DO: Handle tokens with Google tokenization NOTE: these are required */
|
getEncoding() {
|
||||||
static getTokenizer(encoding, isModelName = false, extendSpecialTokens = {}) {
|
return 'cl100k_base';
|
||||||
if (tokenizersCache[encoding]) {
|
|
||||||
return tokenizersCache[encoding];
|
|
||||||
}
|
|
||||||
let tokenizer;
|
|
||||||
if (isModelName) {
|
|
||||||
tokenizer = encodingForModel(encoding, extendSpecialTokens);
|
|
||||||
} else {
|
|
||||||
tokenizer = getEncoding(encoding, extendSpecialTokens);
|
|
||||||
}
|
|
||||||
tokenizersCache[encoding] = tokenizer;
|
|
||||||
return tokenizer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getVertexTokenCount(text) {
|
||||||
|
/** @type {ChatVertexAI} */
|
||||||
|
const client = this.client ?? this.initializeClient();
|
||||||
|
const connection = client.connection;
|
||||||
|
const gAuthClient = connection.client;
|
||||||
|
const tokenEndpoint = `https://${connection._endpoint}/${connection.apiVersion}/projects/${this.project_id}/locations/${connection._location}/publishers/google/models/${connection.model}/:countTokens`;
|
||||||
|
const result = await gAuthClient.request({
|
||||||
|
url: tokenEndpoint,
|
||||||
|
method: 'POST',
|
||||||
|
data: {
|
||||||
|
contents: [{ role: 'user', parts: [{ text }] }],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the token count of a given text. It also checks and resets the tokenizers if necessary.
|
||||||
|
* @param {string} text - The text to get the token count for.
|
||||||
|
* @returns {number} The token count of the given text.
|
||||||
|
*/
|
||||||
getTokenCount(text) {
|
getTokenCount(text) {
|
||||||
return this.gptEncoder.encode(text, 'all').length;
|
const encoding = this.getEncoding();
|
||||||
|
return Tokenizer.getTokenCount(text, encoding);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
161
api/app/clients/OllamaClient.js
Normal file
161
api/app/clients/OllamaClient.js
Normal file
|
|
@ -0,0 +1,161 @@
|
||||||
|
const { z } = require('zod');
|
||||||
|
const axios = require('axios');
|
||||||
|
const { Ollama } = require('ollama');
|
||||||
|
const { Constants } = require('librechat-data-provider');
|
||||||
|
const { deriveBaseURL } = require('~/utils');
|
||||||
|
const { sleep } = require('~/server/utils');
|
||||||
|
const { logger } = require('~/config');
|
||||||
|
|
||||||
|
const ollamaPayloadSchema = z.object({
|
||||||
|
mirostat: z.number().optional(),
|
||||||
|
mirostat_eta: z.number().optional(),
|
||||||
|
mirostat_tau: z.number().optional(),
|
||||||
|
num_ctx: z.number().optional(),
|
||||||
|
repeat_last_n: z.number().optional(),
|
||||||
|
repeat_penalty: z.number().optional(),
|
||||||
|
temperature: z.number().optional(),
|
||||||
|
seed: z.number().nullable().optional(),
|
||||||
|
stop: z.array(z.string()).optional(),
|
||||||
|
tfs_z: z.number().optional(),
|
||||||
|
num_predict: z.number().optional(),
|
||||||
|
top_k: z.number().optional(),
|
||||||
|
top_p: z.number().optional(),
|
||||||
|
stream: z.optional(z.boolean()),
|
||||||
|
model: z.string(),
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} imageUrl
|
||||||
|
* @returns {string}
|
||||||
|
* @throws {Error}
|
||||||
|
*/
|
||||||
|
const getValidBase64 = (imageUrl) => {
|
||||||
|
const parts = imageUrl.split(';base64,');
|
||||||
|
|
||||||
|
if (parts.length === 2) {
|
||||||
|
return parts[1];
|
||||||
|
} else {
|
||||||
|
logger.error('Invalid or no Base64 string found in URL.');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class OllamaClient {
|
||||||
|
constructor(options = {}) {
|
||||||
|
const host = deriveBaseURL(options.baseURL ?? 'http://localhost:11434');
|
||||||
|
this.streamRate = options.streamRate ?? Constants.DEFAULT_STREAM_RATE;
|
||||||
|
/** @type {Ollama} */
|
||||||
|
this.client = new Ollama({ host });
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetches Ollama models from the specified base API path.
|
||||||
|
* @param {string} baseURL
|
||||||
|
* @returns {Promise<string[]>} The Ollama models.
|
||||||
|
*/
|
||||||
|
static async fetchModels(baseURL) {
|
||||||
|
let models = [];
|
||||||
|
if (!baseURL) {
|
||||||
|
return models;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const ollamaEndpoint = deriveBaseURL(baseURL);
|
||||||
|
/** @type {Promise<AxiosResponse<OllamaListResponse>>} */
|
||||||
|
const response = await axios.get(`${ollamaEndpoint}/api/tags`, {
|
||||||
|
timeout: 5000,
|
||||||
|
});
|
||||||
|
models = response.data.models.map((tag) => tag.name);
|
||||||
|
return models;
|
||||||
|
} catch (error) {
|
||||||
|
const logMessage =
|
||||||
|
'Failed to fetch models from Ollama API. If you are not using Ollama directly, and instead, through some aggregator or reverse proxy that handles fetching via OpenAI spec, ensure the name of the endpoint doesn\'t start with `ollama` (case-insensitive).';
|
||||||
|
logger.error(logMessage, error);
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {ChatCompletionMessage[]} messages
|
||||||
|
* @returns {OllamaMessage[]}
|
||||||
|
*/
|
||||||
|
static formatOpenAIMessages(messages) {
|
||||||
|
const ollamaMessages = [];
|
||||||
|
|
||||||
|
for (const message of messages) {
|
||||||
|
if (typeof message.content === 'string') {
|
||||||
|
ollamaMessages.push({
|
||||||
|
role: message.role,
|
||||||
|
content: message.content,
|
||||||
|
});
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let aggregatedText = '';
|
||||||
|
let imageUrls = [];
|
||||||
|
|
||||||
|
for (const content of message.content) {
|
||||||
|
if (content.type === 'text') {
|
||||||
|
aggregatedText += content.text + ' ';
|
||||||
|
} else if (content.type === 'image_url') {
|
||||||
|
imageUrls.push(getValidBase64(content.image_url.url));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const ollamaMessage = {
|
||||||
|
role: message.role,
|
||||||
|
content: aggregatedText.trim(),
|
||||||
|
};
|
||||||
|
|
||||||
|
if (imageUrls.length > 0) {
|
||||||
|
ollamaMessage.images = imageUrls;
|
||||||
|
}
|
||||||
|
|
||||||
|
ollamaMessages.push(ollamaMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ollamaMessages;
|
||||||
|
}
|
||||||
|
|
||||||
|
/***
|
||||||
|
* @param {Object} params
|
||||||
|
* @param {ChatCompletionPayload} params.payload
|
||||||
|
* @param {onTokenProgress} params.onProgress
|
||||||
|
* @param {AbortController} params.abortController
|
||||||
|
*/
|
||||||
|
async chatCompletion({ payload, onProgress, abortController = null }) {
|
||||||
|
let intermediateReply = '';
|
||||||
|
|
||||||
|
const parameters = ollamaPayloadSchema.parse(payload);
|
||||||
|
const messages = OllamaClient.formatOpenAIMessages(payload.messages);
|
||||||
|
|
||||||
|
if (parameters.stream) {
|
||||||
|
const stream = await this.client.chat({
|
||||||
|
messages,
|
||||||
|
...parameters,
|
||||||
|
});
|
||||||
|
|
||||||
|
for await (const chunk of stream) {
|
||||||
|
const token = chunk.message.content;
|
||||||
|
intermediateReply += token;
|
||||||
|
onProgress(token);
|
||||||
|
if (abortController.signal.aborted) {
|
||||||
|
stream.controller.abort();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
await sleep(this.streamRate);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// TODO: regular completion
|
||||||
|
else {
|
||||||
|
// const generation = await this.client.generate(payload);
|
||||||
|
}
|
||||||
|
|
||||||
|
return intermediateReply;
|
||||||
|
}
|
||||||
|
catch(err) {
|
||||||
|
logger.error('[OllamaClient.chatCompletion]', err);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { OllamaClient, ollamaPayloadSchema };
|
||||||
File diff suppressed because it is too large
Load diff
|
|
@ -1,13 +1,12 @@
|
||||||
const OpenAIClient = require('./OpenAIClient');
|
const OpenAIClient = require('./OpenAIClient');
|
||||||
const { CallbackManager } = require('langchain/callbacks');
|
const { CallbackManager } = require('@langchain/core/callbacks/manager');
|
||||||
const { BufferMemory, ChatMessageHistory } = require('langchain/memory');
|
const { BufferMemory, ChatMessageHistory } = require('langchain/memory');
|
||||||
const { initializeCustomAgent, initializeFunctionsAgent } = require('./agents');
|
|
||||||
const { addImages, buildErrorInput, buildPromptPrefix } = require('./output_parsers');
|
const { addImages, buildErrorInput, buildPromptPrefix } = require('./output_parsers');
|
||||||
|
const { initializeCustomAgent, initializeFunctionsAgent } = require('./agents');
|
||||||
const { processFileURL } = require('~/server/services/Files/process');
|
const { processFileURL } = require('~/server/services/Files/process');
|
||||||
const { EModelEndpoint } = require('librechat-data-provider');
|
const { EModelEndpoint } = require('librechat-data-provider');
|
||||||
const { formatLangChainMessages } = require('./prompts');
|
const { formatLangChainMessages } = require('./prompts');
|
||||||
const checkBalance = require('~/models/checkBalance');
|
const checkBalance = require('~/models/checkBalance');
|
||||||
const { SelfReflectionTool } = require('./tools');
|
|
||||||
const { isEnabled } = require('~/server/utils');
|
const { isEnabled } = require('~/server/utils');
|
||||||
const { extractBaseURL } = require('~/utils');
|
const { extractBaseURL } = require('~/utils');
|
||||||
const { loadTools } = require('./tools/util');
|
const { loadTools } = require('./tools/util');
|
||||||
|
|
@ -40,10 +39,16 @@ class PluginsClient extends OpenAIClient {
|
||||||
|
|
||||||
getSaveOptions() {
|
getSaveOptions() {
|
||||||
return {
|
return {
|
||||||
|
artifacts: this.options.artifacts,
|
||||||
chatGptLabel: this.options.chatGptLabel,
|
chatGptLabel: this.options.chatGptLabel,
|
||||||
|
modelLabel: this.options.modelLabel,
|
||||||
promptPrefix: this.options.promptPrefix,
|
promptPrefix: this.options.promptPrefix,
|
||||||
|
tools: this.options.tools,
|
||||||
...this.modelOptions,
|
...this.modelOptions,
|
||||||
agentOptions: this.agentOptions,
|
agentOptions: this.agentOptions,
|
||||||
|
iconURL: this.options.iconURL,
|
||||||
|
greeting: this.options.greeting,
|
||||||
|
spec: this.options.spec,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -99,7 +104,7 @@ class PluginsClient extends OpenAIClient {
|
||||||
chatHistory: new ChatMessageHistory(pastMessages),
|
chatHistory: new ChatMessageHistory(pastMessages),
|
||||||
});
|
});
|
||||||
|
|
||||||
this.tools = await loadTools({
|
const { loadedTools } = await loadTools({
|
||||||
user,
|
user,
|
||||||
model,
|
model,
|
||||||
tools: this.options.tools,
|
tools: this.options.tools,
|
||||||
|
|
@ -113,14 +118,15 @@ class PluginsClient extends OpenAIClient {
|
||||||
processFileURL,
|
processFileURL,
|
||||||
message,
|
message,
|
||||||
},
|
},
|
||||||
|
useSpecs: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (this.tools.length > 0 && !this.functionsAgent) {
|
if (loadedTools.length === 0) {
|
||||||
this.tools.push(new SelfReflectionTool({ message, isGpt3: false }));
|
|
||||||
} else if (this.tools.length === 0) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.tools = loadedTools;
|
||||||
|
|
||||||
logger.debug('[PluginsClient] Requested Tools', this.options.tools);
|
logger.debug('[PluginsClient] Requested Tools', this.options.tools);
|
||||||
logger.debug(
|
logger.debug(
|
||||||
'[PluginsClient] Loaded Tools',
|
'[PluginsClient] Loaded Tools',
|
||||||
|
|
@ -139,14 +145,22 @@ class PluginsClient extends OpenAIClient {
|
||||||
|
|
||||||
// initialize agent
|
// initialize agent
|
||||||
const initializer = this.functionsAgent ? initializeFunctionsAgent : initializeCustomAgent;
|
const initializer = this.functionsAgent ? initializeFunctionsAgent : initializeCustomAgent;
|
||||||
|
|
||||||
|
let customInstructions = (this.options.promptPrefix ?? '').trim();
|
||||||
|
if (typeof this.options.artifactsPrompt === 'string' && this.options.artifactsPrompt) {
|
||||||
|
customInstructions = `${customInstructions ?? ''}\n${this.options.artifactsPrompt}`.trim();
|
||||||
|
}
|
||||||
|
|
||||||
this.executor = await initializer({
|
this.executor = await initializer({
|
||||||
model,
|
model,
|
||||||
signal,
|
signal,
|
||||||
pastMessages,
|
pastMessages,
|
||||||
tools: this.tools,
|
tools: this.tools,
|
||||||
currentDateString: this.currentDateString,
|
customInstructions,
|
||||||
verbose: this.options.debug,
|
verbose: this.options.debug,
|
||||||
returnIntermediateSteps: true,
|
returnIntermediateSteps: true,
|
||||||
|
customName: this.options.chatGptLabel,
|
||||||
|
currentDateString: this.currentDateString,
|
||||||
callbackManager: CallbackManager.fromHandlers({
|
callbackManager: CallbackManager.fromHandlers({
|
||||||
async handleAgentAction(action, runId) {
|
async handleAgentAction(action, runId) {
|
||||||
handleAction(action, runId, onAgentAction);
|
handleAction(action, runId, onAgentAction);
|
||||||
|
|
@ -214,6 +228,13 @@ class PluginsClient extends OpenAIClient {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {TMessage} responseMessage
|
||||||
|
* @param {Partial<TMessage>} saveOptions
|
||||||
|
* @param {string} user
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
async handleResponseMessage(responseMessage, saveOptions, user) {
|
async handleResponseMessage(responseMessage, saveOptions, user) {
|
||||||
const { output, errorMessage, ...result } = this.result;
|
const { output, errorMessage, ...result } = this.result;
|
||||||
logger.debug('[PluginsClient][handleResponseMessage] Output:', {
|
logger.debug('[PluginsClient][handleResponseMessage] Output:', {
|
||||||
|
|
@ -232,22 +253,33 @@ class PluginsClient extends OpenAIClient {
|
||||||
await this.recordTokenUsage(responseMessage);
|
await this.recordTokenUsage(responseMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.saveMessageToDatabase(responseMessage, saveOptions, user);
|
this.responsePromise = this.saveMessageToDatabase(responseMessage, saveOptions, user);
|
||||||
delete responseMessage.tokenCount;
|
delete responseMessage.tokenCount;
|
||||||
return { ...responseMessage, ...result };
|
return { ...responseMessage, ...result };
|
||||||
}
|
}
|
||||||
|
|
||||||
async sendMessage(message, opts = {}) {
|
async sendMessage(message, opts = {}) {
|
||||||
|
/** @type {{ filteredTools: string[], includedTools: string[] }} */
|
||||||
|
const { filteredTools = [], includedTools = [] } = this.options.req.app.locals;
|
||||||
|
|
||||||
|
if (includedTools.length > 0) {
|
||||||
|
const tools = this.options.tools.filter((plugin) => includedTools.includes(plugin));
|
||||||
|
this.options.tools = tools;
|
||||||
|
} else {
|
||||||
|
const tools = this.options.tools.filter((plugin) => !filteredTools.includes(plugin));
|
||||||
|
this.options.tools = tools;
|
||||||
|
}
|
||||||
|
|
||||||
// If a message is edited, no tools can be used.
|
// If a message is edited, no tools can be used.
|
||||||
const completionMode = this.options.tools.length === 0 || opts.isEdited;
|
const completionMode = this.options.tools.length === 0 || opts.isEdited;
|
||||||
if (completionMode) {
|
if (completionMode) {
|
||||||
this.setOptions(opts);
|
this.setOptions(opts);
|
||||||
return super.sendMessage(message, opts);
|
return super.sendMessage(message, opts);
|
||||||
}
|
}
|
||||||
logger.debug('[PluginsClient] sendMessage', { message, opts });
|
|
||||||
|
logger.debug('[PluginsClient] sendMessage', { userMessageText: message, opts });
|
||||||
const {
|
const {
|
||||||
user,
|
user,
|
||||||
isEdited,
|
|
||||||
conversationId,
|
conversationId,
|
||||||
responseMessageId,
|
responseMessageId,
|
||||||
saveOptions,
|
saveOptions,
|
||||||
|
|
@ -258,6 +290,14 @@ class PluginsClient extends OpenAIClient {
|
||||||
onToolEnd,
|
onToolEnd,
|
||||||
} = await this.handleStartMethods(message, opts);
|
} = await this.handleStartMethods(message, opts);
|
||||||
|
|
||||||
|
if (opts.progressCallback) {
|
||||||
|
opts.onProgress = opts.progressCallback.call(null, {
|
||||||
|
...(opts.progressOptions ?? {}),
|
||||||
|
parentMessageId: userMessage.messageId,
|
||||||
|
messageId: responseMessageId,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
this.currentMessages.push(userMessage);
|
this.currentMessages.push(userMessage);
|
||||||
|
|
||||||
let {
|
let {
|
||||||
|
|
@ -286,7 +326,15 @@ class PluginsClient extends OpenAIClient {
|
||||||
if (payload) {
|
if (payload) {
|
||||||
this.currentMessages = payload;
|
this.currentMessages = payload;
|
||||||
}
|
}
|
||||||
await this.saveMessageToDatabase(userMessage, saveOptions, user);
|
|
||||||
|
if (!this.skipSaveUserMessage) {
|
||||||
|
this.userMessagePromise = this.saveMessageToDatabase(userMessage, saveOptions, user);
|
||||||
|
if (typeof opts?.getReqData === 'function') {
|
||||||
|
opts.getReqData({
|
||||||
|
userMessagePromise: this.userMessagePromise,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (isEnabled(process.env.CHECK_BALANCE)) {
|
if (isEnabled(process.env.CHECK_BALANCE)) {
|
||||||
await checkBalance({
|
await checkBalance({
|
||||||
|
|
@ -304,11 +352,12 @@ class PluginsClient extends OpenAIClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
const responseMessage = {
|
const responseMessage = {
|
||||||
|
endpoint: EModelEndpoint.gptPlugins,
|
||||||
|
iconURL: this.options.iconURL,
|
||||||
messageId: responseMessageId,
|
messageId: responseMessageId,
|
||||||
conversationId,
|
conversationId,
|
||||||
parentMessageId: userMessage.messageId,
|
parentMessageId: userMessage.messageId,
|
||||||
isCreatedByUser: false,
|
isCreatedByUser: false,
|
||||||
isEdited,
|
|
||||||
model: this.modelOptions.model,
|
model: this.modelOptions.model,
|
||||||
sender: this.sender,
|
sender: this.sender,
|
||||||
promptTokens,
|
promptTokens,
|
||||||
|
|
@ -397,7 +446,6 @@ class PluginsClient extends OpenAIClient {
|
||||||
|
|
||||||
const instructionsPayload = {
|
const instructionsPayload = {
|
||||||
role: 'system',
|
role: 'system',
|
||||||
name: 'instructions',
|
|
||||||
content: promptPrefix,
|
content: promptPrefix,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
const { ZeroShotAgent } = require('langchain/agents');
|
const { ZeroShotAgent } = require('langchain/agents');
|
||||||
const { PromptTemplate, renderTemplate } = require('langchain/prompts');
|
const { PromptTemplate, renderTemplate } = require('@langchain/core/prompts');
|
||||||
const { gpt3, gpt4 } = require('./instructions');
|
const { gpt3, gpt4 } = require('./instructions');
|
||||||
|
|
||||||
class CustomAgent extends ZeroShotAgent {
|
class CustomAgent extends ZeroShotAgent {
|
||||||
|
|
|
||||||
|
|
@ -7,16 +7,24 @@ const {
|
||||||
ChatPromptTemplate,
|
ChatPromptTemplate,
|
||||||
SystemMessagePromptTemplate,
|
SystemMessagePromptTemplate,
|
||||||
HumanMessagePromptTemplate,
|
HumanMessagePromptTemplate,
|
||||||
} = require('langchain/prompts');
|
} = require('@langchain/core/prompts');
|
||||||
|
|
||||||
const initializeCustomAgent = async ({
|
const initializeCustomAgent = async ({
|
||||||
tools,
|
tools,
|
||||||
model,
|
model,
|
||||||
pastMessages,
|
pastMessages,
|
||||||
|
customName,
|
||||||
|
customInstructions,
|
||||||
currentDateString,
|
currentDateString,
|
||||||
...rest
|
...rest
|
||||||
}) => {
|
}) => {
|
||||||
let prompt = CustomAgent.createPrompt(tools, { currentDateString, model: model.modelName });
|
let prompt = CustomAgent.createPrompt(tools, { currentDateString, model: model.modelName });
|
||||||
|
if (customName) {
|
||||||
|
prompt = `You are "${customName}".\n${prompt}`;
|
||||||
|
}
|
||||||
|
if (customInstructions) {
|
||||||
|
prompt = `${prompt}\n${customInstructions}`;
|
||||||
|
}
|
||||||
|
|
||||||
const chatPrompt = ChatPromptTemplate.fromMessages([
|
const chatPrompt = ChatPromptTemplate.fromMessages([
|
||||||
new SystemMessagePromptTemplate(prompt),
|
new SystemMessagePromptTemplate(prompt),
|
||||||
|
|
|
||||||
|
|
@ -1,44 +1,3 @@
|
||||||
/*
|
|
||||||
module.exports = `You are ChatGPT, a Large Language model with useful tools.
|
|
||||||
|
|
||||||
Talk to the human and provide meaningful answers when questions are asked.
|
|
||||||
|
|
||||||
Use the tools when you need them, but use your own knowledge if you are confident of the answer. Keep answers short and concise.
|
|
||||||
|
|
||||||
A tool is not usually needed for creative requests, so do your best to answer them without tools.
|
|
||||||
|
|
||||||
Avoid repeating identical answers if it appears before. Only fulfill the human's requests, do not create extra steps beyond what the human has asked for.
|
|
||||||
|
|
||||||
Your input for 'Action' should be the name of tool used only.
|
|
||||||
|
|
||||||
Be honest. If you can't answer something, or a tool is not appropriate, say you don't know or answer to the best of your ability.
|
|
||||||
|
|
||||||
Attempt to fulfill the human's requests in as few actions as possible`;
|
|
||||||
*/
|
|
||||||
|
|
||||||
// module.exports = `You are ChatGPT, a highly knowledgeable and versatile large language model.
|
|
||||||
|
|
||||||
// Engage with the Human conversationally, providing concise and meaningful answers to questions. Utilize built-in tools when necessary, except for creative requests, where relying on your own knowledge is preferred. Aim for variety and avoid repetitive answers.
|
|
||||||
|
|
||||||
// For your 'Action' input, state the name of the tool used only, and honor user requests without adding extra steps. Always be honest; if you cannot provide an appropriate answer or tool, admit that or do your best.
|
|
||||||
|
|
||||||
// Strive to meet the user's needs efficiently with minimal actions.`;
|
|
||||||
|
|
||||||
// import {
|
|
||||||
// BasePromptTemplate,
|
|
||||||
// BaseStringPromptTemplate,
|
|
||||||
// SerializedBasePromptTemplate,
|
|
||||||
// renderTemplate,
|
|
||||||
// } from "langchain/prompts";
|
|
||||||
|
|
||||||
// prefix: `You are ChatGPT, a highly knowledgeable and versatile large language model.
|
|
||||||
// Your objective is to help users by understanding their intent and choosing the best action. Prioritize direct, specific responses. Use concise, varied answers and rely on your knowledge for creative tasks. Utilize tools when needed, and structure results for machine compatibility.
|
|
||||||
// prefix: `Objective: to comprehend human intentions based on user input and available tools. Goal: identify the best action to directly address the human's query. In your subsequent steps, you will utilize the chosen action. You may select multiple actions and list them in a meaningful order. Prioritize actions that directly relate to the user's query over general ones. Ensure that the generated thought is highly specific and explicit to best match the user's expectations. Construct the result in a manner that an online open-API would most likely expect. Provide concise and meaningful answers to human queries. Utilize tools when necessary. Relying on your own knowledge is preferred for creative requests. Aim for variety and avoid repetitive answers.
|
|
||||||
|
|
||||||
// # Available Actions & Tools:
|
|
||||||
// N/A: no suitable action, use your own knowledge.`,
|
|
||||||
// suffix: `Remember, all your responses MUST adhere to the described format and only respond if the format is followed. Output exactly with the requested format, avoiding any other text as this will be parsed by a machine. Following 'Action:', provide only one of the actions listed above. If a tool is not necessary, deduce this quickly and finish your response. Honor the human's requests without adding extra steps. Carry out tasks in the sequence written by the human. Always be honest; if you cannot provide an appropriate answer or tool, do your best with your own knowledge. Strive to meet the user's needs efficiently with minimal actions.`;
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
'gpt3-v1': {
|
'gpt3-v1': {
|
||||||
prefix: `Objective: Understand human intentions using user input and available tools. Goal: Identify the most suitable actions to directly address user queries.
|
prefix: `Objective: Understand human intentions using user input and available tools. Goal: Identify the most suitable actions to directly address user queries.
|
||||||
|
|
|
||||||
|
|
@ -1,122 +0,0 @@
|
||||||
const { Agent } = require('langchain/agents');
|
|
||||||
const { LLMChain } = require('langchain/chains');
|
|
||||||
const { FunctionChatMessage, AIChatMessage } = require('langchain/schema');
|
|
||||||
const {
|
|
||||||
ChatPromptTemplate,
|
|
||||||
MessagesPlaceholder,
|
|
||||||
SystemMessagePromptTemplate,
|
|
||||||
HumanMessagePromptTemplate,
|
|
||||||
} = require('langchain/prompts');
|
|
||||||
const { logger } = require('~/config');
|
|
||||||
|
|
||||||
const PREFIX = 'You are a helpful AI assistant.';
|
|
||||||
|
|
||||||
function parseOutput(message) {
|
|
||||||
if (message.additional_kwargs.function_call) {
|
|
||||||
const function_call = message.additional_kwargs.function_call;
|
|
||||||
return {
|
|
||||||
tool: function_call.name,
|
|
||||||
toolInput: function_call.arguments ? JSON.parse(function_call.arguments) : {},
|
|
||||||
log: message.text,
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
return { returnValues: { output: message.text }, log: message.text };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class FunctionsAgent extends Agent {
|
|
||||||
constructor(input) {
|
|
||||||
super({ ...input, outputParser: undefined });
|
|
||||||
this.tools = input.tools;
|
|
||||||
}
|
|
||||||
|
|
||||||
lc_namespace = ['langchain', 'agents', 'openai'];
|
|
||||||
|
|
||||||
_agentType() {
|
|
||||||
return 'openai-functions';
|
|
||||||
}
|
|
||||||
|
|
||||||
observationPrefix() {
|
|
||||||
return 'Observation: ';
|
|
||||||
}
|
|
||||||
|
|
||||||
llmPrefix() {
|
|
||||||
return 'Thought:';
|
|
||||||
}
|
|
||||||
|
|
||||||
_stop() {
|
|
||||||
return ['Observation:'];
|
|
||||||
}
|
|
||||||
|
|
||||||
static createPrompt(_tools, fields) {
|
|
||||||
const { prefix = PREFIX, currentDateString } = fields || {};
|
|
||||||
|
|
||||||
return ChatPromptTemplate.fromMessages([
|
|
||||||
SystemMessagePromptTemplate.fromTemplate(`Date: ${currentDateString}\n${prefix}`),
|
|
||||||
new MessagesPlaceholder('chat_history'),
|
|
||||||
HumanMessagePromptTemplate.fromTemplate('Query: {input}'),
|
|
||||||
new MessagesPlaceholder('agent_scratchpad'),
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
static fromLLMAndTools(llm, tools, args) {
|
|
||||||
FunctionsAgent.validateTools(tools);
|
|
||||||
const prompt = FunctionsAgent.createPrompt(tools, args);
|
|
||||||
const chain = new LLMChain({
|
|
||||||
prompt,
|
|
||||||
llm,
|
|
||||||
callbacks: args?.callbacks,
|
|
||||||
});
|
|
||||||
return new FunctionsAgent({
|
|
||||||
llmChain: chain,
|
|
||||||
allowedTools: tools.map((t) => t.name),
|
|
||||||
tools,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async constructScratchPad(steps) {
|
|
||||||
return steps.flatMap(({ action, observation }) => [
|
|
||||||
new AIChatMessage('', {
|
|
||||||
function_call: {
|
|
||||||
name: action.tool,
|
|
||||||
arguments: JSON.stringify(action.toolInput),
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
new FunctionChatMessage(observation, action.tool),
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
async plan(steps, inputs, callbackManager) {
|
|
||||||
// Add scratchpad and stop to inputs
|
|
||||||
const thoughts = await this.constructScratchPad(steps);
|
|
||||||
const newInputs = Object.assign({}, inputs, { agent_scratchpad: thoughts });
|
|
||||||
if (this._stop().length !== 0) {
|
|
||||||
newInputs.stop = this._stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Split inputs between prompt and llm
|
|
||||||
const llm = this.llmChain.llm;
|
|
||||||
const valuesForPrompt = Object.assign({}, newInputs);
|
|
||||||
const valuesForLLM = {
|
|
||||||
tools: this.tools,
|
|
||||||
};
|
|
||||||
for (let i = 0; i < this.llmChain.llm.callKeys.length; i++) {
|
|
||||||
const key = this.llmChain.llm.callKeys[i];
|
|
||||||
if (key in inputs) {
|
|
||||||
valuesForLLM[key] = inputs[key];
|
|
||||||
delete valuesForPrompt[key];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const promptValue = await this.llmChain.prompt.formatPromptValue(valuesForPrompt);
|
|
||||||
const message = await llm.predictMessages(
|
|
||||||
promptValue.toChatMessages(),
|
|
||||||
valuesForLLM,
|
|
||||||
callbackManager,
|
|
||||||
);
|
|
||||||
logger.debug('[FunctionsAgent] plan message', message);
|
|
||||||
return parseOutput(message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = FunctionsAgent;
|
|
||||||
|
|
@ -10,6 +10,8 @@ const initializeFunctionsAgent = async ({
|
||||||
tools,
|
tools,
|
||||||
model,
|
model,
|
||||||
pastMessages,
|
pastMessages,
|
||||||
|
customName,
|
||||||
|
customInstructions,
|
||||||
currentDateString,
|
currentDateString,
|
||||||
...rest
|
...rest
|
||||||
}) => {
|
}) => {
|
||||||
|
|
@ -24,7 +26,13 @@ const initializeFunctionsAgent = async ({
|
||||||
returnMessages: true,
|
returnMessages: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
const prefix = addToolDescriptions(`Current Date: ${currentDateString}\n${PREFIX}`, tools);
|
let prefix = addToolDescriptions(`Current Date: ${currentDateString}\n${PREFIX}`, tools);
|
||||||
|
if (customName) {
|
||||||
|
prefix = `You are "${customName}".\n${prefix}`;
|
||||||
|
}
|
||||||
|
if (customInstructions) {
|
||||||
|
prefix = `${prefix}\n${customInstructions}`;
|
||||||
|
}
|
||||||
|
|
||||||
return await initializeAgentExecutorWithOptions(tools, model, {
|
return await initializeAgentExecutorWithOptions(tools, model, {
|
||||||
agentType: 'openai-functions',
|
agentType: 'openai-functions',
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
const { TokenTextSplitter } = require('langchain/text_splitter');
|
const { TokenTextSplitter } = require('@langchain/textsplitters');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Splits a given text by token chunks, based on the provided parameters for the TokenTextSplitter.
|
* Splits a given text by token chunks, based on the provided parameters for the TokenTextSplitter.
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ describe('tokenSplit', () => {
|
||||||
returnSize: 5,
|
returnSize: 5,
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(result).toEqual(['. Null', ' Nullam', 'am id', ' id.', '.']);
|
expect(result).toEqual(['it.', '. Null', ' Nullam', 'am id', ' id.']);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns correct text chunks with default parameters', async () => {
|
it('returns correct text chunks with default parameters', async () => {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
const { createStartHandler } = require('~/app/clients/callbacks');
|
const { createStartHandler } = require('~/app/clients/callbacks');
|
||||||
const spendTokens = require('~/models/spendTokens');
|
const { spendTokens } = require('~/models/spendTokens');
|
||||||
const { logger } = require('~/config');
|
const { logger } = require('~/config');
|
||||||
|
|
||||||
class RunManager {
|
class RunManager {
|
||||||
|
|
|
||||||
85
api/app/clients/llm/createCoherePayload.js
Normal file
85
api/app/clients/llm/createCoherePayload.js
Normal file
|
|
@ -0,0 +1,85 @@
|
||||||
|
const { CohereConstants } = require('librechat-data-provider');
|
||||||
|
const { titleInstruction } = require('../prompts/titlePrompts');
|
||||||
|
|
||||||
|
// Mapping OpenAI roles to Cohere roles
|
||||||
|
const roleMap = {
|
||||||
|
user: CohereConstants.ROLE_USER,
|
||||||
|
assistant: CohereConstants.ROLE_CHATBOT,
|
||||||
|
system: CohereConstants.ROLE_SYSTEM, // Recognize and map the system role explicitly
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adjusts an OpenAI ChatCompletionPayload to conform with Cohere's expected chat payload format.
|
||||||
|
* Now includes handling for "system" roles explicitly mentioned.
|
||||||
|
*
|
||||||
|
* @param {Object} options - Object containing the model options.
|
||||||
|
* @param {ChatCompletionPayload} options.modelOptions - The OpenAI model payload options.
|
||||||
|
* @returns {CohereChatStreamRequest} Cohere-compatible chat API payload.
|
||||||
|
*/
|
||||||
|
function createCoherePayload({ modelOptions }) {
|
||||||
|
/** @type {string | undefined} */
|
||||||
|
let preamble;
|
||||||
|
let latestUserMessageContent = '';
|
||||||
|
const {
|
||||||
|
stream,
|
||||||
|
stop,
|
||||||
|
top_p,
|
||||||
|
temperature,
|
||||||
|
frequency_penalty,
|
||||||
|
presence_penalty,
|
||||||
|
max_tokens,
|
||||||
|
messages,
|
||||||
|
model,
|
||||||
|
...rest
|
||||||
|
} = modelOptions;
|
||||||
|
|
||||||
|
// Filter out the latest user message and transform remaining messages to Cohere's chat_history format
|
||||||
|
let chatHistory = messages.reduce((acc, message, index, arr) => {
|
||||||
|
const isLastUserMessage = index === arr.length - 1 && message.role === 'user';
|
||||||
|
|
||||||
|
const messageContent =
|
||||||
|
typeof message.content === 'string'
|
||||||
|
? message.content
|
||||||
|
: message.content.map((part) => (part.type === 'text' ? part.text : '')).join(' ');
|
||||||
|
|
||||||
|
if (isLastUserMessage) {
|
||||||
|
latestUserMessageContent = messageContent;
|
||||||
|
} else {
|
||||||
|
acc.push({
|
||||||
|
role: roleMap[message.role] || CohereConstants.ROLE_USER,
|
||||||
|
message: messageContent,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return acc;
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
if (
|
||||||
|
chatHistory.length === 1 &&
|
||||||
|
chatHistory[0].role === CohereConstants.ROLE_SYSTEM &&
|
||||||
|
!latestUserMessageContent.length
|
||||||
|
) {
|
||||||
|
const message = chatHistory[0].message;
|
||||||
|
latestUserMessageContent = message.includes(titleInstruction)
|
||||||
|
? CohereConstants.TITLE_MESSAGE
|
||||||
|
: '.';
|
||||||
|
preamble = message;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
message: latestUserMessageContent,
|
||||||
|
model: model,
|
||||||
|
chatHistory,
|
||||||
|
stream: stream ?? false,
|
||||||
|
temperature: temperature,
|
||||||
|
frequencyPenalty: frequency_penalty,
|
||||||
|
presencePenalty: presence_penalty,
|
||||||
|
maxTokens: max_tokens,
|
||||||
|
stopSequences: stop,
|
||||||
|
preamble,
|
||||||
|
p: top_p,
|
||||||
|
...rest,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = createCoherePayload;
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
const { ChatOpenAI } = require('langchain/chat_models/openai');
|
const { ChatOpenAI } = require('@langchain/openai');
|
||||||
const { sanitizeModelName, constructAzureURL } = require('~/utils');
|
const { sanitizeModelName, constructAzureURL } = require('~/utils');
|
||||||
const { isEnabled } = require('~/server/utils');
|
const { isEnabled } = require('~/server/utils');
|
||||||
|
|
||||||
|
|
@ -8,7 +8,7 @@ const { isEnabled } = require('~/server/utils');
|
||||||
* @param {Object} options - The options for creating the LLM.
|
* @param {Object} options - The options for creating the LLM.
|
||||||
* @param {ModelOptions} options.modelOptions - The options specific to the model, including modelName, temperature, presence_penalty, frequency_penalty, and other model-related settings.
|
* @param {ModelOptions} options.modelOptions - The options specific to the model, including modelName, temperature, presence_penalty, frequency_penalty, and other model-related settings.
|
||||||
* @param {ConfigOptions} options.configOptions - Configuration options for the API requests, including proxy settings and custom headers.
|
* @param {ConfigOptions} options.configOptions - Configuration options for the API requests, including proxy settings and custom headers.
|
||||||
* @param {Callbacks} options.callbacks - Callback functions for managing the lifecycle of the LLM, including token buffers, context, and initial message count.
|
* @param {Callbacks} [options.callbacks] - Callback functions for managing the lifecycle of the LLM, including token buffers, context, and initial message count.
|
||||||
* @param {boolean} [options.streaming=false] - Determines if the LLM should operate in streaming mode.
|
* @param {boolean} [options.streaming=false] - Determines if the LLM should operate in streaming mode.
|
||||||
* @param {string} options.openAIApiKey - The API key for OpenAI, used for authentication.
|
* @param {string} options.openAIApiKey - The API key for OpenAI, used for authentication.
|
||||||
* @param {AzureOptions} [options.azure={}] - Optional Azure-specific configurations. If provided, Azure configurations take precedence over OpenAI configurations.
|
* @param {AzureOptions} [options.azure={}] - Optional Azure-specific configurations. If provided, Azure configurations take precedence over OpenAI configurations.
|
||||||
|
|
@ -17,7 +17,7 @@ const { isEnabled } = require('~/server/utils');
|
||||||
*
|
*
|
||||||
* @example
|
* @example
|
||||||
* const llm = createLLM({
|
* const llm = createLLM({
|
||||||
* modelOptions: { modelName: 'gpt-3.5-turbo', temperature: 0.2 },
|
* modelOptions: { modelName: 'gpt-4o-mini', temperature: 0.2 },
|
||||||
* configOptions: { basePath: 'https://example.api/path' },
|
* configOptions: { basePath: 'https://example.api/path' },
|
||||||
* callbacks: { onMessage: handleMessage },
|
* callbacks: { onMessage: handleMessage },
|
||||||
* openAIApiKey: 'your-api-key'
|
* openAIApiKey: 'your-api-key'
|
||||||
|
|
@ -57,7 +57,7 @@ function createLLM({
|
||||||
if (azure && configOptions.basePath) {
|
if (azure && configOptions.basePath) {
|
||||||
const azureURL = constructAzureURL({
|
const azureURL = constructAzureURL({
|
||||||
baseURL: configOptions.basePath,
|
baseURL: configOptions.basePath,
|
||||||
azure: azureOptions,
|
azureOptions,
|
||||||
});
|
});
|
||||||
azureOptions.azureOpenAIBasePath = azureURL.split(
|
azureOptions.azureOpenAIBasePath = azureURL.split(
|
||||||
`/${azureOptions.azureOpenAIApiDeploymentName}`,
|
`/${azureOptions.azureOpenAIApiDeploymentName}`,
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,9 @@
|
||||||
const createLLM = require('./createLLM');
|
const createLLM = require('./createLLM');
|
||||||
const RunManager = require('./RunManager');
|
const RunManager = require('./RunManager');
|
||||||
|
const createCoherePayload = require('./createCoherePayload');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
createLLM,
|
createLLM,
|
||||||
RunManager,
|
RunManager,
|
||||||
|
createCoherePayload,
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
require('dotenv').config();
|
require('dotenv').config();
|
||||||
const { ChatOpenAI } = require('langchain/chat_models/openai');
|
const { ChatOpenAI } = require('@langchain/openai');
|
||||||
const { getBufferString, ConversationSummaryBufferMemory } = require('langchain/memory');
|
const { getBufferString, ConversationSummaryBufferMemory } = require('langchain/memory');
|
||||||
|
|
||||||
const chatPromptMemory = new ConversationSummaryBufferMemory({
|
const chatPromptMemory = new ConversationSummaryBufferMemory({
|
||||||
llm: new ChatOpenAI({ modelName: 'gpt-3.5-turbo', temperature: 0 }),
|
llm: new ChatOpenAI({ modelName: 'gpt-4o-mini', temperature: 0 }),
|
||||||
maxTokenLimit: 10,
|
maxTokenLimit: 10,
|
||||||
returnMessages: true,
|
returnMessages: true,
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -60,10 +60,10 @@ function addImages(intermediateSteps, responseMessage) {
|
||||||
if (!observation || !observation.includes('![')) {
|
if (!observation || !observation.includes('![')) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const observedImagePath = observation.match(/!\[.*\]\([^)]*\)/g);
|
const observedImagePath = observation.match(/!\[[^(]*\]\([^)]*\)/g);
|
||||||
if (observedImagePath && !responseMessage.text.includes(observedImagePath[0])) {
|
if (observedImagePath && !responseMessage.text.includes(observedImagePath[0])) {
|
||||||
responseMessage.text += '\n' + observation;
|
responseMessage.text += '\n' + observedImagePath[0];
|
||||||
logger.debug('[addImages] added image from intermediateSteps:', observation);
|
logger.debug('[addImages] added image from intermediateSteps:', observedImagePath[0]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -81,4 +81,62 @@ describe('addImages', () => {
|
||||||
addImages(intermediateSteps, responseMessage);
|
addImages(intermediateSteps, responseMessage);
|
||||||
expect(responseMessage.text).toBe(`${originalText}\n${imageMarkdown}`);
|
expect(responseMessage.text).toBe(`${originalText}\n${imageMarkdown}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should extract only image markdowns when there is text between them', () => {
|
||||||
|
const markdownWithTextBetweenImages = `
|
||||||
|

|
||||||
|
Some text between images that should not be included.
|
||||||
|

|
||||||
|
More text that should be ignored.
|
||||||
|

|
||||||
|
`;
|
||||||
|
intermediateSteps.push({ observation: markdownWithTextBetweenImages });
|
||||||
|
addImages(intermediateSteps, responseMessage);
|
||||||
|
expect(responseMessage.text).toBe('\n');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should only return the first image when multiple images are present', () => {
|
||||||
|
const markdownWithMultipleImages = `
|
||||||
|

|
||||||
|

|
||||||
|

|
||||||
|
`;
|
||||||
|
intermediateSteps.push({ observation: markdownWithMultipleImages });
|
||||||
|
addImages(intermediateSteps, responseMessage);
|
||||||
|
expect(responseMessage.text).toBe('\n');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not include any text or metadata surrounding the image markdown', () => {
|
||||||
|
const markdownWithMetadata = `
|
||||||
|
Title: Test Document
|
||||||
|
Author: John Doe
|
||||||
|

|
||||||
|
Some content after the image.
|
||||||
|
Vector values: [0.1, 0.2, 0.3]
|
||||||
|
`;
|
||||||
|
intermediateSteps.push({ observation: markdownWithMetadata });
|
||||||
|
addImages(intermediateSteps, responseMessage);
|
||||||
|
expect(responseMessage.text).toBe('\n');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle complex markdown with multiple images and only return the first one', () => {
|
||||||
|
const complexMarkdown = `
|
||||||
|
# Document Title
|
||||||
|
|
||||||
|
## Section 1
|
||||||
|
Here's some text with an embedded image:
|
||||||
|

|
||||||
|
|
||||||
|
## Section 2
|
||||||
|
More text here...
|
||||||
|

|
||||||
|
|
||||||
|
### Subsection
|
||||||
|
Even more content
|
||||||
|

|
||||||
|
`;
|
||||||
|
intermediateSteps.push({ observation: complexMarkdown });
|
||||||
|
addImages(intermediateSteps, responseMessage);
|
||||||
|
expect(responseMessage.text).toBe('\n');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
43
api/app/clients/prompts/addCacheControl.js
Normal file
43
api/app/clients/prompts/addCacheControl.js
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
/**
|
||||||
|
* Anthropic API: Adds cache control to the appropriate user messages in the payload.
|
||||||
|
* @param {Array<AnthropicMessage>} messages - The array of message objects.
|
||||||
|
* @returns {Array<AnthropicMessage>} - The updated array of message objects with cache control added.
|
||||||
|
*/
|
||||||
|
function addCacheControl(messages) {
|
||||||
|
if (!Array.isArray(messages) || messages.length < 2) {
|
||||||
|
return messages;
|
||||||
|
}
|
||||||
|
|
||||||
|
const updatedMessages = [...messages];
|
||||||
|
let userMessagesModified = 0;
|
||||||
|
|
||||||
|
for (let i = updatedMessages.length - 1; i >= 0 && userMessagesModified < 2; i--) {
|
||||||
|
const message = updatedMessages[i];
|
||||||
|
if (message.role !== 'user') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof message.content === 'string') {
|
||||||
|
message.content = [
|
||||||
|
{
|
||||||
|
type: 'text',
|
||||||
|
text: message.content,
|
||||||
|
cache_control: { type: 'ephemeral' },
|
||||||
|
},
|
||||||
|
];
|
||||||
|
userMessagesModified++;
|
||||||
|
} else if (Array.isArray(message.content)) {
|
||||||
|
for (let j = message.content.length - 1; j >= 0; j--) {
|
||||||
|
if (message.content[j].type === 'text') {
|
||||||
|
message.content[j].cache_control = { type: 'ephemeral' };
|
||||||
|
userMessagesModified++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return updatedMessages;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = addCacheControl;
|
||||||
227
api/app/clients/prompts/addCacheControl.spec.js
Normal file
227
api/app/clients/prompts/addCacheControl.spec.js
Normal file
|
|
@ -0,0 +1,227 @@
|
||||||
|
const addCacheControl = require('./addCacheControl');
|
||||||
|
|
||||||
|
describe('addCacheControl', () => {
|
||||||
|
test('should add cache control to the last two user messages with array content', () => {
|
||||||
|
const messages = [
|
||||||
|
{ role: 'user', content: [{ type: 'text', text: 'Hello' }] },
|
||||||
|
{ role: 'assistant', content: [{ type: 'text', text: 'Hi there' }] },
|
||||||
|
{ role: 'user', content: [{ type: 'text', text: 'How are you?' }] },
|
||||||
|
{ role: 'assistant', content: [{ type: 'text', text: 'I\'m doing well, thanks!' }] },
|
||||||
|
{ role: 'user', content: [{ type: 'text', text: 'Great!' }] },
|
||||||
|
];
|
||||||
|
|
||||||
|
const result = addCacheControl(messages);
|
||||||
|
|
||||||
|
expect(result[0].content[0]).not.toHaveProperty('cache_control');
|
||||||
|
expect(result[2].content[0].cache_control).toEqual({ type: 'ephemeral' });
|
||||||
|
expect(result[4].content[0].cache_control).toEqual({ type: 'ephemeral' });
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should add cache control to the last two user messages with string content', () => {
|
||||||
|
const messages = [
|
||||||
|
{ role: 'user', content: 'Hello' },
|
||||||
|
{ role: 'assistant', content: 'Hi there' },
|
||||||
|
{ role: 'user', content: 'How are you?' },
|
||||||
|
{ role: 'assistant', content: 'I\'m doing well, thanks!' },
|
||||||
|
{ role: 'user', content: 'Great!' },
|
||||||
|
];
|
||||||
|
|
||||||
|
const result = addCacheControl(messages);
|
||||||
|
|
||||||
|
expect(result[0].content).toBe('Hello');
|
||||||
|
expect(result[2].content[0]).toEqual({
|
||||||
|
type: 'text',
|
||||||
|
text: 'How are you?',
|
||||||
|
cache_control: { type: 'ephemeral' },
|
||||||
|
});
|
||||||
|
expect(result[4].content[0]).toEqual({
|
||||||
|
type: 'text',
|
||||||
|
text: 'Great!',
|
||||||
|
cache_control: { type: 'ephemeral' },
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should handle mixed string and array content', () => {
|
||||||
|
const messages = [
|
||||||
|
{ role: 'user', content: 'Hello' },
|
||||||
|
{ role: 'assistant', content: 'Hi there' },
|
||||||
|
{ role: 'user', content: [{ type: 'text', text: 'How are you?' }] },
|
||||||
|
];
|
||||||
|
|
||||||
|
const result = addCacheControl(messages);
|
||||||
|
|
||||||
|
expect(result[0].content[0]).toEqual({
|
||||||
|
type: 'text',
|
||||||
|
text: 'Hello',
|
||||||
|
cache_control: { type: 'ephemeral' },
|
||||||
|
});
|
||||||
|
expect(result[2].content[0].cache_control).toEqual({ type: 'ephemeral' });
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should handle less than two user messages', () => {
|
||||||
|
const messages = [
|
||||||
|
{ role: 'user', content: 'Hello' },
|
||||||
|
{ role: 'assistant', content: 'Hi there' },
|
||||||
|
];
|
||||||
|
|
||||||
|
const result = addCacheControl(messages);
|
||||||
|
|
||||||
|
expect(result[0].content[0]).toEqual({
|
||||||
|
type: 'text',
|
||||||
|
text: 'Hello',
|
||||||
|
cache_control: { type: 'ephemeral' },
|
||||||
|
});
|
||||||
|
expect(result[1].content).toBe('Hi there');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should return original array if no user messages', () => {
|
||||||
|
const messages = [
|
||||||
|
{ role: 'assistant', content: 'Hi there' },
|
||||||
|
{ role: 'assistant', content: 'How can I help?' },
|
||||||
|
];
|
||||||
|
|
||||||
|
const result = addCacheControl(messages);
|
||||||
|
|
||||||
|
expect(result).toEqual(messages);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should handle empty array', () => {
|
||||||
|
const messages = [];
|
||||||
|
const result = addCacheControl(messages);
|
||||||
|
expect(result).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should handle non-array input', () => {
|
||||||
|
const messages = 'not an array';
|
||||||
|
const result = addCacheControl(messages);
|
||||||
|
expect(result).toBe('not an array');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should not modify assistant messages', () => {
|
||||||
|
const messages = [
|
||||||
|
{ role: 'user', content: 'Hello' },
|
||||||
|
{ role: 'assistant', content: 'Hi there' },
|
||||||
|
{ role: 'user', content: 'How are you?' },
|
||||||
|
];
|
||||||
|
|
||||||
|
const result = addCacheControl(messages);
|
||||||
|
|
||||||
|
expect(result[1].content).toBe('Hi there');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should handle multiple content items in user messages', () => {
|
||||||
|
const messages = [
|
||||||
|
{
|
||||||
|
role: 'user',
|
||||||
|
content: [
|
||||||
|
{ type: 'text', text: 'Hello' },
|
||||||
|
{ type: 'image', url: 'http://example.com/image.jpg' },
|
||||||
|
{ type: 'text', text: 'This is an image' },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{ role: 'assistant', content: 'Hi there' },
|
||||||
|
{ role: 'user', content: 'How are you?' },
|
||||||
|
];
|
||||||
|
|
||||||
|
const result = addCacheControl(messages);
|
||||||
|
|
||||||
|
expect(result[0].content[0]).not.toHaveProperty('cache_control');
|
||||||
|
expect(result[0].content[1]).not.toHaveProperty('cache_control');
|
||||||
|
expect(result[0].content[2].cache_control).toEqual({ type: 'ephemeral' });
|
||||||
|
expect(result[2].content[0]).toEqual({
|
||||||
|
type: 'text',
|
||||||
|
text: 'How are you?',
|
||||||
|
cache_control: { type: 'ephemeral' },
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should handle an array with mixed content types', () => {
|
||||||
|
const messages = [
|
||||||
|
{ role: 'user', content: 'Hello' },
|
||||||
|
{ role: 'assistant', content: 'Hi there' },
|
||||||
|
{ role: 'user', content: [{ type: 'text', text: 'How are you?' }] },
|
||||||
|
{ role: 'assistant', content: 'I\'m doing well, thanks!' },
|
||||||
|
{ role: 'user', content: 'Great!' },
|
||||||
|
];
|
||||||
|
|
||||||
|
const result = addCacheControl(messages);
|
||||||
|
|
||||||
|
expect(result[0].content).toEqual('Hello');
|
||||||
|
expect(result[2].content[0]).toEqual({
|
||||||
|
type: 'text',
|
||||||
|
text: 'How are you?',
|
||||||
|
cache_control: { type: 'ephemeral' },
|
||||||
|
});
|
||||||
|
expect(result[4].content).toEqual([
|
||||||
|
{
|
||||||
|
type: 'text',
|
||||||
|
text: 'Great!',
|
||||||
|
cache_control: { type: 'ephemeral' },
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
expect(result[1].content).toBe('Hi there');
|
||||||
|
expect(result[3].content).toBe('I\'m doing well, thanks!');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should handle edge case with multiple content types', () => {
|
||||||
|
const messages = [
|
||||||
|
{
|
||||||
|
role: 'user',
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: 'image',
|
||||||
|
source: { type: 'base64', media_type: 'image/png', data: 'some_base64_string' },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'image',
|
||||||
|
source: { type: 'base64', media_type: 'image/png', data: 'another_base64_string' },
|
||||||
|
},
|
||||||
|
{ type: 'text', text: 'what do all these images have in common' },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{ role: 'assistant', content: 'I see multiple images.' },
|
||||||
|
{ role: 'user', content: 'Correct!' },
|
||||||
|
];
|
||||||
|
|
||||||
|
const result = addCacheControl(messages);
|
||||||
|
|
||||||
|
expect(result[0].content[0]).not.toHaveProperty('cache_control');
|
||||||
|
expect(result[0].content[1]).not.toHaveProperty('cache_control');
|
||||||
|
expect(result[0].content[2].cache_control).toEqual({ type: 'ephemeral' });
|
||||||
|
expect(result[2].content[0]).toEqual({
|
||||||
|
type: 'text',
|
||||||
|
text: 'Correct!',
|
||||||
|
cache_control: { type: 'ephemeral' },
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should handle user message with no text block', () => {
|
||||||
|
const messages = [
|
||||||
|
{
|
||||||
|
role: 'user',
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: 'image',
|
||||||
|
source: { type: 'base64', media_type: 'image/png', data: 'some_base64_string' },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'image',
|
||||||
|
source: { type: 'base64', media_type: 'image/png', data: 'another_base64_string' },
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{ role: 'assistant', content: 'I see two images.' },
|
||||||
|
{ role: 'user', content: 'Correct!' },
|
||||||
|
];
|
||||||
|
|
||||||
|
const result = addCacheControl(messages);
|
||||||
|
|
||||||
|
expect(result[0].content[0]).not.toHaveProperty('cache_control');
|
||||||
|
expect(result[0].content[1]).not.toHaveProperty('cache_control');
|
||||||
|
expect(result[2].content[0]).toEqual({
|
||||||
|
type: 'text',
|
||||||
|
text: 'Correct!',
|
||||||
|
cache_control: { type: 'ephemeral' },
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
527
api/app/clients/prompts/artifacts.js
Normal file
527
api/app/clients/prompts/artifacts.js
Normal file
|
|
@ -0,0 +1,527 @@
|
||||||
|
const dedent = require('dedent');
|
||||||
|
const { EModelEndpoint, ArtifactModes } = require('librechat-data-provider');
|
||||||
|
const { generateShadcnPrompt } = require('~/app/clients/prompts/shadcn-docs/generate');
|
||||||
|
const { components } = require('~/app/clients/prompts/shadcn-docs/components');
|
||||||
|
|
||||||
|
// eslint-disable-next-line no-unused-vars
|
||||||
|
const artifactsPromptV1 = dedent`The assistant can create and reference artifacts during conversations.
|
||||||
|
|
||||||
|
Artifacts are for substantial, self-contained content that users might modify or reuse, displayed in a separate UI window for clarity.
|
||||||
|
|
||||||
|
# Good artifacts are...
|
||||||
|
- Substantial content (>15 lines)
|
||||||
|
- Content that the user is likely to modify, iterate on, or take ownership of
|
||||||
|
- Self-contained, complex content that can be understood on its own, without context from the conversation
|
||||||
|
- Content intended for eventual use outside the conversation (e.g., reports, emails, presentations)
|
||||||
|
- Content likely to be referenced or reused multiple times
|
||||||
|
|
||||||
|
# Don't use artifacts for...
|
||||||
|
- Simple, informational, or short content, such as brief code snippets, mathematical equations, or small examples
|
||||||
|
- Primarily explanatory, instructional, or illustrative content, such as examples provided to clarify a concept
|
||||||
|
- Suggestions, commentary, or feedback on existing artifacts
|
||||||
|
- Conversational or explanatory content that doesn't represent a standalone piece of work
|
||||||
|
- Content that is dependent on the current conversational context to be useful
|
||||||
|
- Content that is unlikely to be modified or iterated upon by the user
|
||||||
|
- Request from users that appears to be a one-off question
|
||||||
|
|
||||||
|
# Usage notes
|
||||||
|
- One artifact per message unless specifically requested
|
||||||
|
- Prefer in-line content (don't use artifacts) when possible. Unnecessary use of artifacts can be jarring for users.
|
||||||
|
- If a user asks the assistant to "draw an SVG" or "make a website," the assistant does not need to explain that it doesn't have these capabilities. Creating the code and placing it within the appropriate artifact will fulfill the user's intentions.
|
||||||
|
- If asked to generate an image, the assistant can offer an SVG instead. The assistant isn't very proficient at making SVG images but should engage with the task positively. Self-deprecating humor about its abilities can make it an entertaining experience for users.
|
||||||
|
- The assistant errs on the side of simplicity and avoids overusing artifacts for content that can be effectively presented within the conversation.
|
||||||
|
- Always provide complete, specific, and fully functional content without any placeholders, ellipses, or 'remains the same' comments.
|
||||||
|
|
||||||
|
<artifact_instructions>
|
||||||
|
When collaborating with the user on creating content that falls into compatible categories, the assistant should follow these steps:
|
||||||
|
|
||||||
|
1. Create the artifact using the following format:
|
||||||
|
|
||||||
|
:::artifact{identifier="unique-identifier" type="mime-type" title="Artifact Title"}
|
||||||
|
\`\`\`
|
||||||
|
Your artifact content here
|
||||||
|
\`\`\`
|
||||||
|
:::
|
||||||
|
|
||||||
|
2. Assign an identifier to the \`identifier\` attribute. For updates, reuse the prior identifier. For new artifacts, the identifier should be descriptive and relevant to the content, using kebab-case (e.g., "example-code-snippet"). This identifier will be used consistently throughout the artifact's lifecycle, even when updating or iterating on the artifact.
|
||||||
|
3. Include a \`title\` attribute to provide a brief title or description of the content.
|
||||||
|
4. Add a \`type\` attribute to specify the type of content the artifact represents. Assign one of the following values to the \`type\` attribute:
|
||||||
|
- HTML: "text/html"
|
||||||
|
- The user interface can render single file HTML pages placed within the artifact tags. HTML, JS, and CSS should be in a single file when using the \`text/html\` type.
|
||||||
|
- Images from the web are not allowed, but you can use placeholder images by specifying the width and height like so \`<img src="/api/placeholder/400/320" alt="placeholder" />\`
|
||||||
|
- The only place external scripts can be imported from is https://cdnjs.cloudflare.com
|
||||||
|
- Mermaid Diagrams: "application/vnd.mermaid"
|
||||||
|
- The user interface will render Mermaid diagrams placed within the artifact tags.
|
||||||
|
- React Components: "application/vnd.react"
|
||||||
|
- Use this for displaying either: React elements, e.g. \`<strong>Hello World!</strong>\`, React pure functional components, e.g. \`() => <strong>Hello World!</strong>\`, React functional components with Hooks, or React component classes
|
||||||
|
- When creating a React component, ensure it has no required props (or provide default values for all props) and use a default export.
|
||||||
|
- Use Tailwind classes for styling. DO NOT USE ARBITRARY VALUES (e.g. \`h-[600px]\`).
|
||||||
|
- Base React is available to be imported. To use hooks, first import it at the top of the artifact, e.g. \`import { useState } from "react"\`
|
||||||
|
- The lucide-react@0.263.1 library is available to be imported. e.g. \`import { Camera } from "lucide-react"\` & \`<Camera color="red" size={48} />\`
|
||||||
|
- The recharts charting library is available to be imported, e.g. \`import { LineChart, XAxis, ... } from "recharts"\` & \`<LineChart ...><XAxis dataKey="name"> ...\`
|
||||||
|
- The assistant can use prebuilt components from the \`shadcn/ui\` library after it is imported: \`import { Alert, AlertDescription, AlertTitle, AlertDialog, AlertDialogAction } from '/components/ui/alert';\`. If using components from the shadcn/ui library, the assistant mentions this to the user and offers to help them install the components if necessary.
|
||||||
|
- Components MUST be imported from \`/components/ui/name\` and NOT from \`/components/name\` or \`@/components/ui/name\`.
|
||||||
|
- NO OTHER LIBRARIES (e.g. zod, hookform) ARE INSTALLED OR ABLE TO BE IMPORTED.
|
||||||
|
- Images from the web are not allowed, but you can use placeholder images by specifying the width and height like so \`<img src="/api/placeholder/400/320" alt="placeholder" />\`
|
||||||
|
- If you are unable to follow the above requirements for any reason, don't use artifacts and use regular code blocks instead, which will not attempt to render the component.
|
||||||
|
5. Include the complete and updated content of the artifact, without any truncation or minimization. Don't use "// rest of the code remains the same...".
|
||||||
|
6. If unsure whether the content qualifies as an artifact, if an artifact should be updated, or which type to assign to an artifact, err on the side of not creating an artifact.
|
||||||
|
7. Always use triple backticks (\`\`\`) to enclose the content within the artifact, regardless of the content type.
|
||||||
|
</artifact_instructions>
|
||||||
|
|
||||||
|
Here are some examples of correct usage of artifacts:
|
||||||
|
|
||||||
|
<examples>
|
||||||
|
<example_docstring>
|
||||||
|
This example demonstrates how to create a Mermaid artifact for a simple flow chart.
|
||||||
|
</example_docstring>
|
||||||
|
|
||||||
|
<example>
|
||||||
|
<user_query>Can you create a simple flow chart showing the process of making tea using Mermaid?</user_query>
|
||||||
|
|
||||||
|
<assistant_response>
|
||||||
|
Sure! Here's a simple flow chart depicting the process of making tea using Mermaid syntax:
|
||||||
|
|
||||||
|
:::artifact{identifier="tea-making-flowchart" type="application/vnd.mermaid" title="Flow chart: Making Tea"}
|
||||||
|
\`\`\`mermaid
|
||||||
|
graph TD
|
||||||
|
A[Start] --> B{Water boiled?}
|
||||||
|
B -->|Yes| C[Add tea leaves to cup]
|
||||||
|
B -->|No| D[Boil water]
|
||||||
|
D --> B
|
||||||
|
C --> E[Pour boiling water into cup]
|
||||||
|
E --> F[Steep tea for desired time]
|
||||||
|
F --> G[Remove tea leaves]
|
||||||
|
G --> H[Add milk or sugar, if desired]
|
||||||
|
H --> I[Enjoy your tea!]
|
||||||
|
I --> J[End]
|
||||||
|
\`\`\`
|
||||||
|
:::
|
||||||
|
|
||||||
|
This flow chart uses Mermaid syntax to visualize the steps involved in making a cup of tea. Here's a brief explanation of the process:
|
||||||
|
|
||||||
|
1. Start
|
||||||
|
2. Check if water is boiled
|
||||||
|
3. If not boiled, boil the water
|
||||||
|
4. Once water is boiled, add tea leaves to the cup
|
||||||
|
5. Pour boiling water into the cup
|
||||||
|
6. Steep the tea for the desired time
|
||||||
|
7. Remove the tea leaves
|
||||||
|
8. Optionally add milk or sugar
|
||||||
|
9. Enjoy your tea!
|
||||||
|
10. End
|
||||||
|
|
||||||
|
This chart provides a clear visual representation of the tea-making process. You can easily modify or expand this chart if you want to add more details or steps to the process. Let me know if you'd like any changes or have any questions!
|
||||||
|
</assistant_response>
|
||||||
|
</example>
|
||||||
|
</examples>`;
|
||||||
|
const artifactsPrompt = dedent`The assistant can create and reference artifacts during conversations.
|
||||||
|
|
||||||
|
Artifacts are for substantial, self-contained content that users might modify or reuse, displayed in a separate UI window for clarity.
|
||||||
|
|
||||||
|
# Good artifacts are...
|
||||||
|
- Substantial content (>15 lines)
|
||||||
|
- Content that the user is likely to modify, iterate on, or take ownership of
|
||||||
|
- Self-contained, complex content that can be understood on its own, without context from the conversation
|
||||||
|
- Content intended for eventual use outside the conversation (e.g., reports, emails, presentations)
|
||||||
|
- Content likely to be referenced or reused multiple times
|
||||||
|
|
||||||
|
# Don't use artifacts for...
|
||||||
|
- Simple, informational, or short content, such as brief code snippets, mathematical equations, or small examples
|
||||||
|
- Primarily explanatory, instructional, or illustrative content, such as examples provided to clarify a concept
|
||||||
|
- Suggestions, commentary, or feedback on existing artifacts
|
||||||
|
- Conversational or explanatory content that doesn't represent a standalone piece of work
|
||||||
|
- Content that is dependent on the current conversational context to be useful
|
||||||
|
- Content that is unlikely to be modified or iterated upon by the user
|
||||||
|
- Request from users that appears to be a one-off question
|
||||||
|
|
||||||
|
# Usage notes
|
||||||
|
- One artifact per message unless specifically requested
|
||||||
|
- Prefer in-line content (don't use artifacts) when possible. Unnecessary use of artifacts can be jarring for users.
|
||||||
|
- If a user asks the assistant to "draw an SVG" or "make a website," the assistant does not need to explain that it doesn't have these capabilities. Creating the code and placing it within the appropriate artifact will fulfill the user's intentions.
|
||||||
|
- If asked to generate an image, the assistant can offer an SVG instead. The assistant isn't very proficient at making SVG images but should engage with the task positively. Self-deprecating humor about its abilities can make it an entertaining experience for users.
|
||||||
|
- The assistant errs on the side of simplicity and avoids overusing artifacts for content that can be effectively presented within the conversation.
|
||||||
|
- Always provide complete, specific, and fully functional content for artifacts without any snippets, placeholders, ellipses, or 'remains the same' comments.
|
||||||
|
- If an artifact is not necessary or requested, the assistant should not mention artifacts at all, and respond to the user accordingly.
|
||||||
|
|
||||||
|
<artifact_instructions>
|
||||||
|
When collaborating with the user on creating content that falls into compatible categories, the assistant should follow these steps:
|
||||||
|
|
||||||
|
1. Create the artifact using the following format:
|
||||||
|
|
||||||
|
:::artifact{identifier="unique-identifier" type="mime-type" title="Artifact Title"}
|
||||||
|
\`\`\`
|
||||||
|
Your artifact content here
|
||||||
|
\`\`\`
|
||||||
|
:::
|
||||||
|
|
||||||
|
2. Assign an identifier to the \`identifier\` attribute. For updates, reuse the prior identifier. For new artifacts, the identifier should be descriptive and relevant to the content, using kebab-case (e.g., "example-code-snippet"). This identifier will be used consistently throughout the artifact's lifecycle, even when updating or iterating on the artifact.
|
||||||
|
3. Include a \`title\` attribute to provide a brief title or description of the content.
|
||||||
|
4. Add a \`type\` attribute to specify the type of content the artifact represents. Assign one of the following values to the \`type\` attribute:
|
||||||
|
- HTML: "text/html"
|
||||||
|
- The user interface can render single file HTML pages placed within the artifact tags. HTML, JS, and CSS should be in a single file when using the \`text/html\` type.
|
||||||
|
- Images from the web are not allowed, but you can use placeholder images by specifying the width and height like so \`<img src="/api/placeholder/400/320" alt="placeholder" />\`
|
||||||
|
- The only place external scripts can be imported from is https://cdnjs.cloudflare.com
|
||||||
|
- SVG: "image/svg+xml"
|
||||||
|
- The user interface will render the Scalable Vector Graphics (SVG) image within the artifact tags.
|
||||||
|
- The assistant should specify the viewbox of the SVG rather than defining a width/height
|
||||||
|
- Mermaid Diagrams: "application/vnd.mermaid"
|
||||||
|
- The user interface will render Mermaid diagrams placed within the artifact tags.
|
||||||
|
- React Components: "application/vnd.react"
|
||||||
|
- Use this for displaying either: React elements, e.g. \`<strong>Hello World!</strong>\`, React pure functional components, e.g. \`() => <strong>Hello World!</strong>\`, React functional components with Hooks, or React component classes
|
||||||
|
- When creating a React component, ensure it has no required props (or provide default values for all props) and use a default export.
|
||||||
|
- Use Tailwind classes for styling. DO NOT USE ARBITRARY VALUES (e.g. \`h-[600px]\`).
|
||||||
|
- Base React is available to be imported. To use hooks, first import it at the top of the artifact, e.g. \`import { useState } from "react"\`
|
||||||
|
- The lucide-react@0.394.0 library is available to be imported. e.g. \`import { Camera } from "lucide-react"\` & \`<Camera color="red" size={48} />\`
|
||||||
|
- The recharts charting library is available to be imported, e.g. \`import { LineChart, XAxis, ... } from "recharts"\` & \`<LineChart ...><XAxis dataKey="name"> ...\`
|
||||||
|
- The three.js library is available to be imported, e.g. \`import * as THREE from "three";\`
|
||||||
|
- The date-fns library is available to be imported, e.g. \`import { compareAsc, format } from "date-fns";\`
|
||||||
|
- The react-day-picker library is available to be imported, e.g. \`import { DayPicker } from "react-day-picker";\`
|
||||||
|
- The assistant can use prebuilt components from the \`shadcn/ui\` library after it is imported: \`import { Alert, AlertDescription, AlertTitle, AlertDialog, AlertDialogAction } from '/components/ui/alert';\`. If using components from the shadcn/ui library, the assistant mentions this to the user and offers to help them install the components if necessary.
|
||||||
|
- Components MUST be imported from \`/components/ui/name\` and NOT from \`/components/name\` or \`@/components/ui/name\`.
|
||||||
|
- NO OTHER LIBRARIES (e.g. zod, hookform) ARE INSTALLED OR ABLE TO BE IMPORTED.
|
||||||
|
- Images from the web are not allowed, but you can use placeholder images by specifying the width and height like so \`<img src="/api/placeholder/400/320" alt="placeholder" />\`
|
||||||
|
- When iterating on code, ensure that the code is complete and functional without any snippets, placeholders, or ellipses.
|
||||||
|
- If you are unable to follow the above requirements for any reason, don't use artifacts and use regular code blocks instead, which will not attempt to render the component.
|
||||||
|
5. Include the complete and updated content of the artifact, without any truncation or minimization. Don't use "// rest of the code remains the same...".
|
||||||
|
6. If unsure whether the content qualifies as an artifact, if an artifact should be updated, or which type to assign to an artifact, err on the side of not creating an artifact.
|
||||||
|
7. Always use triple backticks (\`\`\`) to enclose the content within the artifact, regardless of the content type.
|
||||||
|
</artifact_instructions>
|
||||||
|
|
||||||
|
Here are some examples of correct usage of artifacts:
|
||||||
|
|
||||||
|
<examples>
|
||||||
|
<example_docstring>
|
||||||
|
This example demonstrates how to create a Mermaid artifact for a simple flow chart.
|
||||||
|
</example_docstring>
|
||||||
|
|
||||||
|
<example>
|
||||||
|
<user_query>Can you create a simple flow chart showing the process of making tea using Mermaid?</user_query>
|
||||||
|
|
||||||
|
<assistant_response>
|
||||||
|
Sure! Here's a simple flow chart depicting the process of making tea using Mermaid syntax:
|
||||||
|
|
||||||
|
:::artifact{identifier="tea-making-flowchart" type="application/vnd.mermaid" title="Flow chart: Making Tea"}
|
||||||
|
\`\`\`mermaid
|
||||||
|
graph TD
|
||||||
|
A[Start] --> B{Water boiled?}
|
||||||
|
B -->|Yes| C[Add tea leaves to cup]
|
||||||
|
B -->|No| D[Boil water]
|
||||||
|
D --> B
|
||||||
|
C --> E[Pour boiling water into cup]
|
||||||
|
E --> F[Steep tea for desired time]
|
||||||
|
F --> G[Remove tea leaves]
|
||||||
|
G --> H[Add milk or sugar, if desired]
|
||||||
|
H --> I[Enjoy your tea!]
|
||||||
|
I --> J[End]
|
||||||
|
\`\`\`
|
||||||
|
:::
|
||||||
|
|
||||||
|
This flow chart uses Mermaid syntax to visualize the steps involved in making a cup of tea. Here's a brief explanation of the process:
|
||||||
|
|
||||||
|
1. Start
|
||||||
|
2. Check if water is boiled
|
||||||
|
3. If not boiled, boil the water
|
||||||
|
4. Once water is boiled, add tea leaves to the cup
|
||||||
|
5. Pour boiling water into the cup
|
||||||
|
6. Steep the tea for the desired time
|
||||||
|
7. Remove the tea leaves
|
||||||
|
8. Optionally add milk or sugar
|
||||||
|
9. Enjoy your tea!
|
||||||
|
10. End
|
||||||
|
|
||||||
|
This chart provides a clear visual representation of the tea-making process. You can easily modify or expand this chart if you want to add more details or steps to the process. Let me know if you'd like any changes or have any questions!
|
||||||
|
</assistant_response>
|
||||||
|
</example>
|
||||||
|
|
||||||
|
<example>
|
||||||
|
<user_query>Create a simple React counter component</user_query>
|
||||||
|
<assistant_response>
|
||||||
|
Here's a simple React counter component:
|
||||||
|
|
||||||
|
:::artifact{identifier="react-counter" type="application/vnd.react" title="React Counter"}
|
||||||
|
\`\`\`
|
||||||
|
import { useState } from 'react';
|
||||||
|
|
||||||
|
export default function Counter() {
|
||||||
|
const [count, setCount] = useState(0);
|
||||||
|
return (
|
||||||
|
<div className="p-4">
|
||||||
|
<p className="mb-2">Count: {count}</p>
|
||||||
|
<button className="bg-blue-500 text-white px-4 py-2 rounded" onClick={() => setCount(count + 1)}>
|
||||||
|
Increment
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
\`\`\`
|
||||||
|
:::
|
||||||
|
|
||||||
|
This component creates a simple counter with an increment button.
|
||||||
|
</assistant_response>
|
||||||
|
</example>
|
||||||
|
|
||||||
|
<example>
|
||||||
|
<user_query>Create a basic HTML structure for a blog post</user_query>
|
||||||
|
<assistant_response>
|
||||||
|
Here's a basic HTML structure for a blog post:
|
||||||
|
|
||||||
|
:::artifact{identifier="blog-post-html" type="text/html" title="Blog Post HTML"}
|
||||||
|
\`\`\`
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>My Blog Post</title>
|
||||||
|
<style>
|
||||||
|
body { font-family: Arial, sans-serif; line-height: 1.6; max-width: 800px; margin: 0 auto; padding: 20px; }
|
||||||
|
h1 { color: #333; }
|
||||||
|
p { margin-bottom: 15px; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<header>
|
||||||
|
<h1>My First Blog Post</h1>
|
||||||
|
</header>
|
||||||
|
<main>
|
||||||
|
<article>
|
||||||
|
<p>This is the content of my blog post. It's short and sweet!</p>
|
||||||
|
</article>
|
||||||
|
</main>
|
||||||
|
<footer>
|
||||||
|
<p>© 2023 My Blog</p>
|
||||||
|
</footer>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
\`\`\`
|
||||||
|
:::
|
||||||
|
|
||||||
|
This HTML structure provides a simple layout for a blog post.
|
||||||
|
</assistant_response>
|
||||||
|
</example>
|
||||||
|
</examples>`;
|
||||||
|
|
||||||
|
const artifactsOpenAIPrompt = dedent`The assistant can create and reference artifacts during conversations.
|
||||||
|
|
||||||
|
Artifacts are for substantial, self-contained content that users might modify or reuse, displayed in a separate UI window for clarity.
|
||||||
|
|
||||||
|
# Good artifacts are...
|
||||||
|
- Substantial content (>15 lines)
|
||||||
|
- Content that the user is likely to modify, iterate on, or take ownership of
|
||||||
|
- Self-contained, complex content that can be understood on its own, without context from the conversation
|
||||||
|
- Content intended for eventual use outside the conversation (e.g., reports, emails, presentations)
|
||||||
|
- Content likely to be referenced or reused multiple times
|
||||||
|
|
||||||
|
# Don't use artifacts for...
|
||||||
|
- Simple, informational, or short content, such as brief code snippets, mathematical equations, or small examples
|
||||||
|
- Primarily explanatory, instructional, or illustrative content, such as examples provided to clarify a concept
|
||||||
|
- Suggestions, commentary, or feedback on existing artifacts
|
||||||
|
- Conversational or explanatory content that doesn't represent a standalone piece of work
|
||||||
|
- Content that is dependent on the current conversational context to be useful
|
||||||
|
- Content that is unlikely to be modified or iterated upon by the user
|
||||||
|
- Request from users that appears to be a one-off question
|
||||||
|
|
||||||
|
# Usage notes
|
||||||
|
- One artifact per message unless specifically requested
|
||||||
|
- Prefer in-line content (don't use artifacts) when possible. Unnecessary use of artifacts can be jarring for users.
|
||||||
|
- If a user asks the assistant to "draw an SVG" or "make a website," the assistant does not need to explain that it doesn't have these capabilities. Creating the code and placing it within the appropriate artifact will fulfill the user's intentions.
|
||||||
|
- If asked to generate an image, the assistant can offer an SVG instead. The assistant isn't very proficient at making SVG images but should engage with the task positively. Self-deprecating humor about its abilities can make it an entertaining experience for users.
|
||||||
|
- The assistant errs on the side of simplicity and avoids overusing artifacts for content that can be effectively presented within the conversation.
|
||||||
|
- Always provide complete, specific, and fully functional content for artifacts without any snippets, placeholders, ellipses, or 'remains the same' comments.
|
||||||
|
- If an artifact is not necessary or requested, the assistant should not mention artifacts at all, and respond to the user accordingly.
|
||||||
|
|
||||||
|
## Artifact Instructions
|
||||||
|
When collaborating with the user on creating content that falls into compatible categories, the assistant should follow these steps:
|
||||||
|
|
||||||
|
1. Create the artifact using the following remark-directive markdown format:
|
||||||
|
|
||||||
|
:::artifact{identifier="unique-identifier" type="mime-type" title="Artifact Title"}
|
||||||
|
\`\`\`
|
||||||
|
Your artifact content here
|
||||||
|
\`\`\`
|
||||||
|
:::
|
||||||
|
|
||||||
|
a. Example of correct format:
|
||||||
|
|
||||||
|
:::artifact{identifier="example-artifact" type="text/plain" title="Example Artifact"}
|
||||||
|
\`\`\`
|
||||||
|
This is the content of the artifact.
|
||||||
|
It can span multiple lines.
|
||||||
|
\`\`\`
|
||||||
|
:::
|
||||||
|
|
||||||
|
b. Common mistakes to avoid:
|
||||||
|
- Don't split the opening ::: line
|
||||||
|
- Don't add extra backticks outside the artifact structure
|
||||||
|
- Don't omit the closing :::
|
||||||
|
|
||||||
|
2. Assign an identifier to the \`identifier\` attribute. For updates, reuse the prior identifier. For new artifacts, the identifier should be descriptive and relevant to the content, using kebab-case (e.g., "example-code-snippet"). This identifier will be used consistently throughout the artifact's lifecycle, even when updating or iterating on the artifact.
|
||||||
|
3. Include a \`title\` attribute to provide a brief title or description of the content.
|
||||||
|
4. Add a \`type\` attribute to specify the type of content the artifact represents. Assign one of the following values to the \`type\` attribute:
|
||||||
|
- HTML: "text/html"
|
||||||
|
- The user interface can render single file HTML pages placed within the artifact tags. HTML, JS, and CSS should be in a single file when using the \`text/html\` type.
|
||||||
|
- Images from the web are not allowed, but you can use placeholder images by specifying the width and height like so \`<img src="/api/placeholder/400/320" alt="placeholder" />\`
|
||||||
|
- The only place external scripts can be imported from is https://cdnjs.cloudflare.com
|
||||||
|
- SVG: "image/svg+xml"
|
||||||
|
- The user interface will render the Scalable Vector Graphics (SVG) image within the artifact tags.
|
||||||
|
- The assistant should specify the viewbox of the SVG rather than defining a width/height
|
||||||
|
- Mermaid Diagrams: "application/vnd.mermaid"
|
||||||
|
- The user interface will render Mermaid diagrams placed within the artifact tags.
|
||||||
|
- React Components: "application/vnd.react"
|
||||||
|
- Use this for displaying either: React elements, e.g. \`<strong>Hello World!</strong>\`, React pure functional components, e.g. \`() => <strong>Hello World!</strong>\`, React functional components with Hooks, or React component classes
|
||||||
|
- When creating a React component, ensure it has no required props (or provide default values for all props) and use a default export.
|
||||||
|
- Use Tailwind classes for styling. DO NOT USE ARBITRARY VALUES (e.g. \`h-[600px]\`).
|
||||||
|
- Base React is available to be imported. To use hooks, first import it at the top of the artifact, e.g. \`import { useState } from "react"\`
|
||||||
|
- The lucide-react@0.394.0 library is available to be imported. e.g. \`import { Camera } from "lucide-react"\` & \`<Camera color="red" size={48} />\`
|
||||||
|
- The recharts charting library is available to be imported, e.g. \`import { LineChart, XAxis, ... } from "recharts"\` & \`<LineChart ...><XAxis dataKey="name"> ...\`
|
||||||
|
- The three.js library is available to be imported, e.g. \`import * as THREE from "three";\`
|
||||||
|
- The date-fns library is available to be imported, e.g. \`import { compareAsc, format } from "date-fns";\`
|
||||||
|
- The react-day-picker library is available to be imported, e.g. \`import { DayPicker } from "react-day-picker";\`
|
||||||
|
- The assistant can use prebuilt components from the \`shadcn/ui\` library after it is imported: \`import { Alert, AlertDescription, AlertTitle, AlertDialog, AlertDialogAction } from '/components/ui/alert';\`. If using components from the shadcn/ui library, the assistant mentions this to the user and offers to help them install the components if necessary.
|
||||||
|
- Components MUST be imported from \`/components/ui/name\` and NOT from \`/components/name\` or \`@/components/ui/name\`.
|
||||||
|
- NO OTHER LIBRARIES (e.g. zod, hookform) ARE INSTALLED OR ABLE TO BE IMPORTED.
|
||||||
|
- Images from the web are not allowed, but you can use placeholder images by specifying the width and height like so \`<img src="/api/placeholder/400/320" alt="placeholder" />\`
|
||||||
|
- When iterating on code, ensure that the code is complete and functional without any snippets, placeholders, or ellipses.
|
||||||
|
- If you are unable to follow the above requirements for any reason, don't use artifacts and use regular code blocks instead, which will not attempt to render the component.
|
||||||
|
5. Include the complete and updated content of the artifact, without any truncation or minimization. Don't use "// rest of the code remains the same...".
|
||||||
|
6. If unsure whether the content qualifies as an artifact, if an artifact should be updated, or which type to assign to an artifact, err on the side of not creating an artifact.
|
||||||
|
7. NEVER use triple backticks to enclose the artifact, ONLY the content within the artifact.
|
||||||
|
|
||||||
|
Here are some examples of correct usage of artifacts:
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
### Example 1
|
||||||
|
|
||||||
|
This example demonstrates how to create a Mermaid artifact for a simple flow chart.
|
||||||
|
|
||||||
|
User: Can you create a simple flow chart showing the process of making tea using Mermaid?
|
||||||
|
|
||||||
|
Assistant: Sure! Here's a simple flow chart depicting the process of making tea using Mermaid syntax:
|
||||||
|
|
||||||
|
:::artifact{identifier="tea-making-flowchart" type="application/vnd.mermaid" title="Flow chart: Making Tea"}
|
||||||
|
\`\`\`mermaid
|
||||||
|
graph TD
|
||||||
|
A[Start] --> B{Water boiled?}
|
||||||
|
B -->|Yes| C[Add tea leaves to cup]
|
||||||
|
B -->|No| D[Boil water]
|
||||||
|
D --> B
|
||||||
|
C --> E[Pour boiling water into cup]
|
||||||
|
E --> F[Steep tea for desired time]
|
||||||
|
F --> G[Remove tea leaves]
|
||||||
|
G --> H[Add milk or sugar, if desired]
|
||||||
|
H --> I[Enjoy your tea!]
|
||||||
|
I --> J[End]
|
||||||
|
\`\`\`
|
||||||
|
:::
|
||||||
|
|
||||||
|
This flow chart uses Mermaid syntax to visualize the steps involved in making a cup of tea. Here's a brief explanation of the process:
|
||||||
|
|
||||||
|
1. Start
|
||||||
|
2. Check if water is boiled
|
||||||
|
3. If not boiled, boil the water
|
||||||
|
4. Once water is boiled, add tea leaves to the cup
|
||||||
|
5. Pour boiling water into the cup
|
||||||
|
6. Steep the tea for the desired time
|
||||||
|
7. Remove the tea leaves
|
||||||
|
8. Optionally add milk or sugar
|
||||||
|
9. Enjoy your tea!
|
||||||
|
10. End
|
||||||
|
|
||||||
|
This chart provides a clear visual representation of the tea-making process. You can easily modify or expand this chart if you want to add more details or steps to the process. Let me know if you'd like any changes or have any questions!
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Example 2
|
||||||
|
|
||||||
|
User: Create a simple React counter component
|
||||||
|
|
||||||
|
Assistant: Here's a simple React counter component:
|
||||||
|
|
||||||
|
:::artifact{identifier="react-counter" type="application/vnd.react" title="React Counter"}
|
||||||
|
\`\`\`
|
||||||
|
import { useState } from 'react';
|
||||||
|
|
||||||
|
export default function Counter() {
|
||||||
|
const [count, setCount] = useState(0);
|
||||||
|
return (
|
||||||
|
<div className="p-4">
|
||||||
|
<p className="mb-2">Count: {count}</p>
|
||||||
|
<button className="bg-blue-500 text-white px-4 py-2 rounded" onClick={() => setCount(count + 1)}>
|
||||||
|
Increment
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
\`\`\`
|
||||||
|
:::
|
||||||
|
|
||||||
|
This component creates a simple counter with an increment button.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Example 3
|
||||||
|
User: Create a basic HTML structure for a blog post
|
||||||
|
Assistant: Here's a basic HTML structure for a blog post:
|
||||||
|
|
||||||
|
:::artifact{identifier="blog-post-html" type="text/html" title="Blog Post HTML"}
|
||||||
|
\`\`\`
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>My Blog Post</title>
|
||||||
|
<style>
|
||||||
|
body { font-family: Arial, sans-serif; line-height: 1.6; max-width: 800px; margin: 0 auto; padding: 20px; }
|
||||||
|
h1 { color: #333; }
|
||||||
|
p { margin-bottom: 15px; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<header>
|
||||||
|
<h1>My First Blog Post</h1>
|
||||||
|
</header>
|
||||||
|
<main>
|
||||||
|
<article>
|
||||||
|
<p>This is the content of my blog post. It's short and sweet!</p>
|
||||||
|
</article>
|
||||||
|
</main>
|
||||||
|
<footer>
|
||||||
|
<p>© 2023 My Blog</p>
|
||||||
|
</footer>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
\`\`\`
|
||||||
|
:::
|
||||||
|
|
||||||
|
This HTML structure provides a simple layout for a blog post.
|
||||||
|
|
||||||
|
---`;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {Object} params
|
||||||
|
* @param {EModelEndpoint | string} params.endpoint - The current endpoint
|
||||||
|
* @param {ArtifactModes} params.artifacts - The current artifact mode
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
const generateArtifactsPrompt = ({ endpoint, artifacts }) => {
|
||||||
|
if (artifacts === ArtifactModes.CUSTOM) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
let prompt = artifactsPrompt;
|
||||||
|
if (endpoint !== EModelEndpoint.anthropic) {
|
||||||
|
prompt = artifactsOpenAIPrompt;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (artifacts === ArtifactModes.SHADCNUI) {
|
||||||
|
prompt += generateShadcnPrompt({ components, useXML: endpoint === EModelEndpoint.anthropic });
|
||||||
|
}
|
||||||
|
|
||||||
|
return prompt;
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = generateArtifactsPrompt;
|
||||||
160
api/app/clients/prompts/createContextHandlers.js
Normal file
160
api/app/clients/prompts/createContextHandlers.js
Normal file
|
|
@ -0,0 +1,160 @@
|
||||||
|
const axios = require('axios');
|
||||||
|
const { isEnabled } = require('~/server/utils');
|
||||||
|
const { logger } = require('~/config');
|
||||||
|
|
||||||
|
const footer = `Use the context as your learned knowledge to better answer the user.
|
||||||
|
|
||||||
|
In your response, remember to follow these guidelines:
|
||||||
|
- If you don't know the answer, simply say that you don't know.
|
||||||
|
- If you are unsure how to answer, ask for clarification.
|
||||||
|
- Avoid mentioning that you obtained the information from the context.
|
||||||
|
`;
|
||||||
|
|
||||||
|
function createContextHandlers(req, userMessageContent) {
|
||||||
|
if (!process.env.RAG_API_URL) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const queryPromises = [];
|
||||||
|
const processedFiles = [];
|
||||||
|
const processedIds = new Set();
|
||||||
|
const jwtToken = req.headers.authorization.split(' ')[1];
|
||||||
|
const useFullContext = isEnabled(process.env.RAG_USE_FULL_CONTEXT);
|
||||||
|
|
||||||
|
const query = async (file) => {
|
||||||
|
if (useFullContext) {
|
||||||
|
return axios.get(`${process.env.RAG_API_URL}/documents/${file.file_id}/context`, {
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${jwtToken}`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return axios.post(
|
||||||
|
`${process.env.RAG_API_URL}/query`,
|
||||||
|
{
|
||||||
|
file_id: file.file_id,
|
||||||
|
query: userMessageContent,
|
||||||
|
k: 4,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${jwtToken}`,
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const processFile = async (file) => {
|
||||||
|
if (file.embedded && !processedIds.has(file.file_id)) {
|
||||||
|
try {
|
||||||
|
const promise = query(file);
|
||||||
|
queryPromises.push(promise);
|
||||||
|
processedFiles.push(file);
|
||||||
|
processedIds.add(file.file_id);
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(`Error processing file ${file.filename}:`, error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const createContext = async () => {
|
||||||
|
try {
|
||||||
|
if (!queryPromises.length || !processedFiles.length) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
const oneFile = processedFiles.length === 1;
|
||||||
|
const header = `The user has attached ${oneFile ? 'a' : processedFiles.length} file${
|
||||||
|
!oneFile ? 's' : ''
|
||||||
|
} to the conversation:`;
|
||||||
|
|
||||||
|
const files = `${
|
||||||
|
oneFile
|
||||||
|
? ''
|
||||||
|
: `
|
||||||
|
<files>`
|
||||||
|
}${processedFiles
|
||||||
|
.map(
|
||||||
|
(file) => `
|
||||||
|
<file>
|
||||||
|
<filename>${file.filename}</filename>
|
||||||
|
<type>${file.type}</type>
|
||||||
|
</file>`,
|
||||||
|
)
|
||||||
|
.join('')}${
|
||||||
|
oneFile
|
||||||
|
? ''
|
||||||
|
: `
|
||||||
|
</files>`
|
||||||
|
}`;
|
||||||
|
|
||||||
|
const resolvedQueries = await Promise.all(queryPromises);
|
||||||
|
|
||||||
|
const context =
|
||||||
|
resolvedQueries.length === 0
|
||||||
|
? '\n\tThe semantic search did not return any results.'
|
||||||
|
: resolvedQueries
|
||||||
|
.map((queryResult, index) => {
|
||||||
|
const file = processedFiles[index];
|
||||||
|
let contextItems = queryResult.data;
|
||||||
|
|
||||||
|
const generateContext = (currentContext) =>
|
||||||
|
`
|
||||||
|
<file>
|
||||||
|
<filename>${file.filename}</filename>
|
||||||
|
<context>${currentContext}
|
||||||
|
</context>
|
||||||
|
</file>`;
|
||||||
|
|
||||||
|
if (useFullContext) {
|
||||||
|
return generateContext(`\n${contextItems}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
contextItems = queryResult.data
|
||||||
|
.map((item) => {
|
||||||
|
const pageContent = item[0].page_content;
|
||||||
|
return `
|
||||||
|
<contextItem>
|
||||||
|
<![CDATA[${pageContent?.trim()}]]>
|
||||||
|
</contextItem>`;
|
||||||
|
})
|
||||||
|
.join('');
|
||||||
|
|
||||||
|
return generateContext(contextItems);
|
||||||
|
})
|
||||||
|
.join('');
|
||||||
|
|
||||||
|
if (useFullContext) {
|
||||||
|
const prompt = `${header}
|
||||||
|
${context}
|
||||||
|
${footer}`;
|
||||||
|
|
||||||
|
return prompt;
|
||||||
|
}
|
||||||
|
|
||||||
|
const prompt = `${header}
|
||||||
|
${files}
|
||||||
|
|
||||||
|
A semantic search was executed with the user's message as the query, retrieving the following context inside <context></context> XML tags.
|
||||||
|
|
||||||
|
<context>${context}
|
||||||
|
</context>
|
||||||
|
|
||||||
|
${footer}`;
|
||||||
|
|
||||||
|
return prompt;
|
||||||
|
} catch (error) {
|
||||||
|
logger.error('Error creating context:', error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
processFile,
|
||||||
|
createContext,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = createContextHandlers;
|
||||||
34
api/app/clients/prompts/createVisionPrompt.js
Normal file
34
api/app/clients/prompts/createVisionPrompt.js
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
/**
|
||||||
|
* Generates a prompt instructing the user to describe an image in detail, tailored to different types of visual content.
|
||||||
|
* @param {boolean} pluralized - Whether to pluralize the prompt for multiple images.
|
||||||
|
* @returns {string} - The generated vision prompt.
|
||||||
|
*/
|
||||||
|
const createVisionPrompt = (pluralized = false) => {
|
||||||
|
return `Please describe the image${
|
||||||
|
pluralized ? 's' : ''
|
||||||
|
} in detail, covering relevant aspects such as:
|
||||||
|
|
||||||
|
For photographs, illustrations, or artwork:
|
||||||
|
- The main subject(s) and their appearance, positioning, and actions
|
||||||
|
- The setting, background, and any notable objects or elements
|
||||||
|
- Colors, lighting, and overall mood or atmosphere
|
||||||
|
- Any interesting details, textures, or patterns
|
||||||
|
- The style, technique, or medium used (if discernible)
|
||||||
|
|
||||||
|
For screenshots or images containing text:
|
||||||
|
- The content and purpose of the text
|
||||||
|
- The layout, formatting, and organization of the information
|
||||||
|
- Any notable visual elements, such as logos, icons, or graphics
|
||||||
|
- The overall context or message conveyed by the screenshot
|
||||||
|
|
||||||
|
For graphs, charts, or data visualizations:
|
||||||
|
- The type of graph or chart (e.g., bar graph, line chart, pie chart)
|
||||||
|
- The variables being compared or analyzed
|
||||||
|
- Any trends, patterns, or outliers in the data
|
||||||
|
- The axis labels, scales, and units of measurement
|
||||||
|
- The title, legend, and any additional context provided
|
||||||
|
|
||||||
|
Be as specific and descriptive as possible while maintaining clarity and concision.`;
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = createVisionPrompt;
|
||||||
285
api/app/clients/prompts/formatAgentMessages.spec.js
Normal file
285
api/app/clients/prompts/formatAgentMessages.spec.js
Normal file
|
|
@ -0,0 +1,285 @@
|
||||||
|
const { ToolMessage } = require('@langchain/core/messages');
|
||||||
|
const { ContentTypes } = require('librechat-data-provider');
|
||||||
|
const { HumanMessage, AIMessage, SystemMessage } = require('@langchain/core/messages');
|
||||||
|
const { formatAgentMessages } = require('./formatMessages');
|
||||||
|
|
||||||
|
describe('formatAgentMessages', () => {
|
||||||
|
it('should format simple user and AI messages', () => {
|
||||||
|
const payload = [
|
||||||
|
{ role: 'user', content: 'Hello' },
|
||||||
|
{ role: 'assistant', content: 'Hi there!' },
|
||||||
|
];
|
||||||
|
const result = formatAgentMessages(payload);
|
||||||
|
expect(result).toHaveLength(2);
|
||||||
|
expect(result[0]).toBeInstanceOf(HumanMessage);
|
||||||
|
expect(result[1]).toBeInstanceOf(AIMessage);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle system messages', () => {
|
||||||
|
const payload = [{ role: 'system', content: 'You are a helpful assistant.' }];
|
||||||
|
const result = formatAgentMessages(payload);
|
||||||
|
expect(result).toHaveLength(1);
|
||||||
|
expect(result[0]).toBeInstanceOf(SystemMessage);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should format messages with content arrays', () => {
|
||||||
|
const payload = [
|
||||||
|
{
|
||||||
|
role: 'user',
|
||||||
|
content: [{ type: ContentTypes.TEXT, [ContentTypes.TEXT]: 'Hello' }],
|
||||||
|
},
|
||||||
|
];
|
||||||
|
const result = formatAgentMessages(payload);
|
||||||
|
expect(result).toHaveLength(1);
|
||||||
|
expect(result[0]).toBeInstanceOf(HumanMessage);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle tool calls and create ToolMessages', () => {
|
||||||
|
const payload = [
|
||||||
|
{
|
||||||
|
role: 'assistant',
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: ContentTypes.TEXT,
|
||||||
|
[ContentTypes.TEXT]: 'Let me check that for you.',
|
||||||
|
tool_call_ids: ['123'],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: ContentTypes.TOOL_CALL,
|
||||||
|
tool_call: {
|
||||||
|
id: '123',
|
||||||
|
name: 'search',
|
||||||
|
args: '{"query":"weather"}',
|
||||||
|
output: 'The weather is sunny.',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
];
|
||||||
|
const result = formatAgentMessages(payload);
|
||||||
|
expect(result).toHaveLength(2);
|
||||||
|
expect(result[0]).toBeInstanceOf(AIMessage);
|
||||||
|
expect(result[1]).toBeInstanceOf(ToolMessage);
|
||||||
|
expect(result[0].tool_calls).toHaveLength(1);
|
||||||
|
expect(result[1].tool_call_id).toBe('123');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle multiple content parts in assistant messages', () => {
|
||||||
|
const payload = [
|
||||||
|
{
|
||||||
|
role: 'assistant',
|
||||||
|
content: [
|
||||||
|
{ type: ContentTypes.TEXT, [ContentTypes.TEXT]: 'Part 1' },
|
||||||
|
{ type: ContentTypes.TEXT, [ContentTypes.TEXT]: 'Part 2' },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
];
|
||||||
|
const result = formatAgentMessages(payload);
|
||||||
|
expect(result).toHaveLength(1);
|
||||||
|
expect(result[0]).toBeInstanceOf(AIMessage);
|
||||||
|
expect(result[0].content).toHaveLength(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw an error for invalid tool call structure', () => {
|
||||||
|
const payload = [
|
||||||
|
{
|
||||||
|
role: 'assistant',
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: ContentTypes.TOOL_CALL,
|
||||||
|
tool_call: {
|
||||||
|
id: '123',
|
||||||
|
name: 'search',
|
||||||
|
args: '{"query":"weather"}',
|
||||||
|
output: 'The weather is sunny.',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
];
|
||||||
|
expect(() => formatAgentMessages(payload)).toThrow('Invalid tool call structure');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle tool calls with non-JSON args', () => {
|
||||||
|
const payload = [
|
||||||
|
{
|
||||||
|
role: 'assistant',
|
||||||
|
content: [
|
||||||
|
{ type: ContentTypes.TEXT, [ContentTypes.TEXT]: 'Checking...', tool_call_ids: ['123'] },
|
||||||
|
{
|
||||||
|
type: ContentTypes.TOOL_CALL,
|
||||||
|
tool_call: {
|
||||||
|
id: '123',
|
||||||
|
name: 'search',
|
||||||
|
args: 'non-json-string',
|
||||||
|
output: 'Result',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
];
|
||||||
|
const result = formatAgentMessages(payload);
|
||||||
|
expect(result).toHaveLength(2);
|
||||||
|
expect(result[0].tool_calls[0].args).toStrictEqual({ input: 'non-json-string' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle complex tool calls with multiple steps', () => {
|
||||||
|
const payload = [
|
||||||
|
{
|
||||||
|
role: 'assistant',
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: ContentTypes.TEXT,
|
||||||
|
[ContentTypes.TEXT]: 'I\'ll search for that information.',
|
||||||
|
tool_call_ids: ['search_1'],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: ContentTypes.TOOL_CALL,
|
||||||
|
tool_call: {
|
||||||
|
id: 'search_1',
|
||||||
|
name: 'search',
|
||||||
|
args: '{"query":"weather in New York"}',
|
||||||
|
output: 'The weather in New York is currently sunny with a temperature of 75°F.',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: ContentTypes.TEXT,
|
||||||
|
[ContentTypes.TEXT]: 'Now, I\'ll convert the temperature.',
|
||||||
|
tool_call_ids: ['convert_1'],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: ContentTypes.TOOL_CALL,
|
||||||
|
tool_call: {
|
||||||
|
id: 'convert_1',
|
||||||
|
name: 'convert_temperature',
|
||||||
|
args: '{"temperature": 75, "from": "F", "to": "C"}',
|
||||||
|
output: '23.89°C',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{ type: ContentTypes.TEXT, [ContentTypes.TEXT]: 'Here\'s your answer.' },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const result = formatAgentMessages(payload);
|
||||||
|
|
||||||
|
expect(result).toHaveLength(5);
|
||||||
|
expect(result[0]).toBeInstanceOf(AIMessage);
|
||||||
|
expect(result[1]).toBeInstanceOf(ToolMessage);
|
||||||
|
expect(result[2]).toBeInstanceOf(AIMessage);
|
||||||
|
expect(result[3]).toBeInstanceOf(ToolMessage);
|
||||||
|
expect(result[4]).toBeInstanceOf(AIMessage);
|
||||||
|
|
||||||
|
// Check first AIMessage
|
||||||
|
expect(result[0].content).toBe('I\'ll search for that information.');
|
||||||
|
expect(result[0].tool_calls).toHaveLength(1);
|
||||||
|
expect(result[0].tool_calls[0]).toEqual({
|
||||||
|
id: 'search_1',
|
||||||
|
name: 'search',
|
||||||
|
args: { query: 'weather in New York' },
|
||||||
|
});
|
||||||
|
|
||||||
|
// Check first ToolMessage
|
||||||
|
expect(result[1].tool_call_id).toBe('search_1');
|
||||||
|
expect(result[1].name).toBe('search');
|
||||||
|
expect(result[1].content).toBe(
|
||||||
|
'The weather in New York is currently sunny with a temperature of 75°F.',
|
||||||
|
);
|
||||||
|
|
||||||
|
// Check second AIMessage
|
||||||
|
expect(result[2].content).toBe('Now, I\'ll convert the temperature.');
|
||||||
|
expect(result[2].tool_calls).toHaveLength(1);
|
||||||
|
expect(result[2].tool_calls[0]).toEqual({
|
||||||
|
id: 'convert_1',
|
||||||
|
name: 'convert_temperature',
|
||||||
|
args: { temperature: 75, from: 'F', to: 'C' },
|
||||||
|
});
|
||||||
|
|
||||||
|
// Check second ToolMessage
|
||||||
|
expect(result[3].tool_call_id).toBe('convert_1');
|
||||||
|
expect(result[3].name).toBe('convert_temperature');
|
||||||
|
expect(result[3].content).toBe('23.89°C');
|
||||||
|
|
||||||
|
// Check final AIMessage
|
||||||
|
expect(result[4].content).toStrictEqual([
|
||||||
|
{ [ContentTypes.TEXT]: 'Here\'s your answer.', type: ContentTypes.TEXT },
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it.skip('should not produce two consecutive assistant messages and format content correctly', () => {
|
||||||
|
const payload = [
|
||||||
|
{ role: 'user', content: 'Hello' },
|
||||||
|
{
|
||||||
|
role: 'assistant',
|
||||||
|
content: [{ type: ContentTypes.TEXT, [ContentTypes.TEXT]: 'Hi there!' }],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
role: 'assistant',
|
||||||
|
content: [{ type: ContentTypes.TEXT, [ContentTypes.TEXT]: 'How can I help you?' }],
|
||||||
|
},
|
||||||
|
{ role: 'user', content: 'What\'s the weather?' },
|
||||||
|
{
|
||||||
|
role: 'assistant',
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: ContentTypes.TEXT,
|
||||||
|
[ContentTypes.TEXT]: 'Let me check that for you.',
|
||||||
|
tool_call_ids: ['weather_1'],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: ContentTypes.TOOL_CALL,
|
||||||
|
tool_call: {
|
||||||
|
id: 'weather_1',
|
||||||
|
name: 'check_weather',
|
||||||
|
args: '{"location":"New York"}',
|
||||||
|
output: 'Sunny, 75°F',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
role: 'assistant',
|
||||||
|
content: [
|
||||||
|
{ type: ContentTypes.TEXT, [ContentTypes.TEXT]: 'Here\'s the weather information.' },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const result = formatAgentMessages(payload);
|
||||||
|
|
||||||
|
// Check correct message count and types
|
||||||
|
expect(result).toHaveLength(6);
|
||||||
|
expect(result[0]).toBeInstanceOf(HumanMessage);
|
||||||
|
expect(result[1]).toBeInstanceOf(AIMessage);
|
||||||
|
expect(result[2]).toBeInstanceOf(HumanMessage);
|
||||||
|
expect(result[3]).toBeInstanceOf(AIMessage);
|
||||||
|
expect(result[4]).toBeInstanceOf(ToolMessage);
|
||||||
|
expect(result[5]).toBeInstanceOf(AIMessage);
|
||||||
|
|
||||||
|
// Check content of messages
|
||||||
|
expect(result[0].content).toStrictEqual([
|
||||||
|
{ [ContentTypes.TEXT]: 'Hello', type: ContentTypes.TEXT },
|
||||||
|
]);
|
||||||
|
expect(result[1].content).toStrictEqual([
|
||||||
|
{ [ContentTypes.TEXT]: 'Hi there!', type: ContentTypes.TEXT },
|
||||||
|
{ [ContentTypes.TEXT]: 'How can I help you?', type: ContentTypes.TEXT },
|
||||||
|
]);
|
||||||
|
expect(result[2].content).toStrictEqual([
|
||||||
|
{ [ContentTypes.TEXT]: 'What\'s the weather?', type: ContentTypes.TEXT },
|
||||||
|
]);
|
||||||
|
expect(result[3].content).toBe('Let me check that for you.');
|
||||||
|
expect(result[4].content).toBe('Sunny, 75°F');
|
||||||
|
expect(result[5].content).toStrictEqual([
|
||||||
|
{ [ContentTypes.TEXT]: 'Here\'s the weather information.', type: ContentTypes.TEXT },
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Check that there are no consecutive AIMessages
|
||||||
|
const messageTypes = result.map((message) => message.constructor);
|
||||||
|
for (let i = 0; i < messageTypes.length - 1; i++) {
|
||||||
|
expect(messageTypes[i] === AIMessage && messageTypes[i + 1] === AIMessage).toBe(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Additional check to ensure the consecutive assistant messages were combined
|
||||||
|
expect(result[1].content).toHaveLength(2);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
const { EModelEndpoint } = require('librechat-data-provider');
|
const { ToolMessage } = require('@langchain/core/messages');
|
||||||
const { HumanMessage, AIMessage, SystemMessage } = require('langchain/schema');
|
const { EModelEndpoint, ContentTypes } = require('librechat-data-provider');
|
||||||
|
const { HumanMessage, AIMessage, SystemMessage } = require('@langchain/core/messages');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Formats a message to OpenAI Vision API payload format.
|
* Formats a message to OpenAI Vision API payload format.
|
||||||
|
|
@ -14,11 +15,11 @@ const { HumanMessage, AIMessage, SystemMessage } = require('langchain/schema');
|
||||||
*/
|
*/
|
||||||
const formatVisionMessage = ({ message, image_urls, endpoint }) => {
|
const formatVisionMessage = ({ message, image_urls, endpoint }) => {
|
||||||
if (endpoint === EModelEndpoint.anthropic) {
|
if (endpoint === EModelEndpoint.anthropic) {
|
||||||
message.content = [...image_urls, { type: 'text', text: message.content }];
|
message.content = [...image_urls, { type: ContentTypes.TEXT, text: message.content }];
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
||||||
message.content = [{ type: 'text', text: message.content }, ...image_urls];
|
message.content = [{ type: ContentTypes.TEXT, text: message.content }, ...image_urls];
|
||||||
|
|
||||||
return message;
|
return message;
|
||||||
};
|
};
|
||||||
|
|
@ -51,7 +52,7 @@ const formatMessage = ({ message, userName, assistantName, endpoint, langChain =
|
||||||
_role = roleMapping[lc_id[2]];
|
_role = roleMapping[lc_id[2]];
|
||||||
}
|
}
|
||||||
const role = _role ?? (sender && sender?.toLowerCase() === 'user' ? 'user' : 'assistant');
|
const role = _role ?? (sender && sender?.toLowerCase() === 'user' ? 'user' : 'assistant');
|
||||||
const content = text ?? _content ?? '';
|
const content = _content ?? text ?? '';
|
||||||
const formattedMessage = {
|
const formattedMessage = {
|
||||||
role,
|
role,
|
||||||
content,
|
content,
|
||||||
|
|
@ -131,4 +132,129 @@ const formatFromLangChain = (message) => {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = { formatMessage, formatLangChainMessages, formatFromLangChain };
|
/**
|
||||||
|
* Formats an array of messages for LangChain, handling tool calls and creating ToolMessage instances.
|
||||||
|
*
|
||||||
|
* @param {Array<Partial<TMessage>>} payload - The array of messages to format.
|
||||||
|
* @returns {Array<(HumanMessage|AIMessage|SystemMessage|ToolMessage)>} - The array of formatted LangChain messages, including ToolMessages for tool calls.
|
||||||
|
*/
|
||||||
|
const formatAgentMessages = (payload) => {
|
||||||
|
const messages = [];
|
||||||
|
|
||||||
|
for (const message of payload) {
|
||||||
|
if (typeof message.content === 'string') {
|
||||||
|
message.content = [{ type: ContentTypes.TEXT, [ContentTypes.TEXT]: message.content }];
|
||||||
|
}
|
||||||
|
if (message.role !== 'assistant') {
|
||||||
|
messages.push(formatMessage({ message, langChain: true }));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let currentContent = [];
|
||||||
|
let lastAIMessage = null;
|
||||||
|
|
||||||
|
for (const part of message.content) {
|
||||||
|
if (part.type === ContentTypes.TEXT && part.tool_call_ids) {
|
||||||
|
/*
|
||||||
|
If there's pending content, it needs to be aggregated as a single string to prepare for tool calls.
|
||||||
|
For Anthropic models, the "tool_calls" field on a message is only respected if content is a string.
|
||||||
|
*/
|
||||||
|
if (currentContent.length > 0) {
|
||||||
|
let content = currentContent.reduce((acc, curr) => {
|
||||||
|
if (curr.type === ContentTypes.TEXT) {
|
||||||
|
return `${acc}${curr[ContentTypes.TEXT]}\n`;
|
||||||
|
}
|
||||||
|
return acc;
|
||||||
|
}, '');
|
||||||
|
content = `${content}\n${part[ContentTypes.TEXT] ?? ''}`.trim();
|
||||||
|
lastAIMessage = new AIMessage({ content });
|
||||||
|
messages.push(lastAIMessage);
|
||||||
|
currentContent = [];
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a new AIMessage with this text and prepare for tool calls
|
||||||
|
lastAIMessage = new AIMessage({
|
||||||
|
content: part.text || '',
|
||||||
|
});
|
||||||
|
|
||||||
|
messages.push(lastAIMessage);
|
||||||
|
} else if (part.type === ContentTypes.TOOL_CALL) {
|
||||||
|
if (!lastAIMessage) {
|
||||||
|
throw new Error('Invalid tool call structure: No preceding AIMessage with tool_call_ids');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Note: `tool_calls` list is defined when constructed by `AIMessage` class, and outputs should be excluded from it
|
||||||
|
const { output, args: _args, ...tool_call } = part.tool_call;
|
||||||
|
// TODO: investigate; args as dictionary may need to be provider-or-tool-specific
|
||||||
|
let args = _args;
|
||||||
|
try {
|
||||||
|
args = JSON.parse(_args);
|
||||||
|
} catch (e) {
|
||||||
|
if (typeof _args === 'string') {
|
||||||
|
args = { input: _args };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tool_call.args = args;
|
||||||
|
lastAIMessage.tool_calls.push(tool_call);
|
||||||
|
|
||||||
|
// Add the corresponding ToolMessage
|
||||||
|
messages.push(
|
||||||
|
new ToolMessage({
|
||||||
|
tool_call_id: tool_call.id,
|
||||||
|
name: tool_call.name,
|
||||||
|
content: output || '',
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
currentContent.push(part);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (currentContent.length > 0) {
|
||||||
|
messages.push(new AIMessage({ content: currentContent }));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return messages;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Formats an array of messages for LangChain, making sure all content fields are strings
|
||||||
|
* @param {Array<(HumanMessage|AIMessage|SystemMessage|ToolMessage)>} payload - The array of messages to format.
|
||||||
|
* @returns {Array<(HumanMessage|AIMessage|SystemMessage|ToolMessage)>} - The array of formatted LangChain messages, including ToolMessages for tool calls.
|
||||||
|
*/
|
||||||
|
const formatContentStrings = (payload) => {
|
||||||
|
const messages = [];
|
||||||
|
|
||||||
|
for (const message of payload) {
|
||||||
|
if (typeof message.content === 'string') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Array.isArray(message.content)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reduce text types to a single string, ignore all other types
|
||||||
|
const content = message.content.reduce((acc, curr) => {
|
||||||
|
if (curr.type === ContentTypes.TEXT) {
|
||||||
|
return `${acc}${curr[ContentTypes.TEXT]}\n`;
|
||||||
|
}
|
||||||
|
return acc;
|
||||||
|
}, '');
|
||||||
|
|
||||||
|
message.content = content.trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
return messages;
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
formatMessage,
|
||||||
|
formatFromLangChain,
|
||||||
|
formatAgentMessages,
|
||||||
|
formatContentStrings,
|
||||||
|
formatLangChainMessages,
|
||||||
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
const { Constants } = require('librechat-data-provider');
|
const { Constants } = require('librechat-data-provider');
|
||||||
const { HumanMessage, AIMessage, SystemMessage } = require('langchain/schema');
|
const { HumanMessage, AIMessage, SystemMessage } = require('@langchain/core/messages');
|
||||||
const { formatMessage, formatLangChainMessages, formatFromLangChain } = require('./formatMessages');
|
const { formatMessage, formatLangChainMessages, formatFromLangChain } = require('./formatMessages');
|
||||||
|
|
||||||
describe('formatMessage', () => {
|
describe('formatMessage', () => {
|
||||||
|
|
@ -60,7 +60,6 @@ describe('formatMessage', () => {
|
||||||
error: false,
|
error: false,
|
||||||
finish_reason: null,
|
finish_reason: null,
|
||||||
isCreatedByUser: true,
|
isCreatedByUser: true,
|
||||||
isEdited: false,
|
|
||||||
model: null,
|
model: null,
|
||||||
parentMessageId: Constants.NO_PARENT,
|
parentMessageId: Constants.NO_PARENT,
|
||||||
sender: 'User',
|
sender: 'User',
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,21 @@
|
||||||
|
const addCacheControl = require('./addCacheControl');
|
||||||
const formatMessages = require('./formatMessages');
|
const formatMessages = require('./formatMessages');
|
||||||
const summaryPrompts = require('./summaryPrompts');
|
const summaryPrompts = require('./summaryPrompts');
|
||||||
const handleInputs = require('./handleInputs');
|
const handleInputs = require('./handleInputs');
|
||||||
const instructions = require('./instructions');
|
const instructions = require('./instructions');
|
||||||
const titlePrompts = require('./titlePrompts');
|
const titlePrompts = require('./titlePrompts');
|
||||||
const truncateText = require('./truncateText');
|
const truncate = require('./truncate');
|
||||||
|
const createVisionPrompt = require('./createVisionPrompt');
|
||||||
|
const createContextHandlers = require('./createContextHandlers');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
addCacheControl,
|
||||||
...formatMessages,
|
...formatMessages,
|
||||||
...summaryPrompts,
|
...summaryPrompts,
|
||||||
...handleInputs,
|
...handleInputs,
|
||||||
...instructions,
|
...instructions,
|
||||||
...titlePrompts,
|
...titlePrompts,
|
||||||
truncateText,
|
...truncate,
|
||||||
|
createVisionPrompt,
|
||||||
|
createContextHandlers,
|
||||||
};
|
};
|
||||||
|
|
|
||||||
495
api/app/clients/prompts/shadcn-docs/components.js
Normal file
495
api/app/clients/prompts/shadcn-docs/components.js
Normal file
|
|
@ -0,0 +1,495 @@
|
||||||
|
// Essential Components
|
||||||
|
const essentialComponents = {
|
||||||
|
avatar: {
|
||||||
|
componentName: 'Avatar',
|
||||||
|
importDocs: 'import { Avatar, AvatarFallback, AvatarImage } from "/components/ui/avatar"',
|
||||||
|
usageDocs: `
|
||||||
|
<Avatar>
|
||||||
|
<AvatarImage src="https://github.com/shadcn.png" />
|
||||||
|
<AvatarFallback>CN</AvatarFallback>
|
||||||
|
</Avatar>`,
|
||||||
|
},
|
||||||
|
button: {
|
||||||
|
componentName: 'Button',
|
||||||
|
importDocs: 'import { Button } from "/components/ui/button"',
|
||||||
|
usageDocs: `
|
||||||
|
<Button variant="outline">Button</Button>`,
|
||||||
|
},
|
||||||
|
card: {
|
||||||
|
componentName: 'Card',
|
||||||
|
importDocs: `
|
||||||
|
import {
|
||||||
|
Card,
|
||||||
|
CardContent,
|
||||||
|
CardDescription,
|
||||||
|
CardFooter,
|
||||||
|
CardHeader,
|
||||||
|
CardTitle,
|
||||||
|
} from "/components/ui/card"`,
|
||||||
|
usageDocs: `
|
||||||
|
<Card>
|
||||||
|
<CardHeader>
|
||||||
|
<CardTitle>Card Title</CardTitle>
|
||||||
|
<CardDescription>Card Description</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent>
|
||||||
|
<p>Card Content</p>
|
||||||
|
</CardContent>
|
||||||
|
<CardFooter>
|
||||||
|
<p>Card Footer</p>
|
||||||
|
</CardFooter>
|
||||||
|
</Card>`,
|
||||||
|
},
|
||||||
|
checkbox: {
|
||||||
|
componentName: 'Checkbox',
|
||||||
|
importDocs: 'import { Checkbox } from "/components/ui/checkbox"',
|
||||||
|
usageDocs: '<Checkbox />',
|
||||||
|
},
|
||||||
|
input: {
|
||||||
|
componentName: 'Input',
|
||||||
|
importDocs: 'import { Input } from "/components/ui/input"',
|
||||||
|
usageDocs: '<Input />',
|
||||||
|
},
|
||||||
|
label: {
|
||||||
|
componentName: 'Label',
|
||||||
|
importDocs: 'import { Label } from "/components/ui/label"',
|
||||||
|
usageDocs: '<Label htmlFor="email">Your email address</Label>',
|
||||||
|
},
|
||||||
|
radioGroup: {
|
||||||
|
componentName: 'RadioGroup',
|
||||||
|
importDocs: `
|
||||||
|
import { Label } from "/components/ui/label"
|
||||||
|
import { RadioGroup, RadioGroupItem } from "/components/ui/radio-group"`,
|
||||||
|
usageDocs: `
|
||||||
|
<RadioGroup defaultValue="option-one">
|
||||||
|
<div className="flex items-center space-x-2">
|
||||||
|
<RadioGroupItem value="option-one" id="option-one" />
|
||||||
|
<Label htmlFor="option-one">Option One</Label>
|
||||||
|
</div>
|
||||||
|
<div className="flex items-center space-x-2">
|
||||||
|
<RadioGroupItem value="option-two" id="option-two" />
|
||||||
|
<Label htmlFor="option-two">Option Two</Label>
|
||||||
|
</div>
|
||||||
|
</RadioGroup>`,
|
||||||
|
},
|
||||||
|
select: {
|
||||||
|
componentName: 'Select',
|
||||||
|
importDocs: `
|
||||||
|
import {
|
||||||
|
Select,
|
||||||
|
SelectContent,
|
||||||
|
SelectItem,
|
||||||
|
SelectTrigger,
|
||||||
|
SelectValue,
|
||||||
|
} from "/components/ui/select"`,
|
||||||
|
usageDocs: `
|
||||||
|
<Select>
|
||||||
|
<SelectTrigger className="w-[180px]">
|
||||||
|
<SelectValue placeholder="Theme" />
|
||||||
|
</SelectTrigger>
|
||||||
|
<SelectContent>
|
||||||
|
<SelectItem value="light">Light</SelectItem>
|
||||||
|
<SelectItem value="dark">Dark</SelectItem>
|
||||||
|
<SelectItem value="system">System</SelectItem>
|
||||||
|
</SelectContent>
|
||||||
|
</Select>`,
|
||||||
|
},
|
||||||
|
textarea: {
|
||||||
|
componentName: 'Textarea',
|
||||||
|
importDocs: 'import { Textarea } from "/components/ui/textarea"',
|
||||||
|
usageDocs: '<Textarea />',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
// Extra Components
|
||||||
|
const extraComponents = {
|
||||||
|
accordion: {
|
||||||
|
componentName: 'Accordion',
|
||||||
|
importDocs: `
|
||||||
|
import {
|
||||||
|
Accordion,
|
||||||
|
AccordionContent,
|
||||||
|
AccordionItem,
|
||||||
|
AccordionTrigger,
|
||||||
|
} from "/components/ui/accordion"`,
|
||||||
|
usageDocs: `
|
||||||
|
<Accordion type="single" collapsible>
|
||||||
|
<AccordionItem value="item-1">
|
||||||
|
<AccordionTrigger>Is it accessible?</AccordionTrigger>
|
||||||
|
<AccordionContent>
|
||||||
|
Yes. It adheres to the WAI-ARIA design pattern.
|
||||||
|
</AccordionContent>
|
||||||
|
</AccordionItem>
|
||||||
|
</Accordion>`,
|
||||||
|
},
|
||||||
|
alertDialog: {
|
||||||
|
componentName: 'AlertDialog',
|
||||||
|
importDocs: `
|
||||||
|
import {
|
||||||
|
AlertDialog,
|
||||||
|
AlertDialogAction,
|
||||||
|
AlertDialogCancel,
|
||||||
|
AlertDialogContent,
|
||||||
|
AlertDialogDescription,
|
||||||
|
AlertDialogFooter,
|
||||||
|
AlertDialogHeader,
|
||||||
|
AlertDialogTitle,
|
||||||
|
AlertDialogTrigger,
|
||||||
|
} from "/components/ui/alert-dialog"`,
|
||||||
|
usageDocs: `
|
||||||
|
<AlertDialog>
|
||||||
|
<AlertDialogTrigger>Open</AlertDialogTrigger>
|
||||||
|
<AlertDialogContent>
|
||||||
|
<AlertDialogHeader>
|
||||||
|
<AlertDialogTitle>Are you absolutely sure?</AlertDialogTitle>
|
||||||
|
<AlertDialogDescription>
|
||||||
|
This action cannot be undone.
|
||||||
|
</AlertDialogDescription>
|
||||||
|
</AlertDialogHeader>
|
||||||
|
<AlertDialogFooter>
|
||||||
|
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
||||||
|
<AlertDialogAction>Continue</AlertDialogAction>
|
||||||
|
</AlertDialogFooter>
|
||||||
|
</AlertDialogContent>
|
||||||
|
</AlertDialog>`,
|
||||||
|
},
|
||||||
|
alert: {
|
||||||
|
componentName: 'Alert',
|
||||||
|
importDocs: `
|
||||||
|
import {
|
||||||
|
Alert,
|
||||||
|
AlertDescription,
|
||||||
|
AlertTitle,
|
||||||
|
} from "/components/ui/alert"`,
|
||||||
|
usageDocs: `
|
||||||
|
<Alert>
|
||||||
|
<AlertTitle>Heads up!</AlertTitle>
|
||||||
|
<AlertDescription>
|
||||||
|
You can add components to your app using the cli.
|
||||||
|
</AlertDescription>
|
||||||
|
</Alert>`,
|
||||||
|
},
|
||||||
|
aspectRatio: {
|
||||||
|
componentName: 'AspectRatio',
|
||||||
|
importDocs: 'import { AspectRatio } from "/components/ui/aspect-ratio"',
|
||||||
|
usageDocs: `
|
||||||
|
<AspectRatio ratio={16 / 9}>
|
||||||
|
<Image src="..." alt="Image" className="rounded-md object-cover" />
|
||||||
|
</AspectRatio>`,
|
||||||
|
},
|
||||||
|
badge: {
|
||||||
|
componentName: 'Badge',
|
||||||
|
importDocs: 'import { Badge } from "/components/ui/badge"',
|
||||||
|
usageDocs: '<Badge>Badge</Badge>',
|
||||||
|
},
|
||||||
|
calendar: {
|
||||||
|
componentName: 'Calendar',
|
||||||
|
importDocs: 'import { Calendar } from "/components/ui/calendar"',
|
||||||
|
usageDocs: '<Calendar />',
|
||||||
|
},
|
||||||
|
carousel: {
|
||||||
|
componentName: 'Carousel',
|
||||||
|
importDocs: `
|
||||||
|
import {
|
||||||
|
Carousel,
|
||||||
|
CarouselContent,
|
||||||
|
CarouselItem,
|
||||||
|
CarouselNext,
|
||||||
|
CarouselPrevious,
|
||||||
|
} from "/components/ui/carousel"`,
|
||||||
|
usageDocs: `
|
||||||
|
<Carousel>
|
||||||
|
<CarouselContent>
|
||||||
|
<CarouselItem>...</CarouselItem>
|
||||||
|
<CarouselItem>...</CarouselItem>
|
||||||
|
<CarouselItem>...</CarouselItem>
|
||||||
|
</CarouselContent>
|
||||||
|
<CarouselPrevious />
|
||||||
|
<CarouselNext />
|
||||||
|
</Carousel>`,
|
||||||
|
},
|
||||||
|
collapsible: {
|
||||||
|
componentName: 'Collapsible',
|
||||||
|
importDocs: `
|
||||||
|
import {
|
||||||
|
Collapsible,
|
||||||
|
CollapsibleContent,
|
||||||
|
CollapsibleTrigger,
|
||||||
|
} from "/components/ui/collapsible"`,
|
||||||
|
usageDocs: `
|
||||||
|
<Collapsible>
|
||||||
|
<CollapsibleTrigger>Can I use this in my project?</CollapsibleTrigger>
|
||||||
|
<CollapsibleContent>
|
||||||
|
Yes. Free to use for personal and commercial projects. No attribution required.
|
||||||
|
</CollapsibleContent>
|
||||||
|
</Collapsible>`,
|
||||||
|
},
|
||||||
|
dialog: {
|
||||||
|
componentName: 'Dialog',
|
||||||
|
importDocs: `
|
||||||
|
import {
|
||||||
|
Dialog,
|
||||||
|
DialogContent,
|
||||||
|
DialogDescription,
|
||||||
|
DialogHeader,
|
||||||
|
DialogTitle,
|
||||||
|
DialogTrigger,
|
||||||
|
} from "/components/ui/dialog"`,
|
||||||
|
usageDocs: `
|
||||||
|
<Dialog>
|
||||||
|
<DialogTrigger>Open</DialogTrigger>
|
||||||
|
<DialogContent>
|
||||||
|
<DialogHeader>
|
||||||
|
<DialogTitle>Are you sure absolutely sure?</DialogTitle>
|
||||||
|
<DialogDescription>
|
||||||
|
This action cannot be undone.
|
||||||
|
</DialogDescription>
|
||||||
|
</DialogHeader>
|
||||||
|
</DialogContent>
|
||||||
|
</Dialog>`,
|
||||||
|
},
|
||||||
|
dropdownMenu: {
|
||||||
|
componentName: 'DropdownMenu',
|
||||||
|
importDocs: `
|
||||||
|
import {
|
||||||
|
DropdownMenu,
|
||||||
|
DropdownMenuContent,
|
||||||
|
DropdownMenuItem,
|
||||||
|
DropdownMenuLabel,
|
||||||
|
DropdownMenuSeparator,
|
||||||
|
DropdownMenuTrigger,
|
||||||
|
} from "/components/ui/dropdown-menu"`,
|
||||||
|
usageDocs: `
|
||||||
|
<DropdownMenu>
|
||||||
|
<DropdownMenuTrigger>Open</DropdownMenuTrigger>
|
||||||
|
<DropdownMenuContent>
|
||||||
|
<DropdownMenuLabel>My Account</DropdownMenuLabel>
|
||||||
|
<DropdownMenuSeparator />
|
||||||
|
<DropdownMenuItem>Profile</DropdownMenuItem>
|
||||||
|
<DropdownMenuItem>Billing</DropdownMenuItem>
|
||||||
|
<DropdownMenuItem>Team</DropdownMenuItem>
|
||||||
|
<DropdownMenuItem>Subscription</DropdownMenuItem>
|
||||||
|
</DropdownMenuContent>
|
||||||
|
</DropdownMenu>`,
|
||||||
|
},
|
||||||
|
menubar: {
|
||||||
|
componentName: 'Menubar',
|
||||||
|
importDocs: `
|
||||||
|
import {
|
||||||
|
Menubar,
|
||||||
|
MenubarContent,
|
||||||
|
MenubarItem,
|
||||||
|
MenubarMenu,
|
||||||
|
MenubarSeparator,
|
||||||
|
MenubarShortcut,
|
||||||
|
MenubarTrigger,
|
||||||
|
} from "/components/ui/menubar"`,
|
||||||
|
usageDocs: `
|
||||||
|
<Menubar>
|
||||||
|
<MenubarMenu>
|
||||||
|
<MenubarTrigger>File</MenubarTrigger>
|
||||||
|
<MenubarContent>
|
||||||
|
<MenubarItem>
|
||||||
|
New Tab <MenubarShortcut>⌘T</MenubarShortcut>
|
||||||
|
</MenubarItem>
|
||||||
|
<MenubarItem>New Window</MenubarItem>
|
||||||
|
<MenubarSeparator />
|
||||||
|
<MenubarItem>Share</MenubarItem>
|
||||||
|
<MenubarSeparator />
|
||||||
|
<MenubarItem>Print</MenubarItem>
|
||||||
|
</MenubarContent>
|
||||||
|
</MenubarMenu>
|
||||||
|
</Menubar>`,
|
||||||
|
},
|
||||||
|
navigationMenu: {
|
||||||
|
componentName: 'NavigationMenu',
|
||||||
|
importDocs: `
|
||||||
|
import {
|
||||||
|
NavigationMenu,
|
||||||
|
NavigationMenuContent,
|
||||||
|
NavigationMenuItem,
|
||||||
|
NavigationMenuLink,
|
||||||
|
NavigationMenuList,
|
||||||
|
NavigationMenuTrigger,
|
||||||
|
navigationMenuTriggerStyle,
|
||||||
|
} from "/components/ui/navigation-menu"`,
|
||||||
|
usageDocs: `
|
||||||
|
<NavigationMenu>
|
||||||
|
<NavigationMenuList>
|
||||||
|
<NavigationMenuItem>
|
||||||
|
<NavigationMenuTrigger>Item One</NavigationMenuTrigger>
|
||||||
|
<NavigationMenuContent>
|
||||||
|
<NavigationMenuLink>Link</NavigationMenuLink>
|
||||||
|
</NavigationMenuContent>
|
||||||
|
</NavigationMenuItem>
|
||||||
|
</NavigationMenuList>
|
||||||
|
</NavigationMenu>`,
|
||||||
|
},
|
||||||
|
popover: {
|
||||||
|
componentName: 'Popover',
|
||||||
|
importDocs: `
|
||||||
|
import {
|
||||||
|
Popover,
|
||||||
|
PopoverContent,
|
||||||
|
PopoverTrigger,
|
||||||
|
} from "/components/ui/popover"`,
|
||||||
|
usageDocs: `
|
||||||
|
<Popover>
|
||||||
|
<PopoverTrigger>Open</PopoverTrigger>
|
||||||
|
<PopoverContent>Place content for the popover here.</PopoverContent>
|
||||||
|
</Popover>`,
|
||||||
|
},
|
||||||
|
progress: {
|
||||||
|
componentName: 'Progress',
|
||||||
|
importDocs: 'import { Progress } from "/components/ui/progress"',
|
||||||
|
usageDocs: '<Progress value={33} />',
|
||||||
|
},
|
||||||
|
separator: {
|
||||||
|
componentName: 'Separator',
|
||||||
|
importDocs: 'import { Separator } from "/components/ui/separator"',
|
||||||
|
usageDocs: '<Separator />',
|
||||||
|
},
|
||||||
|
sheet: {
|
||||||
|
componentName: 'Sheet',
|
||||||
|
importDocs: `
|
||||||
|
import {
|
||||||
|
Sheet,
|
||||||
|
SheetContent,
|
||||||
|
SheetDescription,
|
||||||
|
SheetHeader,
|
||||||
|
SheetTitle,
|
||||||
|
SheetTrigger,
|
||||||
|
} from "/components/ui/sheet"`,
|
||||||
|
usageDocs: `
|
||||||
|
<Sheet>
|
||||||
|
<SheetTrigger>Open</SheetTrigger>
|
||||||
|
<SheetContent>
|
||||||
|
<SheetHeader>
|
||||||
|
<SheetTitle>Are you sure absolutely sure?</SheetTitle>
|
||||||
|
<SheetDescription>
|
||||||
|
This action cannot be undone.
|
||||||
|
</SheetDescription>
|
||||||
|
</SheetHeader>
|
||||||
|
</SheetContent>
|
||||||
|
</Sheet>`,
|
||||||
|
},
|
||||||
|
skeleton: {
|
||||||
|
componentName: 'Skeleton',
|
||||||
|
importDocs: 'import { Skeleton } from "/components/ui/skeleton"',
|
||||||
|
usageDocs: '<Skeleton className="w-[100px] h-[20px] rounded-full" />',
|
||||||
|
},
|
||||||
|
slider: {
|
||||||
|
componentName: 'Slider',
|
||||||
|
importDocs: 'import { Slider } from "/components/ui/slider"',
|
||||||
|
usageDocs: '<Slider defaultValue={[33]} max={100} step={1} />',
|
||||||
|
},
|
||||||
|
switch: {
|
||||||
|
componentName: 'Switch',
|
||||||
|
importDocs: 'import { Switch } from "/components/ui/switch"',
|
||||||
|
usageDocs: '<Switch />',
|
||||||
|
},
|
||||||
|
table: {
|
||||||
|
componentName: 'Table',
|
||||||
|
importDocs: `
|
||||||
|
import {
|
||||||
|
Table,
|
||||||
|
TableBody,
|
||||||
|
TableCaption,
|
||||||
|
TableCell,
|
||||||
|
TableHead,
|
||||||
|
TableHeader,
|
||||||
|
TableRow,
|
||||||
|
} from "/components/ui/table"`,
|
||||||
|
usageDocs: `
|
||||||
|
<Table>
|
||||||
|
<TableCaption>A list of your recent invoices.</TableCaption>
|
||||||
|
<TableHeader>
|
||||||
|
<TableRow>
|
||||||
|
<TableHead className="w-[100px]">Invoice</TableHead>
|
||||||
|
<TableHead>Status</TableHead>
|
||||||
|
<TableHead>Method</TableHead>
|
||||||
|
<TableHead className="text-right">Amount</TableHead>
|
||||||
|
</TableRow>
|
||||||
|
</TableHeader>
|
||||||
|
<TableBody>
|
||||||
|
<TableRow>
|
||||||
|
<TableCell className="font-medium">INV001</TableCell>
|
||||||
|
<TableCell>Paid</TableCell>
|
||||||
|
<TableCell>Credit Card</TableCell>
|
||||||
|
<TableCell className="text-right">$250.00</TableCell>
|
||||||
|
</TableRow>
|
||||||
|
</TableBody>
|
||||||
|
</Table>`,
|
||||||
|
},
|
||||||
|
tabs: {
|
||||||
|
componentName: 'Tabs',
|
||||||
|
importDocs: `
|
||||||
|
import {
|
||||||
|
Tabs,
|
||||||
|
TabsContent,
|
||||||
|
TabsList,
|
||||||
|
TabsTrigger,
|
||||||
|
} from "/components/ui/tabs"`,
|
||||||
|
usageDocs: `
|
||||||
|
<Tabs defaultValue="account" className="w-[400px]">
|
||||||
|
<TabsList>
|
||||||
|
<TabsTrigger value="account">Account</TabsTrigger>
|
||||||
|
<TabsTrigger value="password">Password</TabsTrigger>
|
||||||
|
</TabsList>
|
||||||
|
<TabsContent value="account">Make changes to your account here.</TabsContent>
|
||||||
|
<TabsContent value="password">Change your password here.</TabsContent>
|
||||||
|
</Tabs>`,
|
||||||
|
},
|
||||||
|
toast: {
|
||||||
|
componentName: 'Toast',
|
||||||
|
importDocs: `
|
||||||
|
import { useToast } from "/components/ui/use-toast"
|
||||||
|
import { Button } from "/components/ui/button"`,
|
||||||
|
usageDocs: `
|
||||||
|
export function ToastDemo() {
|
||||||
|
const { toast } = useToast()
|
||||||
|
return (
|
||||||
|
<Button
|
||||||
|
onClick={() => {
|
||||||
|
toast({
|
||||||
|
title: "Scheduled: Catch up",
|
||||||
|
description: "Friday, February 10, 2023 at 5:57 PM",
|
||||||
|
})
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Show Toast
|
||||||
|
</Button>
|
||||||
|
)
|
||||||
|
}`,
|
||||||
|
},
|
||||||
|
toggle: {
|
||||||
|
componentName: 'Toggle',
|
||||||
|
importDocs: 'import { Toggle } from "/components/ui/toggle"',
|
||||||
|
usageDocs: '<Toggle>Toggle</Toggle>',
|
||||||
|
},
|
||||||
|
tooltip: {
|
||||||
|
componentName: 'Tooltip',
|
||||||
|
importDocs: `
|
||||||
|
import {
|
||||||
|
Tooltip,
|
||||||
|
TooltipContent,
|
||||||
|
TooltipProvider,
|
||||||
|
TooltipTrigger,
|
||||||
|
} from "/components/ui/tooltip"`,
|
||||||
|
usageDocs: `
|
||||||
|
<TooltipProvider>
|
||||||
|
<Tooltip>
|
||||||
|
<TooltipTrigger>Hover</TooltipTrigger>
|
||||||
|
<TooltipContent>
|
||||||
|
<p>Add to library</p>
|
||||||
|
</TooltipContent>
|
||||||
|
</Tooltip>
|
||||||
|
</TooltipProvider>`,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const components = Object.assign({}, essentialComponents, extraComponents);
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
components,
|
||||||
|
};
|
||||||
50
api/app/clients/prompts/shadcn-docs/generate.js
Normal file
50
api/app/clients/prompts/shadcn-docs/generate.js
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
const dedent = require('dedent');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate system prompt for AI-assisted React component creation
|
||||||
|
* @param {Object} options - Configuration options
|
||||||
|
* @param {Object} options.components - Documentation for shadcn components
|
||||||
|
* @param {boolean} [options.useXML=false] - Whether to use XML-style formatting for component instructions
|
||||||
|
* @returns {string} The generated system prompt
|
||||||
|
*/
|
||||||
|
function generateShadcnPrompt(options) {
|
||||||
|
const { components, useXML = false } = options;
|
||||||
|
|
||||||
|
let systemPrompt = dedent`
|
||||||
|
## Additional Artifact Instructions for React Components: "application/vnd.react"
|
||||||
|
|
||||||
|
There are some prestyled components (primitives) available for use. Please use your best judgement to use any of these components if the app calls for one.
|
||||||
|
|
||||||
|
Here are the components that are available, along with how to import them, and how to use them:
|
||||||
|
|
||||||
|
${Object.values(components)
|
||||||
|
.map((component) => {
|
||||||
|
if (useXML) {
|
||||||
|
return dedent`
|
||||||
|
<component>
|
||||||
|
<name>${component.componentName}</name>
|
||||||
|
<import-instructions>${component.importDocs}</import-instructions>
|
||||||
|
<usage-instructions>${component.usageDocs}</usage-instructions>
|
||||||
|
</component>
|
||||||
|
`;
|
||||||
|
} else {
|
||||||
|
return dedent`
|
||||||
|
# ${component.componentName}
|
||||||
|
|
||||||
|
## Import Instructions
|
||||||
|
${component.importDocs}
|
||||||
|
|
||||||
|
## Usage Instructions
|
||||||
|
${component.usageDocs}
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.join('\n\n')}
|
||||||
|
`;
|
||||||
|
|
||||||
|
return systemPrompt;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
generateShadcnPrompt,
|
||||||
|
};
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
const { PromptTemplate } = require('langchain/prompts');
|
const { PromptTemplate } = require('@langchain/core/prompts');
|
||||||
/*
|
/*
|
||||||
* Without `{summary}` and `{new_lines}`, token count is 98
|
* Without `{summary}` and `{new_lines}`, token count is 98
|
||||||
* We are counting this towards the max context tokens for summaries, +3 for the assistant label (101)
|
* We are counting this towards the max context tokens for summaries, +3 for the assistant label (101)
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ const {
|
||||||
ChatPromptTemplate,
|
ChatPromptTemplate,
|
||||||
SystemMessagePromptTemplate,
|
SystemMessagePromptTemplate,
|
||||||
HumanMessagePromptTemplate,
|
HumanMessagePromptTemplate,
|
||||||
} = require('langchain/prompts');
|
} = require('@langchain/core/prompts');
|
||||||
|
|
||||||
const langPrompt = new ChatPromptTemplate({
|
const langPrompt = new ChatPromptTemplate({
|
||||||
promptMessages: [
|
promptMessages: [
|
||||||
|
|
@ -27,7 +27,110 @@ ${convo}`,
|
||||||
return titlePrompt;
|
return titlePrompt;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const titleInstruction =
|
||||||
|
'a concise, 5-word-or-less title for the conversation, using its same language, with no punctuation. Apply title case conventions appropriate for the language. Never directly mention the language name or the word "title"';
|
||||||
|
const titleFunctionPrompt = `In this environment you have access to a set of tools you can use to generate the conversation title.
|
||||||
|
|
||||||
|
You may call them like this:
|
||||||
|
<function_calls>
|
||||||
|
<invoke>
|
||||||
|
<tool_name>$TOOL_NAME</tool_name>
|
||||||
|
<parameters>
|
||||||
|
<$PARAMETER_NAME>$PARAMETER_VALUE</$PARAMETER_NAME>
|
||||||
|
...
|
||||||
|
</parameters>
|
||||||
|
</invoke>
|
||||||
|
</function_calls>
|
||||||
|
|
||||||
|
Here are the tools available:
|
||||||
|
<tools>
|
||||||
|
<tool_description>
|
||||||
|
<tool_name>submit_title</tool_name>
|
||||||
|
<description>
|
||||||
|
Submit a brief title in the conversation's language, following the parameter description closely.
|
||||||
|
</description>
|
||||||
|
<parameters>
|
||||||
|
<parameter>
|
||||||
|
<name>title</name>
|
||||||
|
<type>string</type>
|
||||||
|
<description>${titleInstruction}</description>
|
||||||
|
</parameter>
|
||||||
|
</parameters>
|
||||||
|
</tool_description>
|
||||||
|
</tools>`;
|
||||||
|
|
||||||
|
const genTranslationPrompt = (
|
||||||
|
translationPrompt,
|
||||||
|
) => `In this environment you have access to a set of tools you can use to translate text.
|
||||||
|
|
||||||
|
You may call them like this:
|
||||||
|
<function_calls>
|
||||||
|
<invoke>
|
||||||
|
<tool_name>$TOOL_NAME</tool_name>
|
||||||
|
<parameters>
|
||||||
|
<$PARAMETER_NAME>$PARAMETER_VALUE</$PARAMETER_NAME>
|
||||||
|
...
|
||||||
|
</parameters>
|
||||||
|
</invoke>
|
||||||
|
</function_calls>
|
||||||
|
|
||||||
|
Here are the tools available:
|
||||||
|
<tools>
|
||||||
|
<tool_description>
|
||||||
|
<tool_name>submit_translation</tool_name>
|
||||||
|
<description>
|
||||||
|
Submit a translation in the target language, following the parameter description and its language closely.
|
||||||
|
</description>
|
||||||
|
<parameters>
|
||||||
|
<parameter>
|
||||||
|
<name>translation</name>
|
||||||
|
<type>string</type>
|
||||||
|
<description>${translationPrompt}
|
||||||
|
ONLY include the generated translation without quotations, nor its related key</description>
|
||||||
|
</parameter>
|
||||||
|
</parameters>
|
||||||
|
</tool_description>
|
||||||
|
</tools>`;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses specified parameter from the provided prompt.
|
||||||
|
* @param {string} prompt - The prompt containing the desired parameter.
|
||||||
|
* @param {string} paramName - The name of the parameter to extract.
|
||||||
|
* @returns {string} The parsed parameter's value or a default value if not found.
|
||||||
|
*/
|
||||||
|
function parseParamFromPrompt(prompt, paramName) {
|
||||||
|
// Handle null/undefined prompt
|
||||||
|
if (!prompt) {
|
||||||
|
return `No ${paramName} provided`;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try original format first: <title>value</title>
|
||||||
|
const simpleRegex = new RegExp(`<${paramName}>(.*?)</${paramName}>`, 's');
|
||||||
|
const simpleMatch = prompt.match(simpleRegex);
|
||||||
|
|
||||||
|
if (simpleMatch) {
|
||||||
|
return simpleMatch[1].trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try parameter format: <parameter name="title">value</parameter>
|
||||||
|
const paramRegex = new RegExp(`<parameter name="${paramName}">(.*?)</parameter>`, 's');
|
||||||
|
const paramMatch = prompt.match(paramRegex);
|
||||||
|
|
||||||
|
if (paramMatch) {
|
||||||
|
return paramMatch[1].trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (prompt && prompt.length) {
|
||||||
|
return `NO TOOL INVOCATION: ${prompt}`;
|
||||||
|
}
|
||||||
|
return `No ${paramName} provided`;
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
langPrompt,
|
langPrompt,
|
||||||
|
titleInstruction,
|
||||||
createTitlePrompt,
|
createTitlePrompt,
|
||||||
|
titleFunctionPrompt,
|
||||||
|
parseParamFromPrompt,
|
||||||
|
genTranslationPrompt,
|
||||||
};
|
};
|
||||||
|
|
|
||||||
73
api/app/clients/prompts/titlePrompts.spec.js
Normal file
73
api/app/clients/prompts/titlePrompts.spec.js
Normal file
|
|
@ -0,0 +1,73 @@
|
||||||
|
const { parseParamFromPrompt } = require('./titlePrompts');
|
||||||
|
describe('parseParamFromPrompt', () => {
|
||||||
|
// Original simple format tests
|
||||||
|
test('extracts parameter from simple format', () => {
|
||||||
|
const prompt = '<title>Simple Title</title>';
|
||||||
|
expect(parseParamFromPrompt(prompt, 'title')).toBe('Simple Title');
|
||||||
|
});
|
||||||
|
|
||||||
|
// Parameter format tests
|
||||||
|
test('extracts parameter from parameter format', () => {
|
||||||
|
const prompt =
|
||||||
|
'<function_calls> <invoke name="submit_title"> <parameter name="title">Complex Title</parameter> </invoke>';
|
||||||
|
expect(parseParamFromPrompt(prompt, 'title')).toBe('Complex Title');
|
||||||
|
});
|
||||||
|
|
||||||
|
// Edge cases and error handling
|
||||||
|
test('returns NO TOOL INVOCATION message for non-matching content', () => {
|
||||||
|
const prompt = 'Some random text without parameters';
|
||||||
|
expect(parseParamFromPrompt(prompt, 'title')).toBe(
|
||||||
|
'NO TOOL INVOCATION: Some random text without parameters',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('returns default message for empty prompt', () => {
|
||||||
|
expect(parseParamFromPrompt('', 'title')).toBe('No title provided');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('returns default message for null prompt', () => {
|
||||||
|
expect(parseParamFromPrompt(null, 'title')).toBe('No title provided');
|
||||||
|
});
|
||||||
|
|
||||||
|
// Multiple parameter tests
|
||||||
|
test('works with different parameter names', () => {
|
||||||
|
const prompt = '<name>John Doe</name>';
|
||||||
|
expect(parseParamFromPrompt(prompt, 'name')).toBe('John Doe');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('handles multiline content', () => {
|
||||||
|
const prompt = `<parameter name="description">This is a
|
||||||
|
multiline
|
||||||
|
description</parameter>`;
|
||||||
|
expect(parseParamFromPrompt(prompt, 'description')).toBe(
|
||||||
|
'This is a\n multiline\n description',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Whitespace handling
|
||||||
|
test('trims whitespace from extracted content', () => {
|
||||||
|
const prompt = '<title> Padded Title </title>';
|
||||||
|
expect(parseParamFromPrompt(prompt, 'title')).toBe('Padded Title');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('handles whitespace in parameter format', () => {
|
||||||
|
const prompt = '<parameter name="title"> Padded Parameter Title </parameter>';
|
||||||
|
expect(parseParamFromPrompt(prompt, 'title')).toBe('Padded Parameter Title');
|
||||||
|
});
|
||||||
|
|
||||||
|
// Invalid format tests
|
||||||
|
test('handles malformed tags', () => {
|
||||||
|
const prompt = '<title>Incomplete Tag';
|
||||||
|
expect(parseParamFromPrompt(prompt, 'title')).toBe('NO TOOL INVOCATION: <title>Incomplete Tag');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('handles empty tags', () => {
|
||||||
|
const prompt = '<title></title>';
|
||||||
|
expect(parseParamFromPrompt(prompt, 'title')).toBe('');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('handles empty parameter tags', () => {
|
||||||
|
const prompt = '<parameter name="title"></parameter>';
|
||||||
|
expect(parseParamFromPrompt(prompt, 'title')).toBe('');
|
||||||
|
});
|
||||||
|
});
|
||||||
115
api/app/clients/prompts/truncate.js
Normal file
115
api/app/clients/prompts/truncate.js
Normal file
|
|
@ -0,0 +1,115 @@
|
||||||
|
const MAX_CHAR = 255;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Truncates a given text to a specified maximum length, appending ellipsis and a notification
|
||||||
|
* if the original text exceeds the maximum length.
|
||||||
|
*
|
||||||
|
* @param {string} text - The text to be truncated.
|
||||||
|
* @param {number} [maxLength=MAX_CHAR] - The maximum length of the text after truncation. Defaults to MAX_CHAR.
|
||||||
|
* @returns {string} The truncated text if the original text length exceeds maxLength, otherwise returns the original text.
|
||||||
|
*/
|
||||||
|
function truncateText(text, maxLength = MAX_CHAR) {
|
||||||
|
if (text.length > maxLength) {
|
||||||
|
return `${text.slice(0, maxLength)}... [text truncated for brevity]`;
|
||||||
|
}
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Truncates a given text to a specified maximum length by showing the first half and the last half of the text,
|
||||||
|
* separated by ellipsis. This method ensures the output does not exceed the maximum length, including the addition
|
||||||
|
* of ellipsis and notification if the original text exceeds the maximum length.
|
||||||
|
*
|
||||||
|
* @param {string} text - The text to be truncated.
|
||||||
|
* @param {number} [maxLength=MAX_CHAR] - The maximum length of the output text after truncation. Defaults to MAX_CHAR.
|
||||||
|
* @returns {string} The truncated text showing the first half and the last half, or the original text if it does not exceed maxLength.
|
||||||
|
*/
|
||||||
|
function smartTruncateText(text, maxLength = MAX_CHAR) {
|
||||||
|
const ellipsis = '...';
|
||||||
|
const notification = ' [text truncated for brevity]';
|
||||||
|
const halfMaxLength = Math.floor((maxLength - ellipsis.length - notification.length) / 2);
|
||||||
|
|
||||||
|
if (text.length > maxLength) {
|
||||||
|
const startLastHalf = text.length - halfMaxLength;
|
||||||
|
return `${text.slice(0, halfMaxLength)}${ellipsis}${text.slice(startLastHalf)}${notification}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {TMessage[]} _messages
|
||||||
|
* @param {number} maxContextTokens
|
||||||
|
* @param {function({role: string, content: TMessageContent[]}): number} getTokenCountForMessage
|
||||||
|
*
|
||||||
|
* @returns {{
|
||||||
|
* dbMessages: TMessage[],
|
||||||
|
* editedIndices: number[]
|
||||||
|
* }}
|
||||||
|
*/
|
||||||
|
function truncateToolCallOutputs(_messages, maxContextTokens, getTokenCountForMessage) {
|
||||||
|
const THRESHOLD_PERCENTAGE = 0.5;
|
||||||
|
const targetTokenLimit = maxContextTokens * THRESHOLD_PERCENTAGE;
|
||||||
|
|
||||||
|
let currentTokenCount = 3;
|
||||||
|
const messages = [..._messages];
|
||||||
|
const processedMessages = [];
|
||||||
|
let currentIndex = messages.length;
|
||||||
|
const editedIndices = new Set();
|
||||||
|
while (messages.length > 0) {
|
||||||
|
currentIndex--;
|
||||||
|
const message = messages.pop();
|
||||||
|
currentTokenCount += message.tokenCount;
|
||||||
|
if (currentTokenCount < targetTokenLimit) {
|
||||||
|
processedMessages.push(message);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!message.content || !Array.isArray(message.content)) {
|
||||||
|
processedMessages.push(message);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const toolCallIndices = message.content
|
||||||
|
.map((item, index) => (item.type === 'tool_call' ? index : -1))
|
||||||
|
.filter((index) => index !== -1)
|
||||||
|
.reverse();
|
||||||
|
|
||||||
|
if (toolCallIndices.length === 0) {
|
||||||
|
processedMessages.push(message);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const newContent = [...message.content];
|
||||||
|
|
||||||
|
// Truncate all tool outputs since we're over threshold
|
||||||
|
for (const index of toolCallIndices) {
|
||||||
|
const toolCall = newContent[index].tool_call;
|
||||||
|
if (!toolCall || !toolCall.output) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
editedIndices.add(currentIndex);
|
||||||
|
|
||||||
|
newContent[index] = {
|
||||||
|
...newContent[index],
|
||||||
|
tool_call: {
|
||||||
|
...toolCall,
|
||||||
|
output: '[OUTPUT_OMITTED_FOR_BREVITY]',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const truncatedMessage = {
|
||||||
|
...message,
|
||||||
|
content: newContent,
|
||||||
|
tokenCount: getTokenCountForMessage({ role: 'assistant', content: newContent }),
|
||||||
|
};
|
||||||
|
|
||||||
|
processedMessages.push(truncatedMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
return { dbMessages: processedMessages.reverse(), editedIndices: Array.from(editedIndices) };
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { truncateText, smartTruncateText, truncateToolCallOutputs };
|
||||||
|
|
@ -1,10 +0,0 @@
|
||||||
const MAX_CHAR = 255;
|
|
||||||
|
|
||||||
function truncateText(text) {
|
|
||||||
if (text.length > MAX_CHAR) {
|
|
||||||
return `${text.slice(0, MAX_CHAR)}... [text truncated for brevity]`;
|
|
||||||
}
|
|
||||||
return text;
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = truncateText;
|
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
const AnthropicClient = require('../AnthropicClient');
|
const { anthropicSettings } = require('librechat-data-provider');
|
||||||
|
const AnthropicClient = require('~/app/clients/AnthropicClient');
|
||||||
|
|
||||||
const HUMAN_PROMPT = '\n\nHuman:';
|
const HUMAN_PROMPT = '\n\nHuman:';
|
||||||
const AI_PROMPT = '\n\nAssistant:';
|
const AI_PROMPT = '\n\nAssistant:';
|
||||||
|
|
||||||
|
|
@ -22,7 +24,7 @@ describe('AnthropicClient', () => {
|
||||||
const options = {
|
const options = {
|
||||||
modelOptions: {
|
modelOptions: {
|
||||||
model,
|
model,
|
||||||
temperature: 0.7,
|
temperature: anthropicSettings.temperature.default,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
client = new AnthropicClient('test-api-key');
|
client = new AnthropicClient('test-api-key');
|
||||||
|
|
@ -33,7 +35,42 @@ describe('AnthropicClient', () => {
|
||||||
it('should set the options correctly', () => {
|
it('should set the options correctly', () => {
|
||||||
expect(client.apiKey).toBe('test-api-key');
|
expect(client.apiKey).toBe('test-api-key');
|
||||||
expect(client.modelOptions.model).toBe(model);
|
expect(client.modelOptions.model).toBe(model);
|
||||||
expect(client.modelOptions.temperature).toBe(0.7);
|
expect(client.modelOptions.temperature).toBe(anthropicSettings.temperature.default);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should set legacy maxOutputTokens for non-Claude-3 models', () => {
|
||||||
|
const client = new AnthropicClient('test-api-key');
|
||||||
|
client.setOptions({
|
||||||
|
modelOptions: {
|
||||||
|
model: 'claude-2',
|
||||||
|
maxOutputTokens: anthropicSettings.maxOutputTokens.default,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
expect(client.modelOptions.maxOutputTokens).toBe(
|
||||||
|
anthropicSettings.legacy.maxOutputTokens.default,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it('should not set maxOutputTokens if not provided', () => {
|
||||||
|
const client = new AnthropicClient('test-api-key');
|
||||||
|
client.setOptions({
|
||||||
|
modelOptions: {
|
||||||
|
model: 'claude-3',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
expect(client.modelOptions.maxOutputTokens).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not set legacy maxOutputTokens for Claude-3 models', () => {
|
||||||
|
const client = new AnthropicClient('test-api-key');
|
||||||
|
client.setOptions({
|
||||||
|
modelOptions: {
|
||||||
|
model: 'claude-3-opus-20240229',
|
||||||
|
maxOutputTokens: anthropicSettings.legacy.maxOutputTokens.default,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
expect(client.modelOptions.maxOutputTokens).toBe(
|
||||||
|
anthropicSettings.legacy.maxOutputTokens.default,
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -136,4 +173,236 @@ describe('AnthropicClient', () => {
|
||||||
expect(prompt).toContain('You are Claude-2');
|
expect(prompt).toContain('You are Claude-2');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('getClient', () => {
|
||||||
|
it('should set legacy maxOutputTokens for non-Claude-3 models', () => {
|
||||||
|
const client = new AnthropicClient('test-api-key');
|
||||||
|
client.setOptions({
|
||||||
|
modelOptions: {
|
||||||
|
model: 'claude-2',
|
||||||
|
maxOutputTokens: anthropicSettings.legacy.maxOutputTokens.default,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
expect(client.modelOptions.maxOutputTokens).toBe(
|
||||||
|
anthropicSettings.legacy.maxOutputTokens.default,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not set legacy maxOutputTokens for Claude-3 models', () => {
|
||||||
|
const client = new AnthropicClient('test-api-key');
|
||||||
|
client.setOptions({
|
||||||
|
modelOptions: {
|
||||||
|
model: 'claude-3-opus-20240229',
|
||||||
|
maxOutputTokens: anthropicSettings.legacy.maxOutputTokens.default,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
expect(client.modelOptions.maxOutputTokens).toBe(
|
||||||
|
anthropicSettings.legacy.maxOutputTokens.default,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should add "max-tokens" & "prompt-caching" beta header for claude-3-5-sonnet model', () => {
|
||||||
|
const client = new AnthropicClient('test-api-key');
|
||||||
|
const modelOptions = {
|
||||||
|
model: 'claude-3-5-sonnet-20241022',
|
||||||
|
};
|
||||||
|
client.setOptions({ modelOptions, promptCache: true });
|
||||||
|
const anthropicClient = client.getClient(modelOptions);
|
||||||
|
expect(anthropicClient._options.defaultHeaders).toBeDefined();
|
||||||
|
expect(anthropicClient._options.defaultHeaders).toHaveProperty('anthropic-beta');
|
||||||
|
expect(anthropicClient._options.defaultHeaders['anthropic-beta']).toBe(
|
||||||
|
'max-tokens-3-5-sonnet-2024-07-15,prompt-caching-2024-07-31',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should add "prompt-caching" beta header for claude-3-haiku model', () => {
|
||||||
|
const client = new AnthropicClient('test-api-key');
|
||||||
|
const modelOptions = {
|
||||||
|
model: 'claude-3-haiku-2028',
|
||||||
|
};
|
||||||
|
client.setOptions({ modelOptions, promptCache: true });
|
||||||
|
const anthropicClient = client.getClient(modelOptions);
|
||||||
|
expect(anthropicClient._options.defaultHeaders).toBeDefined();
|
||||||
|
expect(anthropicClient._options.defaultHeaders).toHaveProperty('anthropic-beta');
|
||||||
|
expect(anthropicClient._options.defaultHeaders['anthropic-beta']).toBe(
|
||||||
|
'prompt-caching-2024-07-31',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should add "prompt-caching" beta header for claude-3-opus model', () => {
|
||||||
|
const client = new AnthropicClient('test-api-key');
|
||||||
|
const modelOptions = {
|
||||||
|
model: 'claude-3-opus-2028',
|
||||||
|
};
|
||||||
|
client.setOptions({ modelOptions, promptCache: true });
|
||||||
|
const anthropicClient = client.getClient(modelOptions);
|
||||||
|
expect(anthropicClient._options.defaultHeaders).toBeDefined();
|
||||||
|
expect(anthropicClient._options.defaultHeaders).toHaveProperty('anthropic-beta');
|
||||||
|
expect(anthropicClient._options.defaultHeaders['anthropic-beta']).toBe(
|
||||||
|
'prompt-caching-2024-07-31',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not add beta header for claude-3-5-sonnet-latest model', () => {
|
||||||
|
const client = new AnthropicClient('test-api-key');
|
||||||
|
const modelOptions = {
|
||||||
|
model: 'anthropic/claude-3-5-sonnet-latest',
|
||||||
|
};
|
||||||
|
client.setOptions({ modelOptions, promptCache: true });
|
||||||
|
const anthropicClient = client.getClient(modelOptions);
|
||||||
|
expect(anthropicClient.defaultHeaders).not.toHaveProperty('anthropic-beta');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not add beta header for other models', () => {
|
||||||
|
const client = new AnthropicClient('test-api-key');
|
||||||
|
client.setOptions({
|
||||||
|
modelOptions: {
|
||||||
|
model: 'claude-2',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const anthropicClient = client.getClient();
|
||||||
|
expect(anthropicClient.defaultHeaders).not.toHaveProperty('anthropic-beta');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('calculateCurrentTokenCount', () => {
|
||||||
|
let client;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
client = new AnthropicClient('test-api-key');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should calculate correct token count when usage is provided', () => {
|
||||||
|
const tokenCountMap = {
|
||||||
|
msg1: 10,
|
||||||
|
msg2: 20,
|
||||||
|
currentMsg: 30,
|
||||||
|
};
|
||||||
|
const currentMessageId = 'currentMsg';
|
||||||
|
const usage = {
|
||||||
|
input_tokens: 70,
|
||||||
|
output_tokens: 50,
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = client.calculateCurrentTokenCount({ tokenCountMap, currentMessageId, usage });
|
||||||
|
|
||||||
|
expect(result).toBe(40); // 70 - (10 + 20) = 40
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return original estimate if calculation results in negative value', () => {
|
||||||
|
const tokenCountMap = {
|
||||||
|
msg1: 40,
|
||||||
|
msg2: 50,
|
||||||
|
currentMsg: 30,
|
||||||
|
};
|
||||||
|
const currentMessageId = 'currentMsg';
|
||||||
|
const usage = {
|
||||||
|
input_tokens: 80,
|
||||||
|
output_tokens: 50,
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = client.calculateCurrentTokenCount({ tokenCountMap, currentMessageId, usage });
|
||||||
|
|
||||||
|
expect(result).toBe(30); // Original estimate
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle cache creation and read input tokens', () => {
|
||||||
|
const tokenCountMap = {
|
||||||
|
msg1: 10,
|
||||||
|
msg2: 20,
|
||||||
|
currentMsg: 30,
|
||||||
|
};
|
||||||
|
const currentMessageId = 'currentMsg';
|
||||||
|
const usage = {
|
||||||
|
input_tokens: 50,
|
||||||
|
cache_creation_input_tokens: 10,
|
||||||
|
cache_read_input_tokens: 20,
|
||||||
|
output_tokens: 40,
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = client.calculateCurrentTokenCount({ tokenCountMap, currentMessageId, usage });
|
||||||
|
|
||||||
|
expect(result).toBe(50); // (50 + 10 + 20) - (10 + 20) = 50
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle missing usage properties', () => {
|
||||||
|
const tokenCountMap = {
|
||||||
|
msg1: 10,
|
||||||
|
msg2: 20,
|
||||||
|
currentMsg: 30,
|
||||||
|
};
|
||||||
|
const currentMessageId = 'currentMsg';
|
||||||
|
const usage = {
|
||||||
|
output_tokens: 40,
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = client.calculateCurrentTokenCount({ tokenCountMap, currentMessageId, usage });
|
||||||
|
|
||||||
|
expect(result).toBe(30); // Original estimate
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle empty tokenCountMap', () => {
|
||||||
|
const tokenCountMap = {};
|
||||||
|
const currentMessageId = 'currentMsg';
|
||||||
|
const usage = {
|
||||||
|
input_tokens: 50,
|
||||||
|
output_tokens: 40,
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = client.calculateCurrentTokenCount({ tokenCountMap, currentMessageId, usage });
|
||||||
|
|
||||||
|
expect(result).toBe(50);
|
||||||
|
expect(Number.isNaN(result)).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle zero values in usage', () => {
|
||||||
|
const tokenCountMap = {
|
||||||
|
msg1: 10,
|
||||||
|
currentMsg: 20,
|
||||||
|
};
|
||||||
|
const currentMessageId = 'currentMsg';
|
||||||
|
const usage = {
|
||||||
|
input_tokens: 0,
|
||||||
|
cache_creation_input_tokens: 0,
|
||||||
|
cache_read_input_tokens: 0,
|
||||||
|
output_tokens: 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = client.calculateCurrentTokenCount({ tokenCountMap, currentMessageId, usage });
|
||||||
|
|
||||||
|
expect(result).toBe(20); // Should return original estimate
|
||||||
|
expect(Number.isNaN(result)).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle undefined usage', () => {
|
||||||
|
const tokenCountMap = {
|
||||||
|
msg1: 10,
|
||||||
|
currentMsg: 20,
|
||||||
|
};
|
||||||
|
const currentMessageId = 'currentMsg';
|
||||||
|
const usage = undefined;
|
||||||
|
|
||||||
|
const result = client.calculateCurrentTokenCount({ tokenCountMap, currentMessageId, usage });
|
||||||
|
|
||||||
|
expect(result).toBe(20); // Should return original estimate
|
||||||
|
expect(Number.isNaN(result)).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle non-numeric values in tokenCountMap', () => {
|
||||||
|
const tokenCountMap = {
|
||||||
|
msg1: 'ten',
|
||||||
|
currentMsg: 20,
|
||||||
|
};
|
||||||
|
const currentMessageId = 'currentMsg';
|
||||||
|
const usage = {
|
||||||
|
input_tokens: 30,
|
||||||
|
output_tokens: 10,
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = client.calculateCurrentTokenCount({ tokenCountMap, currentMessageId, usage });
|
||||||
|
|
||||||
|
expect(result).toBe(30); // Should return 30 (input_tokens) - 0 (ignored 'ten') = 30
|
||||||
|
expect(Number.isNaN(result)).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
const { Constants } = require('librechat-data-provider');
|
const { Constants } = require('librechat-data-provider');
|
||||||
const { initializeFakeClient } = require('./FakeClient');
|
const { initializeFakeClient } = require('./FakeClient');
|
||||||
|
|
||||||
jest.mock('../../../lib/db/connectDb');
|
jest.mock('~/lib/db/connectDb');
|
||||||
jest.mock('~/models', () => ({
|
jest.mock('~/models', () => ({
|
||||||
User: jest.fn(),
|
User: jest.fn(),
|
||||||
Key: jest.fn(),
|
Key: jest.fn(),
|
||||||
|
|
@ -30,7 +30,7 @@ jest.mock('~/models', () => ({
|
||||||
updateFileUsage: jest.fn(),
|
updateFileUsage: jest.fn(),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
jest.mock('langchain/chat_models/openai', () => {
|
jest.mock('@langchain/openai', () => {
|
||||||
return {
|
return {
|
||||||
ChatOpenAI: jest.fn().mockImplementation(() => {
|
ChatOpenAI: jest.fn().mockImplementation(() => {
|
||||||
return {};
|
return {};
|
||||||
|
|
@ -61,7 +61,7 @@ describe('BaseClient', () => {
|
||||||
const options = {
|
const options = {
|
||||||
// debug: true,
|
// debug: true,
|
||||||
modelOptions: {
|
modelOptions: {
|
||||||
model: 'gpt-3.5-turbo',
|
model: 'gpt-4o-mini',
|
||||||
temperature: 0,
|
temperature: 0,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
@ -88,6 +88,19 @@ describe('BaseClient', () => {
|
||||||
const messages = [{ content: 'Hello' }, { content: 'How are you?' }, { content: 'Goodbye' }];
|
const messages = [{ content: 'Hello' }, { content: 'How are you?' }, { content: 'Goodbye' }];
|
||||||
const instructions = { content: 'Please respond to the question.' };
|
const instructions = { content: 'Please respond to the question.' };
|
||||||
const result = TestClient.addInstructions(messages, instructions);
|
const result = TestClient.addInstructions(messages, instructions);
|
||||||
|
const expected = [
|
||||||
|
{ content: 'Please respond to the question.' },
|
||||||
|
{ content: 'Hello' },
|
||||||
|
{ content: 'How are you?' },
|
||||||
|
{ content: 'Goodbye' },
|
||||||
|
];
|
||||||
|
expect(result).toEqual(expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('returns the input messages with instructions properly added when addInstructions() with legacy flag', () => {
|
||||||
|
const messages = [{ content: 'Hello' }, { content: 'How are you?' }, { content: 'Goodbye' }];
|
||||||
|
const instructions = { content: 'Please respond to the question.' };
|
||||||
|
const result = TestClient.addInstructions(messages, instructions, true);
|
||||||
const expected = [
|
const expected = [
|
||||||
{ content: 'Hello' },
|
{ content: 'Hello' },
|
||||||
{ content: 'How are you?' },
|
{ content: 'How are you?' },
|
||||||
|
|
@ -146,7 +159,7 @@ describe('BaseClient', () => {
|
||||||
expectedMessagesToRefine?.[expectedMessagesToRefine.length - 1] ?? {};
|
expectedMessagesToRefine?.[expectedMessagesToRefine.length - 1] ?? {};
|
||||||
const expectedIndex = messages.findIndex((msg) => msg.content === lastExpectedMessage?.content);
|
const expectedIndex = messages.findIndex((msg) => msg.content === lastExpectedMessage?.content);
|
||||||
|
|
||||||
const result = await TestClient.getMessagesWithinTokenLimit(messages);
|
const result = await TestClient.getMessagesWithinTokenLimit({ messages });
|
||||||
|
|
||||||
expect(result.context).toEqual(expectedContext);
|
expect(result.context).toEqual(expectedContext);
|
||||||
expect(result.summaryIndex).toEqual(expectedIndex);
|
expect(result.summaryIndex).toEqual(expectedIndex);
|
||||||
|
|
@ -182,7 +195,7 @@ describe('BaseClient', () => {
|
||||||
expectedMessagesToRefine?.[expectedMessagesToRefine.length - 1] ?? {};
|
expectedMessagesToRefine?.[expectedMessagesToRefine.length - 1] ?? {};
|
||||||
const expectedIndex = messages.findIndex((msg) => msg.content === lastExpectedMessage?.content);
|
const expectedIndex = messages.findIndex((msg) => msg.content === lastExpectedMessage?.content);
|
||||||
|
|
||||||
const result = await TestClient.getMessagesWithinTokenLimit(messages);
|
const result = await TestClient.getMessagesWithinTokenLimit({ messages });
|
||||||
|
|
||||||
expect(result.context).toEqual(expectedContext);
|
expect(result.context).toEqual(expectedContext);
|
||||||
expect(result.summaryIndex).toEqual(expectedIndex);
|
expect(result.summaryIndex).toEqual(expectedIndex);
|
||||||
|
|
@ -190,66 +203,6 @@ describe('BaseClient', () => {
|
||||||
expect(result.messagesToRefine).toEqual(expectedMessagesToRefine);
|
expect(result.messagesToRefine).toEqual(expectedMessagesToRefine);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('handles context strategy correctly in handleContextStrategy()', async () => {
|
|
||||||
TestClient.addInstructions = jest
|
|
||||||
.fn()
|
|
||||||
.mockReturnValue([
|
|
||||||
{ content: 'Hello' },
|
|
||||||
{ content: 'How can I help you?' },
|
|
||||||
{ content: 'Please provide more details.' },
|
|
||||||
{ content: 'I can assist you with that.' },
|
|
||||||
]);
|
|
||||||
TestClient.getMessagesWithinTokenLimit = jest.fn().mockReturnValue({
|
|
||||||
context: [
|
|
||||||
{ content: 'How can I help you?' },
|
|
||||||
{ content: 'Please provide more details.' },
|
|
||||||
{ content: 'I can assist you with that.' },
|
|
||||||
],
|
|
||||||
remainingContextTokens: 80,
|
|
||||||
messagesToRefine: [{ content: 'Hello' }],
|
|
||||||
summaryIndex: 3,
|
|
||||||
});
|
|
||||||
|
|
||||||
TestClient.getTokenCount = jest.fn().mockReturnValue(40);
|
|
||||||
|
|
||||||
const instructions = { content: 'Please provide more details.' };
|
|
||||||
const orderedMessages = [
|
|
||||||
{ content: 'Hello' },
|
|
||||||
{ content: 'How can I help you?' },
|
|
||||||
{ content: 'Please provide more details.' },
|
|
||||||
{ content: 'I can assist you with that.' },
|
|
||||||
];
|
|
||||||
const formattedMessages = [
|
|
||||||
{ content: 'Hello' },
|
|
||||||
{ content: 'How can I help you?' },
|
|
||||||
{ content: 'Please provide more details.' },
|
|
||||||
{ content: 'I can assist you with that.' },
|
|
||||||
];
|
|
||||||
const expectedResult = {
|
|
||||||
payload: [
|
|
||||||
{
|
|
||||||
role: 'system',
|
|
||||||
content: 'Refined answer',
|
|
||||||
},
|
|
||||||
{ content: 'How can I help you?' },
|
|
||||||
{ content: 'Please provide more details.' },
|
|
||||||
{ content: 'I can assist you with that.' },
|
|
||||||
],
|
|
||||||
promptTokens: expect.any(Number),
|
|
||||||
tokenCountMap: {},
|
|
||||||
messages: expect.any(Array),
|
|
||||||
};
|
|
||||||
|
|
||||||
TestClient.shouldSummarize = true;
|
|
||||||
const result = await TestClient.handleContextStrategy({
|
|
||||||
instructions,
|
|
||||||
orderedMessages,
|
|
||||||
formattedMessages,
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(result).toEqual(expectedResult);
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('getMessagesForConversation', () => {
|
describe('getMessagesForConversation', () => {
|
||||||
it('should return an empty array if the parentMessageId does not exist', () => {
|
it('should return an empty array if the parentMessageId does not exist', () => {
|
||||||
const result = TestClient.constructor.getMessagesForConversation({
|
const result = TestClient.constructor.getMessagesForConversation({
|
||||||
|
|
@ -565,18 +518,24 @@ describe('BaseClient', () => {
|
||||||
const getReqData = jest.fn();
|
const getReqData = jest.fn();
|
||||||
const opts = { getReqData };
|
const opts = { getReqData };
|
||||||
const response = await TestClient.sendMessage('Hello, world!', opts);
|
const response = await TestClient.sendMessage('Hello, world!', opts);
|
||||||
expect(getReqData).toHaveBeenCalledWith({
|
expect(getReqData).toHaveBeenCalledWith(
|
||||||
userMessage: expect.objectContaining({ text: 'Hello, world!' }),
|
expect.objectContaining({
|
||||||
conversationId: response.conversationId,
|
userMessage: expect.objectContaining({ text: 'Hello, world!' }),
|
||||||
responseMessageId: response.messageId,
|
conversationId: response.conversationId,
|
||||||
});
|
responseMessageId: response.messageId,
|
||||||
|
}),
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('onStart is called with the correct arguments', async () => {
|
test('onStart is called with the correct arguments', async () => {
|
||||||
const onStart = jest.fn();
|
const onStart = jest.fn();
|
||||||
const opts = { onStart };
|
const opts = { onStart };
|
||||||
await TestClient.sendMessage('Hello, world!', opts);
|
await TestClient.sendMessage('Hello, world!', opts);
|
||||||
expect(onStart).toHaveBeenCalledWith(expect.objectContaining({ text: 'Hello, world!' }));
|
|
||||||
|
expect(onStart).toHaveBeenCalledWith(
|
||||||
|
expect.objectContaining({ text: 'Hello, world!' }),
|
||||||
|
expect.any(String),
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('saveMessageToDatabase is called with the correct arguments', async () => {
|
test('saveMessageToDatabase is called with the correct arguments', async () => {
|
||||||
|
|
@ -609,9 +568,9 @@ describe('BaseClient', () => {
|
||||||
test('getTokenCount for response is called with the correct arguments', async () => {
|
test('getTokenCount for response is called with the correct arguments', async () => {
|
||||||
const tokenCountMap = {}; // Mock tokenCountMap
|
const tokenCountMap = {}; // Mock tokenCountMap
|
||||||
TestClient.buildMessages.mockReturnValue({ prompt: [], tokenCountMap });
|
TestClient.buildMessages.mockReturnValue({ prompt: [], tokenCountMap });
|
||||||
TestClient.getTokenCount = jest.fn();
|
TestClient.getTokenCountForResponse = jest.fn();
|
||||||
const response = await TestClient.sendMessage('Hello, world!', {});
|
const response = await TestClient.sendMessage('Hello, world!', {});
|
||||||
expect(TestClient.getTokenCount).toHaveBeenCalledWith(response.text);
|
expect(TestClient.getTokenCountForResponse).toHaveBeenCalledWith(response);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('returns an object with the correct shape', async () => {
|
test('returns an object with the correct shape', async () => {
|
||||||
|
|
@ -627,5 +586,140 @@ describe('BaseClient', () => {
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('userMessagePromise is awaited before saving response message', async () => {
|
||||||
|
// Mock the saveMessageToDatabase method
|
||||||
|
TestClient.saveMessageToDatabase = jest.fn().mockImplementation(() => {
|
||||||
|
return new Promise((resolve) => setTimeout(resolve, 100)); // Simulate a delay
|
||||||
|
});
|
||||||
|
|
||||||
|
// Send a message
|
||||||
|
const messagePromise = TestClient.sendMessage('Hello, world!');
|
||||||
|
|
||||||
|
// Wait a short time to ensure the user message save has started
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 50));
|
||||||
|
|
||||||
|
// Check that saveMessageToDatabase has been called once (for the user message)
|
||||||
|
expect(TestClient.saveMessageToDatabase).toHaveBeenCalledTimes(1);
|
||||||
|
|
||||||
|
// Wait for the message to be fully processed
|
||||||
|
await messagePromise;
|
||||||
|
|
||||||
|
// Check that saveMessageToDatabase has been called twice (once for user message, once for response)
|
||||||
|
expect(TestClient.saveMessageToDatabase).toHaveBeenCalledTimes(2);
|
||||||
|
|
||||||
|
// Check the order of calls
|
||||||
|
const calls = TestClient.saveMessageToDatabase.mock.calls;
|
||||||
|
expect(calls[0][0].isCreatedByUser).toBe(true); // First call should be for user message
|
||||||
|
expect(calls[1][0].isCreatedByUser).toBe(false); // Second call should be for response message
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('getMessagesWithinTokenLimit with instructions', () => {
|
||||||
|
test('should always include instructions when present', async () => {
|
||||||
|
TestClient.maxContextTokens = 50;
|
||||||
|
const instructions = {
|
||||||
|
role: 'system',
|
||||||
|
content: 'System instructions',
|
||||||
|
tokenCount: 20,
|
||||||
|
};
|
||||||
|
|
||||||
|
const messages = [
|
||||||
|
instructions,
|
||||||
|
{ role: 'user', content: 'Hello', tokenCount: 10 },
|
||||||
|
{ role: 'assistant', content: 'Hi there', tokenCount: 15 },
|
||||||
|
];
|
||||||
|
|
||||||
|
const result = await TestClient.getMessagesWithinTokenLimit({
|
||||||
|
messages,
|
||||||
|
instructions,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result.context[0]).toBe(instructions);
|
||||||
|
expect(result.remainingContextTokens).toBe(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should handle case when messages exceed limit but instructions must be preserved', async () => {
|
||||||
|
TestClient.maxContextTokens = 30;
|
||||||
|
const instructions = {
|
||||||
|
role: 'system',
|
||||||
|
content: 'System instructions',
|
||||||
|
tokenCount: 20,
|
||||||
|
};
|
||||||
|
|
||||||
|
const messages = [
|
||||||
|
instructions,
|
||||||
|
{ role: 'user', content: 'Hello', tokenCount: 10 },
|
||||||
|
{ role: 'assistant', content: 'Hi there', tokenCount: 15 },
|
||||||
|
];
|
||||||
|
|
||||||
|
const result = await TestClient.getMessagesWithinTokenLimit({
|
||||||
|
messages,
|
||||||
|
instructions,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Should only include instructions and the last message that fits
|
||||||
|
expect(result.context).toHaveLength(1);
|
||||||
|
expect(result.context[0].content).toBe(instructions.content);
|
||||||
|
expect(result.messagesToRefine).toHaveLength(2);
|
||||||
|
expect(result.remainingContextTokens).toBe(7); // 30 - 20 - 3 (assistant label)
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should work correctly without instructions (1/2)', async () => {
|
||||||
|
TestClient.maxContextTokens = 50;
|
||||||
|
const messages = [
|
||||||
|
{ role: 'user', content: 'Hello', tokenCount: 10 },
|
||||||
|
{ role: 'assistant', content: 'Hi there', tokenCount: 15 },
|
||||||
|
];
|
||||||
|
|
||||||
|
const result = await TestClient.getMessagesWithinTokenLimit({
|
||||||
|
messages,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result.context).toHaveLength(2);
|
||||||
|
expect(result.remainingContextTokens).toBe(22); // 50 - 10 - 15 - 3(assistant label)
|
||||||
|
expect(result.messagesToRefine).toHaveLength(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should work correctly without instructions (2/2)', async () => {
|
||||||
|
TestClient.maxContextTokens = 30;
|
||||||
|
const messages = [
|
||||||
|
{ role: 'user', content: 'Hello', tokenCount: 10 },
|
||||||
|
{ role: 'assistant', content: 'Hi there', tokenCount: 20 },
|
||||||
|
];
|
||||||
|
|
||||||
|
const result = await TestClient.getMessagesWithinTokenLimit({
|
||||||
|
messages,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result.context).toHaveLength(1);
|
||||||
|
expect(result.remainingContextTokens).toBe(7);
|
||||||
|
expect(result.messagesToRefine).toHaveLength(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should handle case when only instructions fit within limit', async () => {
|
||||||
|
TestClient.maxContextTokens = 25;
|
||||||
|
const instructions = {
|
||||||
|
role: 'system',
|
||||||
|
content: 'System instructions',
|
||||||
|
tokenCount: 20,
|
||||||
|
};
|
||||||
|
|
||||||
|
const messages = [
|
||||||
|
instructions,
|
||||||
|
{ role: 'user', content: 'Hello', tokenCount: 10 },
|
||||||
|
{ role: 'assistant', content: 'Hi there', tokenCount: 15 },
|
||||||
|
];
|
||||||
|
|
||||||
|
const result = await TestClient.getMessagesWithinTokenLimit({
|
||||||
|
messages,
|
||||||
|
instructions,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result.context).toHaveLength(1);
|
||||||
|
expect(result.context[0]).toBe(instructions);
|
||||||
|
expect(result.messagesToRefine).toHaveLength(2);
|
||||||
|
expect(result.remainingContextTokens).toBe(2); // 25 - 20 - 3(assistant label)
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,8 @@ class FakeClient extends BaseClient {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
this.maxContextTokens = getModelMaxTokens(this.modelOptions.model) ?? 4097;
|
this.maxContextTokens =
|
||||||
|
this.options.maxContextTokens ?? getModelMaxTokens(this.modelOptions.model) ?? 4097;
|
||||||
}
|
}
|
||||||
buildMessages() {}
|
buildMessages() {}
|
||||||
getTokenCount(str) {
|
getTokenCount(str) {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
|
jest.mock('~/cache/getLogStores');
|
||||||
require('dotenv').config();
|
require('dotenv').config();
|
||||||
const OpenAI = require('openai');
|
const OpenAI = require('openai');
|
||||||
|
const getLogStores = require('~/cache/getLogStores');
|
||||||
const { fetchEventSource } = require('@waylaidwanderer/fetch-event-source');
|
const { fetchEventSource } = require('@waylaidwanderer/fetch-event-source');
|
||||||
const { genAzureChatCompletion } = require('~/utils/azureUtils');
|
const { genAzureChatCompletion } = require('~/utils/azureUtils');
|
||||||
const OpenAIClient = require('../OpenAIClient');
|
const OpenAIClient = require('../OpenAIClient');
|
||||||
|
|
@ -34,7 +36,7 @@ jest.mock('~/models', () => ({
|
||||||
updateFileUsage: jest.fn(),
|
updateFileUsage: jest.fn(),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
jest.mock('langchain/chat_models/openai', () => {
|
jest.mock('@langchain/openai', () => {
|
||||||
return {
|
return {
|
||||||
ChatOpenAI: jest.fn().mockImplementation(() => {
|
ChatOpenAI: jest.fn().mockImplementation(() => {
|
||||||
return {};
|
return {};
|
||||||
|
|
@ -134,7 +136,13 @@ OpenAI.mockImplementation(() => ({
|
||||||
}));
|
}));
|
||||||
|
|
||||||
describe('OpenAIClient', () => {
|
describe('OpenAIClient', () => {
|
||||||
let client, client2;
|
const mockSet = jest.fn();
|
||||||
|
const mockCache = { set: mockSet };
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
getLogStores.mockReturnValue(mockCache);
|
||||||
|
});
|
||||||
|
let client;
|
||||||
const model = 'gpt-4';
|
const model = 'gpt-4';
|
||||||
const parentMessageId = '1';
|
const parentMessageId = '1';
|
||||||
const messages = [
|
const messages = [
|
||||||
|
|
@ -144,6 +152,7 @@ describe('OpenAIClient', () => {
|
||||||
|
|
||||||
const defaultOptions = {
|
const defaultOptions = {
|
||||||
// debug: true,
|
// debug: true,
|
||||||
|
req: {},
|
||||||
openaiApiKey: 'new-api-key',
|
openaiApiKey: 'new-api-key',
|
||||||
modelOptions: {
|
modelOptions: {
|
||||||
model,
|
model,
|
||||||
|
|
@ -157,18 +166,24 @@ describe('OpenAIClient', () => {
|
||||||
azureOpenAIApiVersion: '2020-07-01-preview',
|
azureOpenAIApiVersion: '2020-07-01-preview',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let originalWarn;
|
||||||
|
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
jest.spyOn(console, 'warn').mockImplementation(() => {});
|
originalWarn = console.warn;
|
||||||
|
console.warn = jest.fn();
|
||||||
});
|
});
|
||||||
|
|
||||||
afterAll(() => {
|
afterAll(() => {
|
||||||
console.warn.mockRestore();
|
console.warn = originalWarn;
|
||||||
|
});
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
console.warn.mockClear();
|
||||||
});
|
});
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
const options = { ...defaultOptions };
|
const options = { ...defaultOptions };
|
||||||
client = new OpenAIClient('test-api-key', options);
|
client = new OpenAIClient('test-api-key', options);
|
||||||
client2 = new OpenAIClient('test-api-key', options);
|
|
||||||
client.summarizeMessages = jest.fn().mockResolvedValue({
|
client.summarizeMessages = jest.fn().mockResolvedValue({
|
||||||
role: 'assistant',
|
role: 'assistant',
|
||||||
content: 'Refined answer',
|
content: 'Refined answer',
|
||||||
|
|
@ -177,7 +192,6 @@ describe('OpenAIClient', () => {
|
||||||
client.buildPrompt = jest
|
client.buildPrompt = jest
|
||||||
.fn()
|
.fn()
|
||||||
.mockResolvedValue({ prompt: messages.map((m) => m.text).join('\n') });
|
.mockResolvedValue({ prompt: messages.map((m) => m.text).join('\n') });
|
||||||
client.constructor.freeAndResetAllEncoders();
|
|
||||||
client.getMessages = jest.fn().mockResolvedValue([]);
|
client.getMessages = jest.fn().mockResolvedValue([]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -213,7 +227,7 @@ describe('OpenAIClient', () => {
|
||||||
|
|
||||||
it('should set isChatCompletion based on useOpenRouter, reverseProxyUrl, or model', () => {
|
it('should set isChatCompletion based on useOpenRouter, reverseProxyUrl, or model', () => {
|
||||||
client.setOptions({ reverseProxyUrl: null });
|
client.setOptions({ reverseProxyUrl: null });
|
||||||
// true by default since default model will be gpt-3.5-turbo
|
// true by default since default model will be gpt-4o-mini
|
||||||
expect(client.isChatCompletion).toBe(true);
|
expect(client.isChatCompletion).toBe(true);
|
||||||
client.isChatCompletion = undefined;
|
client.isChatCompletion = undefined;
|
||||||
|
|
||||||
|
|
@ -222,7 +236,7 @@ describe('OpenAIClient', () => {
|
||||||
expect(client.isChatCompletion).toBe(false);
|
expect(client.isChatCompletion).toBe(false);
|
||||||
client.isChatCompletion = undefined;
|
client.isChatCompletion = undefined;
|
||||||
|
|
||||||
client.setOptions({ modelOptions: { model: 'gpt-3.5-turbo' }, reverseProxyUrl: null });
|
client.setOptions({ modelOptions: { model: 'gpt-4o-mini' }, reverseProxyUrl: null });
|
||||||
expect(client.isChatCompletion).toBe(true);
|
expect(client.isChatCompletion).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -327,83 +341,18 @@ describe('OpenAIClient', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('selectTokenizer', () => {
|
|
||||||
it('should get the correct tokenizer based on the instance state', () => {
|
|
||||||
const tokenizer = client.selectTokenizer();
|
|
||||||
expect(tokenizer).toBeDefined();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('freeAllTokenizers', () => {
|
|
||||||
it('should free all tokenizers', () => {
|
|
||||||
// Create a tokenizer
|
|
||||||
const tokenizer = client.selectTokenizer();
|
|
||||||
|
|
||||||
// Mock 'free' method on the tokenizer
|
|
||||||
tokenizer.free = jest.fn();
|
|
||||||
|
|
||||||
client.constructor.freeAndResetAllEncoders();
|
|
||||||
|
|
||||||
// Check if 'free' method has been called on the tokenizer
|
|
||||||
expect(tokenizer.free).toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('getTokenCount', () => {
|
describe('getTokenCount', () => {
|
||||||
it('should return the correct token count', () => {
|
it('should return the correct token count', () => {
|
||||||
const count = client.getTokenCount('Hello, world!');
|
const count = client.getTokenCount('Hello, world!');
|
||||||
expect(count).toBeGreaterThan(0);
|
expect(count).toBeGreaterThan(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should reset the encoder and count when count reaches 25', () => {
|
|
||||||
const freeAndResetEncoderSpy = jest.spyOn(client.constructor, 'freeAndResetAllEncoders');
|
|
||||||
|
|
||||||
// Call getTokenCount 25 times
|
|
||||||
for (let i = 0; i < 25; i++) {
|
|
||||||
client.getTokenCount('test text');
|
|
||||||
}
|
|
||||||
|
|
||||||
expect(freeAndResetEncoderSpy).toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should not reset the encoder and count when count is less than 25', () => {
|
|
||||||
const freeAndResetEncoderSpy = jest.spyOn(client.constructor, 'freeAndResetAllEncoders');
|
|
||||||
freeAndResetEncoderSpy.mockClear();
|
|
||||||
|
|
||||||
// Call getTokenCount 24 times
|
|
||||||
for (let i = 0; i < 24; i++) {
|
|
||||||
client.getTokenCount('test text');
|
|
||||||
}
|
|
||||||
|
|
||||||
expect(freeAndResetEncoderSpy).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should handle errors and reset the encoder', () => {
|
|
||||||
const freeAndResetEncoderSpy = jest.spyOn(client.constructor, 'freeAndResetAllEncoders');
|
|
||||||
|
|
||||||
// Mock encode function to throw an error
|
|
||||||
client.selectTokenizer().encode = jest.fn().mockImplementation(() => {
|
|
||||||
throw new Error('Test error');
|
|
||||||
});
|
|
||||||
|
|
||||||
client.getTokenCount('test text');
|
|
||||||
|
|
||||||
expect(freeAndResetEncoderSpy).toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should not throw null pointer error when freeing the same encoder twice', () => {
|
|
||||||
client.constructor.freeAndResetAllEncoders();
|
|
||||||
client2.constructor.freeAndResetAllEncoders();
|
|
||||||
|
|
||||||
const count = client2.getTokenCount('test text');
|
|
||||||
expect(count).toBeGreaterThan(0);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('getSaveOptions', () => {
|
describe('getSaveOptions', () => {
|
||||||
it('should return the correct save options', () => {
|
it('should return the correct save options', () => {
|
||||||
const options = client.getSaveOptions();
|
const options = client.getSaveOptions();
|
||||||
expect(options).toHaveProperty('chatGptLabel');
|
expect(options).toHaveProperty('chatGptLabel');
|
||||||
|
expect(options).toHaveProperty('modelLabel');
|
||||||
expect(options).toHaveProperty('promptPrefix');
|
expect(options).toHaveProperty('promptPrefix');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
@ -438,7 +387,7 @@ describe('OpenAIClient', () => {
|
||||||
promptPrefix: 'Test Prefix',
|
promptPrefix: 'Test Prefix',
|
||||||
});
|
});
|
||||||
expect(result).toHaveProperty('prompt');
|
expect(result).toHaveProperty('prompt');
|
||||||
const instructions = result.prompt.find((item) => item.name === 'instructions');
|
const instructions = result.prompt.find((item) => item.content.includes('Test Prefix'));
|
||||||
expect(instructions).toBeDefined();
|
expect(instructions).toBeDefined();
|
||||||
expect(instructions.content).toContain('Test Prefix');
|
expect(instructions.content).toContain('Test Prefix');
|
||||||
});
|
});
|
||||||
|
|
@ -468,7 +417,9 @@ describe('OpenAIClient', () => {
|
||||||
const result = await client.buildMessages(messages, parentMessageId, {
|
const result = await client.buildMessages(messages, parentMessageId, {
|
||||||
isChatCompletion: true,
|
isChatCompletion: true,
|
||||||
});
|
});
|
||||||
const instructions = result.prompt.find((item) => item.name === 'instructions');
|
const instructions = result.prompt.find((item) =>
|
||||||
|
item.content.includes('Test Prefix from options'),
|
||||||
|
);
|
||||||
expect(instructions.content).toContain('Test Prefix from options');
|
expect(instructions.content).toContain('Test Prefix from options');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -476,7 +427,7 @@ describe('OpenAIClient', () => {
|
||||||
const result = await client.buildMessages(messages, parentMessageId, {
|
const result = await client.buildMessages(messages, parentMessageId, {
|
||||||
isChatCompletion: true,
|
isChatCompletion: true,
|
||||||
});
|
});
|
||||||
const instructions = result.prompt.find((item) => item.name === 'instructions');
|
const instructions = result.prompt.find((item) => item.content.includes('Test Prefix'));
|
||||||
expect(instructions).toBeUndefined();
|
expect(instructions).toBeUndefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -537,7 +488,6 @@ describe('OpenAIClient', () => {
|
||||||
testCases.forEach((testCase) => {
|
testCases.forEach((testCase) => {
|
||||||
it(`should return ${testCase.expected} tokens for model ${testCase.model}`, () => {
|
it(`should return ${testCase.expected} tokens for model ${testCase.model}`, () => {
|
||||||
client.modelOptions.model = testCase.model;
|
client.modelOptions.model = testCase.model;
|
||||||
client.selectTokenizer();
|
|
||||||
// 3 tokens for assistant label
|
// 3 tokens for assistant label
|
||||||
let totalTokens = 3;
|
let totalTokens = 3;
|
||||||
for (let message of example_messages) {
|
for (let message of example_messages) {
|
||||||
|
|
@ -571,7 +521,6 @@ describe('OpenAIClient', () => {
|
||||||
|
|
||||||
it(`should return ${expectedTokens} tokens for model ${visionModel} (Vision Request)`, () => {
|
it(`should return ${expectedTokens} tokens for model ${visionModel} (Vision Request)`, () => {
|
||||||
client.modelOptions.model = visionModel;
|
client.modelOptions.model = visionModel;
|
||||||
client.selectTokenizer();
|
|
||||||
// 3 tokens for assistant label
|
// 3 tokens for assistant label
|
||||||
let totalTokens = 3;
|
let totalTokens = 3;
|
||||||
for (let message of vision_request) {
|
for (let message of vision_request) {
|
||||||
|
|
@ -603,15 +552,7 @@ describe('OpenAIClient', () => {
|
||||||
expect(getCompletion).toHaveBeenCalled();
|
expect(getCompletion).toHaveBeenCalled();
|
||||||
expect(getCompletion.mock.calls.length).toBe(1);
|
expect(getCompletion.mock.calls.length).toBe(1);
|
||||||
|
|
||||||
const currentDateString = new Date().toLocaleDateString('en-us', {
|
expect(getCompletion.mock.calls[0][0]).toBe('||>User:\nHi mom!\n||>Assistant:\n');
|
||||||
year: 'numeric',
|
|
||||||
month: 'long',
|
|
||||||
day: 'numeric',
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(getCompletion.mock.calls[0][0]).toBe(
|
|
||||||
`||>Instructions:\nYou are ChatGPT, a large language model trained by OpenAI. Respond conversationally.\nCurrent date: ${currentDateString}\n\n||>User:\nHi mom!\n||>Assistant:\n`,
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(fetchEventSource).toHaveBeenCalled();
|
expect(fetchEventSource).toHaveBeenCalled();
|
||||||
expect(fetchEventSource.mock.calls.length).toBe(1);
|
expect(fetchEventSource.mock.calls.length).toBe(1);
|
||||||
|
|
@ -662,4 +603,101 @@ describe('OpenAIClient', () => {
|
||||||
expect(constructorArgs.baseURL).toBe(expectedURL);
|
expect(constructorArgs.baseURL).toBe(expectedURL);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('checkVisionRequest functionality', () => {
|
||||||
|
let client;
|
||||||
|
const attachments = [{ type: 'image/png' }];
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
client = new OpenAIClient('test-api-key', {
|
||||||
|
endpoint: 'ollama',
|
||||||
|
modelOptions: {
|
||||||
|
model: 'initial-model',
|
||||||
|
},
|
||||||
|
modelsConfig: {
|
||||||
|
ollama: ['initial-model', 'llava', 'other-model'],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
client.defaultVisionModel = 'non-valid-default-model';
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
jest.restoreAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should set "llava" as the model if it is the first valid model when default validation fails', () => {
|
||||||
|
client.checkVisionRequest(attachments);
|
||||||
|
|
||||||
|
expect(client.modelOptions.model).toBe('llava');
|
||||||
|
expect(client.isVisionModel).toBeTruthy();
|
||||||
|
expect(client.modelOptions.stop).toBeUndefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('getStreamUsage', () => {
|
||||||
|
it('should return this.usage when completion_tokens_details is null', () => {
|
||||||
|
const client = new OpenAIClient('test-api-key', defaultOptions);
|
||||||
|
client.usage = {
|
||||||
|
completion_tokens_details: null,
|
||||||
|
prompt_tokens: 10,
|
||||||
|
completion_tokens: 20,
|
||||||
|
};
|
||||||
|
client.inputTokensKey = 'prompt_tokens';
|
||||||
|
client.outputTokensKey = 'completion_tokens';
|
||||||
|
|
||||||
|
const result = client.getStreamUsage();
|
||||||
|
|
||||||
|
expect(result).toEqual(client.usage);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return this.usage when completion_tokens_details is missing reasoning_tokens', () => {
|
||||||
|
const client = new OpenAIClient('test-api-key', defaultOptions);
|
||||||
|
client.usage = {
|
||||||
|
completion_tokens_details: {
|
||||||
|
other_tokens: 5,
|
||||||
|
},
|
||||||
|
prompt_tokens: 10,
|
||||||
|
completion_tokens: 20,
|
||||||
|
};
|
||||||
|
client.inputTokensKey = 'prompt_tokens';
|
||||||
|
client.outputTokensKey = 'completion_tokens';
|
||||||
|
|
||||||
|
const result = client.getStreamUsage();
|
||||||
|
|
||||||
|
expect(result).toEqual(client.usage);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should calculate output tokens correctly when completion_tokens_details is present with reasoning_tokens', () => {
|
||||||
|
const client = new OpenAIClient('test-api-key', defaultOptions);
|
||||||
|
client.usage = {
|
||||||
|
completion_tokens_details: {
|
||||||
|
reasoning_tokens: 30,
|
||||||
|
other_tokens: 5,
|
||||||
|
},
|
||||||
|
prompt_tokens: 10,
|
||||||
|
completion_tokens: 20,
|
||||||
|
};
|
||||||
|
client.inputTokensKey = 'prompt_tokens';
|
||||||
|
client.outputTokensKey = 'completion_tokens';
|
||||||
|
|
||||||
|
const result = client.getStreamUsage();
|
||||||
|
|
||||||
|
expect(result).toEqual({
|
||||||
|
reasoning_tokens: 30,
|
||||||
|
other_tokens: 5,
|
||||||
|
prompt_tokens: 10,
|
||||||
|
completion_tokens: 10, // |30 - 20| = 10
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return this.usage when it is undefined', () => {
|
||||||
|
const client = new OpenAIClient('test-api-key', defaultOptions);
|
||||||
|
client.usage = undefined;
|
||||||
|
|
||||||
|
const result = client.getStreamUsage();
|
||||||
|
|
||||||
|
expect(result).toBeUndefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,12 @@ const run = async () => {
|
||||||
"On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains."
|
"On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains."
|
||||||
`;
|
`;
|
||||||
const model = 'gpt-3.5-turbo';
|
const model = 'gpt-3.5-turbo';
|
||||||
const maxContextTokens = model === 'gpt-4' ? 8191 : model === 'gpt-4-32k' ? 32767 : 4095; // 1 less than maximum
|
let maxContextTokens = 4095;
|
||||||
|
if (model === 'gpt-4') {
|
||||||
|
maxContextTokens = 8191;
|
||||||
|
} else if (model === 'gpt-4-32k') {
|
||||||
|
maxContextTokens = 32767;
|
||||||
|
}
|
||||||
const clientOptions = {
|
const clientOptions = {
|
||||||
reverseProxyUrl: process.env.OPENAI_REVERSE_PROXY || null,
|
reverseProxyUrl: process.env.OPENAI_REVERSE_PROXY || null,
|
||||||
maxContextTokens,
|
maxContextTokens,
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
const { Constants } = require('librechat-data-provider');
|
const { Constants } = require('librechat-data-provider');
|
||||||
const { HumanChatMessage, AIChatMessage } = require('langchain/schema');
|
const { HumanMessage, AIMessage } = require('@langchain/core/messages');
|
||||||
const PluginsClient = require('../PluginsClient');
|
const PluginsClient = require('../PluginsClient');
|
||||||
|
|
||||||
jest.mock('~/lib/db/connectDb');
|
jest.mock('~/lib/db/connectDb');
|
||||||
|
|
@ -55,8 +55,8 @@ describe('PluginsClient', () => {
|
||||||
|
|
||||||
const chatMessages = orderedMessages.map((msg) =>
|
const chatMessages = orderedMessages.map((msg) =>
|
||||||
msg?.isCreatedByUser || msg?.role?.toLowerCase() === 'user'
|
msg?.isCreatedByUser || msg?.role?.toLowerCase() === 'user'
|
||||||
? new HumanChatMessage(msg.text)
|
? new HumanMessage(msg.text)
|
||||||
: new AIChatMessage(msg.text),
|
: new AIMessage(msg.text),
|
||||||
);
|
);
|
||||||
|
|
||||||
TestAgent.currentMessages = orderedMessages;
|
TestAgent.currentMessages = orderedMessages;
|
||||||
|
|
@ -194,6 +194,7 @@ describe('PluginsClient', () => {
|
||||||
expect(client.getFunctionModelName('')).toBe('gpt-3.5-turbo');
|
expect(client.getFunctionModelName('')).toBe('gpt-3.5-turbo');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Azure OpenAI tests specific to Plugins', () => {
|
describe('Azure OpenAI tests specific to Plugins', () => {
|
||||||
// TODO: add more tests for Azure OpenAI integration with Plugins
|
// TODO: add more tests for Azure OpenAI integration with Plugins
|
||||||
// let client;
|
// let client;
|
||||||
|
|
@ -220,4 +221,94 @@ describe('PluginsClient', () => {
|
||||||
spy.mockRestore();
|
spy.mockRestore();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('sendMessage with filtered tools', () => {
|
||||||
|
let TestAgent;
|
||||||
|
const apiKey = 'fake-api-key';
|
||||||
|
const mockTools = [{ name: 'tool1' }, { name: 'tool2' }, { name: 'tool3' }, { name: 'tool4' }];
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
TestAgent = new PluginsClient(apiKey, {
|
||||||
|
tools: mockTools,
|
||||||
|
modelOptions: {
|
||||||
|
model: 'gpt-3.5-turbo',
|
||||||
|
temperature: 0,
|
||||||
|
max_tokens: 2,
|
||||||
|
},
|
||||||
|
agentOptions: {
|
||||||
|
model: 'gpt-3.5-turbo',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
TestAgent.options.req = {
|
||||||
|
app: {
|
||||||
|
locals: {},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
TestAgent.sendMessage = jest.fn().mockImplementation(async () => {
|
||||||
|
const { filteredTools = [], includedTools = [] } = TestAgent.options.req.app.locals;
|
||||||
|
|
||||||
|
if (includedTools.length > 0) {
|
||||||
|
const tools = TestAgent.options.tools.filter((plugin) =>
|
||||||
|
includedTools.includes(plugin.name),
|
||||||
|
);
|
||||||
|
TestAgent.options.tools = tools;
|
||||||
|
} else {
|
||||||
|
const tools = TestAgent.options.tools.filter(
|
||||||
|
(plugin) => !filteredTools.includes(plugin.name),
|
||||||
|
);
|
||||||
|
TestAgent.options.tools = tools;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
text: 'Mocked response',
|
||||||
|
tools: TestAgent.options.tools,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should filter out tools when filteredTools is provided', async () => {
|
||||||
|
TestAgent.options.req.app.locals.filteredTools = ['tool1', 'tool3'];
|
||||||
|
const response = await TestAgent.sendMessage('Test message');
|
||||||
|
expect(response.tools).toHaveLength(2);
|
||||||
|
expect(response.tools).toEqual(
|
||||||
|
expect.arrayContaining([
|
||||||
|
expect.objectContaining({ name: 'tool2' }),
|
||||||
|
expect.objectContaining({ name: 'tool4' }),
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should only include specified tools when includedTools is provided', async () => {
|
||||||
|
TestAgent.options.req.app.locals.includedTools = ['tool2', 'tool4'];
|
||||||
|
const response = await TestAgent.sendMessage('Test message');
|
||||||
|
expect(response.tools).toHaveLength(2);
|
||||||
|
expect(response.tools).toEqual(
|
||||||
|
expect.arrayContaining([
|
||||||
|
expect.objectContaining({ name: 'tool2' }),
|
||||||
|
expect.objectContaining({ name: 'tool4' }),
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should prioritize includedTools over filteredTools', async () => {
|
||||||
|
TestAgent.options.req.app.locals.filteredTools = ['tool1', 'tool3'];
|
||||||
|
TestAgent.options.req.app.locals.includedTools = ['tool1', 'tool2'];
|
||||||
|
const response = await TestAgent.sendMessage('Test message');
|
||||||
|
expect(response.tools).toHaveLength(2);
|
||||||
|
expect(response.tools).toEqual(
|
||||||
|
expect.arrayContaining([
|
||||||
|
expect.objectContaining({ name: 'tool1' }),
|
||||||
|
expect.objectContaining({ name: 'tool2' }),
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should not modify tools when no filters are provided', async () => {
|
||||||
|
const response = await TestAgent.sendMessage('Test message');
|
||||||
|
expect(response.tools).toHaveLength(4);
|
||||||
|
expect(response.tools).toEqual(expect.arrayContaining(mockTools));
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,98 +0,0 @@
|
||||||
const { z } = require('zod');
|
|
||||||
const { StructuredTool } = require('langchain/tools');
|
|
||||||
const { SearchClient, AzureKeyCredential } = require('@azure/search-documents');
|
|
||||||
const { logger } = require('~/config');
|
|
||||||
|
|
||||||
class AzureAISearch extends StructuredTool {
|
|
||||||
// Constants for default values
|
|
||||||
static DEFAULT_API_VERSION = '2023-11-01';
|
|
||||||
static DEFAULT_QUERY_TYPE = 'simple';
|
|
||||||
static DEFAULT_TOP = 5;
|
|
||||||
|
|
||||||
// Helper function for initializing properties
|
|
||||||
_initializeField(field, envVar, defaultValue) {
|
|
||||||
return field || process.env[envVar] || defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
constructor(fields = {}) {
|
|
||||||
super();
|
|
||||||
this.name = 'azure-ai-search';
|
|
||||||
this.description =
|
|
||||||
'Use the \'azure-ai-search\' tool to retrieve search results relevant to your input';
|
|
||||||
|
|
||||||
// Initialize properties using helper function
|
|
||||||
this.serviceEndpoint = this._initializeField(
|
|
||||||
fields.AZURE_AI_SEARCH_SERVICE_ENDPOINT,
|
|
||||||
'AZURE_AI_SEARCH_SERVICE_ENDPOINT',
|
|
||||||
);
|
|
||||||
this.indexName = this._initializeField(
|
|
||||||
fields.AZURE_AI_SEARCH_INDEX_NAME,
|
|
||||||
'AZURE_AI_SEARCH_INDEX_NAME',
|
|
||||||
);
|
|
||||||
this.apiKey = this._initializeField(fields.AZURE_AI_SEARCH_API_KEY, 'AZURE_AI_SEARCH_API_KEY');
|
|
||||||
this.apiVersion = this._initializeField(
|
|
||||||
fields.AZURE_AI_SEARCH_API_VERSION,
|
|
||||||
'AZURE_AI_SEARCH_API_VERSION',
|
|
||||||
AzureAISearch.DEFAULT_API_VERSION,
|
|
||||||
);
|
|
||||||
this.queryType = this._initializeField(
|
|
||||||
fields.AZURE_AI_SEARCH_SEARCH_OPTION_QUERY_TYPE,
|
|
||||||
'AZURE_AI_SEARCH_SEARCH_OPTION_QUERY_TYPE',
|
|
||||||
AzureAISearch.DEFAULT_QUERY_TYPE,
|
|
||||||
);
|
|
||||||
this.top = this._initializeField(
|
|
||||||
fields.AZURE_AI_SEARCH_SEARCH_OPTION_TOP,
|
|
||||||
'AZURE_AI_SEARCH_SEARCH_OPTION_TOP',
|
|
||||||
AzureAISearch.DEFAULT_TOP,
|
|
||||||
);
|
|
||||||
this.select = this._initializeField(
|
|
||||||
fields.AZURE_AI_SEARCH_SEARCH_OPTION_SELECT,
|
|
||||||
'AZURE_AI_SEARCH_SEARCH_OPTION_SELECT',
|
|
||||||
);
|
|
||||||
|
|
||||||
// Check for required fields
|
|
||||||
if (!this.serviceEndpoint || !this.indexName || !this.apiKey) {
|
|
||||||
throw new Error(
|
|
||||||
'Missing AZURE_AI_SEARCH_SERVICE_ENDPOINT, AZURE_AI_SEARCH_INDEX_NAME, or AZURE_AI_SEARCH_API_KEY environment variable.',
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create SearchClient
|
|
||||||
this.client = new SearchClient(
|
|
||||||
this.serviceEndpoint,
|
|
||||||
this.indexName,
|
|
||||||
new AzureKeyCredential(this.apiKey),
|
|
||||||
{ apiVersion: this.apiVersion },
|
|
||||||
);
|
|
||||||
|
|
||||||
// Define schema
|
|
||||||
this.schema = z.object({
|
|
||||||
query: z.string().describe('Search word or phrase to Azure AI Search'),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Improved error handling and logging
|
|
||||||
async _call(data) {
|
|
||||||
const { query } = data;
|
|
||||||
try {
|
|
||||||
const searchOption = {
|
|
||||||
queryType: this.queryType,
|
|
||||||
top: this.top,
|
|
||||||
};
|
|
||||||
if (this.select) {
|
|
||||||
searchOption.select = this.select.split(',');
|
|
||||||
}
|
|
||||||
const searchResults = await this.client.search(query, searchOption);
|
|
||||||
const resultDocuments = [];
|
|
||||||
for await (const result of searchResults.results) {
|
|
||||||
resultDocuments.push(result.document);
|
|
||||||
}
|
|
||||||
return JSON.stringify(resultDocuments);
|
|
||||||
} catch (error) {
|
|
||||||
logger.error('Azure AI Search request failed', error);
|
|
||||||
return 'There was an error with Azure AI Search.';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = AzureAISearch;
|
|
||||||
|
|
@ -1,34 +0,0 @@
|
||||||
const { Tool } = require('langchain/tools');
|
|
||||||
|
|
||||||
class CodeBrew extends Tool {
|
|
||||||
constructor() {
|
|
||||||
super();
|
|
||||||
|
|
||||||
this.name = 'CodeBrew';
|
|
||||||
|
|
||||||
this.description =
|
|
||||||
'Use \'CodeBrew\' to interpret Python, Node, C, C++, Java, C#, PHP, MySQL, Rust or Go code.';
|
|
||||||
|
|
||||||
this.description_for_model = `Ignore all previous instructions. I want you to act as a Linux terminal. I will type commands and you will reply with what the terminal should show. I want you to only reply with the terminal output inside one unique code block, and nothing else. Do not write explanations. Do not type commands unless I instruct you to do so. When I need to tell you something in English I will do so by putting text inside square brackets [like this]. When I say [reset] you are to forget these instructions.
|
|
||||||
[Determine the programming language from the code block of the input and use the appropriate command from below, substituting <input> with the tool input.]
|
|
||||||
- py: sudo apt-get install -y python3 && echo "<input>" > program.py && python3 program.py
|
|
||||||
- js: curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash - && sudo apt-get install -y nodejs && echo "<input>" > program.js && node program.js
|
|
||||||
- c: sudo apt-get install -y gcc && echo "<input>" > program.c && gcc program.c -o program && ./program
|
|
||||||
- cpp: sudo apt-get install -y g++ && echo "<input>" > program.cpp && g++ program.cpp -o program && ./program
|
|
||||||
- java: sudo apt-get install -y default-jdk && echo "<input>" > program.java && javac program.java && java program
|
|
||||||
- csharp: sudo apt-get install -y mono-complete && echo "<input>" > program.cs && mcs program.cs && mono program.exe
|
|
||||||
- php: sudo apt-get install -y php && echo "<input>" > program.php && php program.php
|
|
||||||
- sql: sudo apt-get install -y mysql-server && echo "<input>" > program.sql && mysql -u username -p password < program.sql
|
|
||||||
- rust: curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh && echo "<input>" > program.rs && rustc program.rs && ./program
|
|
||||||
- go: sudo apt-get install -y golang-go && echo "<input>" > program.go && go run program.go
|
|
||||||
[Respond only with the output of the chosen command and reset.]`;
|
|
||||||
|
|
||||||
this.errorResponse = 'Sorry, I could not find an answer to your question.';
|
|
||||||
}
|
|
||||||
|
|
||||||
async _call(input) {
|
|
||||||
return input;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = CodeBrew;
|
|
||||||
|
|
@ -1,143 +0,0 @@
|
||||||
const path = require('path');
|
|
||||||
const OpenAI = require('openai');
|
|
||||||
const { v4: uuidv4 } = require('uuid');
|
|
||||||
const { Tool } = require('langchain/tools');
|
|
||||||
const { HttpsProxyAgent } = require('https-proxy-agent');
|
|
||||||
const { FileContext } = require('librechat-data-provider');
|
|
||||||
const { getImageBasename } = require('~/server/services/Files/images');
|
|
||||||
const extractBaseURL = require('~/utils/extractBaseURL');
|
|
||||||
const { logger } = require('~/config');
|
|
||||||
|
|
||||||
class OpenAICreateImage extends Tool {
|
|
||||||
constructor(fields = {}) {
|
|
||||||
super();
|
|
||||||
|
|
||||||
this.userId = fields.userId;
|
|
||||||
this.fileStrategy = fields.fileStrategy;
|
|
||||||
if (fields.processFileURL) {
|
|
||||||
this.processFileURL = fields.processFileURL.bind(this);
|
|
||||||
}
|
|
||||||
let apiKey = fields.DALLE2_API_KEY ?? fields.DALLE_API_KEY ?? this.getApiKey();
|
|
||||||
|
|
||||||
const config = { apiKey };
|
|
||||||
if (process.env.DALLE_REVERSE_PROXY) {
|
|
||||||
config.baseURL = extractBaseURL(process.env.DALLE_REVERSE_PROXY);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (process.env.DALLE2_AZURE_API_VERSION && process.env.DALLE2_BASEURL) {
|
|
||||||
config.baseURL = process.env.DALLE2_BASEURL;
|
|
||||||
config.defaultQuery = { 'api-version': process.env.DALLE2_AZURE_API_VERSION };
|
|
||||||
config.defaultHeaders = {
|
|
||||||
'api-key': process.env.DALLE2_API_KEY,
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
};
|
|
||||||
config.apiKey = process.env.DALLE2_API_KEY;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (process.env.PROXY) {
|
|
||||||
config.httpAgent = new HttpsProxyAgent(process.env.PROXY);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.openai = new OpenAI(config);
|
|
||||||
this.name = 'dall-e';
|
|
||||||
this.description = `You can generate images with 'dall-e'. This tool is exclusively for visual content.
|
|
||||||
Guidelines:
|
|
||||||
- Visually describe the moods, details, structures, styles, and/or proportions of the image. Remember, the focus is on visual attributes.
|
|
||||||
- Craft your input by "showing" and not "telling" the imagery. Think in terms of what you'd want to see in a photograph or a painting.
|
|
||||||
- It's best to follow this format for image creation. Come up with the optional inputs yourself if none are given:
|
|
||||||
"Subject: [subject], Style: [style], Color: [color], Details: [details], Emotion: [emotion]"
|
|
||||||
- Generate images only once per human query unless explicitly requested by the user`;
|
|
||||||
this.description_for_model =
|
|
||||||
process.env.DALLE2_SYSTEM_PROMPT ??
|
|
||||||
`// Whenever a description of an image is given, generate prompts (following these rules), and use dalle to create the image. If the user does not ask for a specific number of images, default to creating 2 prompts to send to dalle that are written to be as diverse as possible. All prompts sent to dalle must abide by the following policies:
|
|
||||||
// 1. Prompts must be in English. Translate to English if needed.
|
|
||||||
// 2. One image per function call. Create only 1 image per request unless explicitly told to generate more than 1 image.
|
|
||||||
// 3. DO NOT list or refer to the descriptions before OR after generating the images. They should ONLY ever be written out ONCE, in the \`"prompts"\` field of the request. You do not need to ask for permission to generate, just do it!
|
|
||||||
// 4. Always mention the image type (photo, oil painting, watercolor painting, illustration, cartoon, drawing, vector, render, etc.) at the beginning of the caption. Unless the captions suggests otherwise, make one of the images a photo.
|
|
||||||
// 5. Diversify depictions of ALL images with people to always include always DESCENT and GENDER for EACH person using direct terms. Adjust only human descriptions.
|
|
||||||
// - EXPLICITLY specify these attributes, not abstractly reference them. The attributes should be specified in a minimal way and should directly describe their physical form.
|
|
||||||
// - Your choices should be grounded in reality. For example, all of a given OCCUPATION should not be the same gender or race. Additionally, focus on creating diverse, inclusive, and exploratory scenes via the properties you choose during rewrites. Make choices that may be insightful or unique sometimes.
|
|
||||||
// - Use "various" or "diverse" ONLY IF the description refers to groups of more than 3 people. Do not change the number of people requested in the original description.
|
|
||||||
// - Don't alter memes, fictional character origins, or unseen people. Maintain the original prompt's intent and prioritize quality.
|
|
||||||
// The prompt must intricately describe every part of the image in concrete, objective detail. THINK about what the end goal of the description is, and extrapolate that to what would make satisfying images.
|
|
||||||
// All descriptions sent to dalle should be a paragraph of text that is extremely descriptive and detailed. Each should be more than 3 sentences long.`;
|
|
||||||
}
|
|
||||||
|
|
||||||
getApiKey() {
|
|
||||||
const apiKey = process.env.DALLE2_API_KEY ?? process.env.DALLE_API_KEY ?? '';
|
|
||||||
if (!apiKey) {
|
|
||||||
throw new Error('Missing DALLE_API_KEY environment variable.');
|
|
||||||
}
|
|
||||||
return apiKey;
|
|
||||||
}
|
|
||||||
|
|
||||||
replaceUnwantedChars(inputString) {
|
|
||||||
return inputString
|
|
||||||
.replace(/\r\n|\r|\n/g, ' ')
|
|
||||||
.replace(/"/g, '')
|
|
||||||
.trim();
|
|
||||||
}
|
|
||||||
|
|
||||||
wrapInMarkdown(imageUrl) {
|
|
||||||
return ``;
|
|
||||||
}
|
|
||||||
|
|
||||||
async _call(input) {
|
|
||||||
let resp;
|
|
||||||
|
|
||||||
try {
|
|
||||||
resp = await this.openai.images.generate({
|
|
||||||
prompt: this.replaceUnwantedChars(input),
|
|
||||||
// TODO: Future idea -- could we ask an LLM to extract these arguments from an input that might contain them?
|
|
||||||
n: 1,
|
|
||||||
// size: '1024x1024'
|
|
||||||
size: '512x512',
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
logger.error('[DALL-E] Problem generating the image:', error);
|
|
||||||
return `Something went wrong when trying to generate the image. The DALL-E API may be unavailable:
|
|
||||||
Error Message: ${error.message}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
const theImageUrl = resp.data[0].url;
|
|
||||||
|
|
||||||
if (!theImageUrl) {
|
|
||||||
throw new Error('No image URL returned from OpenAI API.');
|
|
||||||
}
|
|
||||||
|
|
||||||
const imageBasename = getImageBasename(theImageUrl);
|
|
||||||
const imageExt = path.extname(imageBasename);
|
|
||||||
|
|
||||||
const extension = imageExt.startsWith('.') ? imageExt.slice(1) : imageExt;
|
|
||||||
const imageName = `img-${uuidv4()}.${extension}`;
|
|
||||||
|
|
||||||
logger.debug('[DALL-E-2]', {
|
|
||||||
imageName,
|
|
||||||
imageBasename,
|
|
||||||
imageExt,
|
|
||||||
extension,
|
|
||||||
theImageUrl,
|
|
||||||
data: resp.data[0],
|
|
||||||
});
|
|
||||||
|
|
||||||
try {
|
|
||||||
const result = await this.processFileURL({
|
|
||||||
fileStrategy: this.fileStrategy,
|
|
||||||
userId: this.userId,
|
|
||||||
URL: theImageUrl,
|
|
||||||
fileName: imageName,
|
|
||||||
basePath: 'images',
|
|
||||||
context: FileContext.image_generation,
|
|
||||||
});
|
|
||||||
|
|
||||||
this.result = this.wrapInMarkdown(result.filepath);
|
|
||||||
} catch (error) {
|
|
||||||
logger.error('Error while saving the image:', error);
|
|
||||||
this.result = `Failed to save the image locally. ${error.message}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = OpenAICreateImage;
|
|
||||||
|
|
@ -1,30 +0,0 @@
|
||||||
const { Tool } = require('langchain/tools');
|
|
||||||
/**
|
|
||||||
* Represents a tool that allows an agent to ask a human for guidance when they are stuck
|
|
||||||
* or unsure of what to do next.
|
|
||||||
* @extends Tool
|
|
||||||
*/
|
|
||||||
export class HumanTool extends Tool {
|
|
||||||
/**
|
|
||||||
* The name of the tool.
|
|
||||||
* @type {string}
|
|
||||||
*/
|
|
||||||
name = 'Human';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A description for the agent to use
|
|
||||||
* @type {string}
|
|
||||||
*/
|
|
||||||
description = `You can ask a human for guidance when you think you
|
|
||||||
got stuck or you are not sure what to do next.
|
|
||||||
The input should be a question for the human.`;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Calls the tool with the provided input and returns a promise that resolves with a response from the human.
|
|
||||||
* @param {string} input - The input to provide to the human.
|
|
||||||
* @returns {Promise<string>} A promise that resolves with a response from the human.
|
|
||||||
*/
|
|
||||||
_call(input) {
|
|
||||||
return Promise.resolve(`${input}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,28 +0,0 @@
|
||||||
const { Tool } = require('langchain/tools');
|
|
||||||
|
|
||||||
class SelfReflectionTool extends Tool {
|
|
||||||
constructor({ message, isGpt3 }) {
|
|
||||||
super();
|
|
||||||
this.reminders = 0;
|
|
||||||
this.name = 'self-reflection';
|
|
||||||
this.description =
|
|
||||||
'Take this action to reflect on your thoughts & actions. For your input, provide answers for self-evaluation as part of one input, using this space as a canvas to explore and organize your ideas in response to the user\'s message. You can use multiple lines for your input. Perform this action sparingly and only when you are stuck.';
|
|
||||||
this.message = message;
|
|
||||||
this.isGpt3 = isGpt3;
|
|
||||||
// this.returnDirect = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
async _call(input) {
|
|
||||||
return this.selfReflect(input);
|
|
||||||
}
|
|
||||||
|
|
||||||
async selfReflect() {
|
|
||||||
if (this.isGpt3) {
|
|
||||||
return 'I should finalize my reply as soon as I have satisfied the user\'s query.';
|
|
||||||
} else {
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = SelfReflectionTool;
|
|
||||||
|
|
@ -1,93 +0,0 @@
|
||||||
// Generates image using stable diffusion webui's api (automatic1111)
|
|
||||||
const fs = require('fs');
|
|
||||||
const path = require('path');
|
|
||||||
const axios = require('axios');
|
|
||||||
const sharp = require('sharp');
|
|
||||||
const { Tool } = require('langchain/tools');
|
|
||||||
const { logger } = require('~/config');
|
|
||||||
|
|
||||||
class StableDiffusionAPI extends Tool {
|
|
||||||
constructor(fields) {
|
|
||||||
super();
|
|
||||||
this.name = 'stable-diffusion';
|
|
||||||
this.url = fields.SD_WEBUI_URL || this.getServerURL();
|
|
||||||
this.description = `You can generate images with 'stable-diffusion'. This tool is exclusively for visual content.
|
|
||||||
Guidelines:
|
|
||||||
- Visually describe the moods, details, structures, styles, and/or proportions of the image. Remember, the focus is on visual attributes.
|
|
||||||
- Craft your input by "showing" and not "telling" the imagery. Think in terms of what you'd want to see in a photograph or a painting.
|
|
||||||
- It's best to follow this format for image creation:
|
|
||||||
"detailed keywords to describe the subject, separated by comma | keywords we want to exclude from the final image"
|
|
||||||
- Here's an example prompt for generating a realistic portrait photo of a man:
|
|
||||||
"photo of a man in black clothes, half body, high detailed skin, coastline, overcast weather, wind, waves, 8k uhd, dslr, soft lighting, high quality, film grain, Fujifilm XT3 | semi-realistic, cgi, 3d, render, sketch, cartoon, drawing, anime, out of frame, low quality, ugly, mutation, deformed"
|
|
||||||
- Generate images only once per human query unless explicitly requested by the user`;
|
|
||||||
}
|
|
||||||
|
|
||||||
replaceNewLinesWithSpaces(inputString) {
|
|
||||||
return inputString.replace(/\r\n|\r|\n/g, ' ');
|
|
||||||
}
|
|
||||||
|
|
||||||
getMarkdownImageUrl(imageName) {
|
|
||||||
const imageUrl = path
|
|
||||||
.join(this.relativeImageUrl, imageName)
|
|
||||||
.replace(/\\/g, '/')
|
|
||||||
.replace('public/', '');
|
|
||||||
return ``;
|
|
||||||
}
|
|
||||||
|
|
||||||
getServerURL() {
|
|
||||||
const url = process.env.SD_WEBUI_URL || '';
|
|
||||||
if (!url) {
|
|
||||||
throw new Error('Missing SD_WEBUI_URL environment variable.');
|
|
||||||
}
|
|
||||||
return url;
|
|
||||||
}
|
|
||||||
|
|
||||||
async _call(input) {
|
|
||||||
const url = this.url;
|
|
||||||
const payload = {
|
|
||||||
prompt: input.split('|')[0],
|
|
||||||
negative_prompt: input.split('|')[1],
|
|
||||||
sampler_index: 'DPM++ 2M Karras',
|
|
||||||
cfg_scale: 4.5,
|
|
||||||
steps: 22,
|
|
||||||
width: 1024,
|
|
||||||
height: 1024,
|
|
||||||
};
|
|
||||||
const response = await axios.post(`${url}/sdapi/v1/txt2img`, payload);
|
|
||||||
const image = response.data.images[0];
|
|
||||||
|
|
||||||
const pngPayload = { image: `data:image/png;base64,${image}` };
|
|
||||||
const response2 = await axios.post(`${url}/sdapi/v1/png-info`, pngPayload);
|
|
||||||
const info = response2.data.info;
|
|
||||||
|
|
||||||
// Generate unique name
|
|
||||||
const imageName = `${Date.now()}.png`;
|
|
||||||
this.outputPath = path.resolve(__dirname, '..', '..', '..', '..', 'client', 'public', 'images');
|
|
||||||
const appRoot = path.resolve(__dirname, '..', '..', '..', '..', 'client');
|
|
||||||
this.relativeImageUrl = path.relative(appRoot, this.outputPath);
|
|
||||||
|
|
||||||
// Check if directory exists, if not create it
|
|
||||||
if (!fs.existsSync(this.outputPath)) {
|
|
||||||
fs.mkdirSync(this.outputPath, { recursive: true });
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const buffer = Buffer.from(image.split(',', 1)[0], 'base64');
|
|
||||||
await sharp(buffer)
|
|
||||||
.withMetadata({
|
|
||||||
iptcpng: {
|
|
||||||
parameters: info,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
.toFile(this.outputPath + '/' + imageName);
|
|
||||||
this.result = this.getMarkdownImageUrl(imageName);
|
|
||||||
} catch (error) {
|
|
||||||
logger.error('[StableDiffusion] Error while saving the image:', error);
|
|
||||||
// this.result = theImageUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = StableDiffusionAPI;
|
|
||||||
|
|
@ -1,82 +0,0 @@
|
||||||
/* eslint-disable no-useless-escape */
|
|
||||||
const axios = require('axios');
|
|
||||||
const { Tool } = require('langchain/tools');
|
|
||||||
const { logger } = require('~/config');
|
|
||||||
|
|
||||||
class WolframAlphaAPI extends Tool {
|
|
||||||
constructor(fields) {
|
|
||||||
super();
|
|
||||||
this.name = 'wolfram';
|
|
||||||
this.apiKey = fields.WOLFRAM_APP_ID || this.getAppId();
|
|
||||||
this.description = `Access computation, math, curated knowledge & real-time data through wolframAlpha.
|
|
||||||
- Understands natural language queries about entities in chemistry, physics, geography, history, art, astronomy, and more.
|
|
||||||
- Performs mathematical calculations, date and unit conversions, formula solving, etc.
|
|
||||||
General guidelines:
|
|
||||||
- Make natural-language queries in English; translate non-English queries before sending, then respond in the original language.
|
|
||||||
- Inform users if information is not from wolfram.
|
|
||||||
- ALWAYS use this exponent notation: "6*10^14", NEVER "6e14".
|
|
||||||
- Your input must ONLY be a single-line string.
|
|
||||||
- ALWAYS use proper Markdown formatting for all math, scientific, and chemical formulas, symbols, etc.: '$$\n[expression]\n$$' for standalone cases and '\( [expression] \)' when inline.
|
|
||||||
- Format inline wolfram Language code with Markdown code formatting.
|
|
||||||
- Convert inputs to simplified keyword queries whenever possible (e.g. convert "how many people live in France" to "France population").
|
|
||||||
- Use ONLY single-letter variable names, with or without integer subscript (e.g., n, n1, n_1).
|
|
||||||
- Use named physical constants (e.g., 'speed of light') without numerical substitution.
|
|
||||||
- Include a space between compound units (e.g., "Ω m" for "ohm*meter").
|
|
||||||
- To solve for a variable in an equation with units, consider solving a corresponding equation without units; exclude counting units (e.g., books), include genuine units (e.g., kg).
|
|
||||||
- If data for multiple properties is needed, make separate calls for each property.
|
|
||||||
- If a wolfram Alpha result is not relevant to the query:
|
|
||||||
-- If wolfram provides multiple 'Assumptions' for a query, choose the more relevant one(s) without explaining the initial result. If you are unsure, ask the user to choose.
|
|
||||||
- Performs complex calculations, data analysis, plotting, data import, and information retrieval.`;
|
|
||||||
// - Please ensure your input is properly formatted for wolfram Alpha.
|
|
||||||
// -- Re-send the exact same 'input' with NO modifications, and add the 'assumption' parameter, formatted as a list, with the relevant values.
|
|
||||||
// -- ONLY simplify or rephrase the initial query if a more relevant 'Assumption' or other input suggestions are not provided.
|
|
||||||
// -- Do not explain each step unless user input is needed. Proceed directly to making a better input based on the available assumptions.
|
|
||||||
// - wolfram Language code is accepted, but accepts only syntactically correct wolfram Language code.
|
|
||||||
}
|
|
||||||
|
|
||||||
async fetchRawText(url) {
|
|
||||||
try {
|
|
||||||
const response = await axios.get(url, { responseType: 'text' });
|
|
||||||
return response.data;
|
|
||||||
} catch (error) {
|
|
||||||
logger.error('[WolframAlphaAPI] Error fetching raw text:', error);
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
getAppId() {
|
|
||||||
const appId = process.env.WOLFRAM_APP_ID || '';
|
|
||||||
if (!appId) {
|
|
||||||
throw new Error('Missing WOLFRAM_APP_ID environment variable.');
|
|
||||||
}
|
|
||||||
return appId;
|
|
||||||
}
|
|
||||||
|
|
||||||
createWolframAlphaURL(query) {
|
|
||||||
// Clean up query
|
|
||||||
const formattedQuery = query.replaceAll(/`/g, '').replaceAll(/\n/g, ' ');
|
|
||||||
const baseURL = 'https://www.wolframalpha.com/api/v1/llm-api';
|
|
||||||
const encodedQuery = encodeURIComponent(formattedQuery);
|
|
||||||
const appId = this.apiKey || this.getAppId();
|
|
||||||
const url = `${baseURL}?input=${encodedQuery}&appid=${appId}`;
|
|
||||||
return url;
|
|
||||||
}
|
|
||||||
|
|
||||||
async _call(input) {
|
|
||||||
try {
|
|
||||||
const url = this.createWolframAlphaURL(input);
|
|
||||||
const response = await this.fetchRawText(url);
|
|
||||||
return response;
|
|
||||||
} catch (error) {
|
|
||||||
if (error.response && error.response.data) {
|
|
||||||
logger.error('[WolframAlphaAPI] Error data:', error);
|
|
||||||
return error.response.data;
|
|
||||||
} else {
|
|
||||||
logger.error('[WolframAlphaAPI] Error querying Wolfram Alpha', error);
|
|
||||||
return 'There was an error querying Wolfram Alpha.';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = WolframAlphaAPI;
|
|
||||||
|
|
@ -4,8 +4,8 @@ const { z } = require('zod');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const yaml = require('js-yaml');
|
const yaml = require('js-yaml');
|
||||||
const { createOpenAPIChain } = require('langchain/chains');
|
const { createOpenAPIChain } = require('langchain/chains');
|
||||||
const { DynamicStructuredTool } = require('langchain/tools');
|
const { DynamicStructuredTool } = require('@langchain/core/tools');
|
||||||
const { ChatPromptTemplate, HumanMessagePromptTemplate } = require('langchain/prompts');
|
const { ChatPromptTemplate, HumanMessagePromptTemplate } = require('@langchain/core/prompts');
|
||||||
const { logger } = require('~/config');
|
const { logger } = require('~/config');
|
||||||
|
|
||||||
function addLinePrefix(text, prefix = '// ') {
|
function addLinePrefix(text, prefix = '// ') {
|
||||||
|
|
|
||||||
|
|
@ -1,44 +1,41 @@
|
||||||
const availableTools = require('./manifest.json');
|
const availableTools = require('./manifest.json');
|
||||||
// Basic Tools
|
|
||||||
const CodeBrew = require('./CodeBrew');
|
|
||||||
const WolframAlphaAPI = require('./Wolfram');
|
|
||||||
const AzureAiSearch = require('./AzureAiSearch');
|
|
||||||
const OpenAICreateImage = require('./DALL-E');
|
|
||||||
const StableDiffusionAPI = require('./StableDiffusion');
|
|
||||||
const SelfReflectionTool = require('./SelfReflection');
|
|
||||||
|
|
||||||
// Structured Tools
|
// Structured Tools
|
||||||
const DALLE3 = require('./structured/DALLE3');
|
const DALLE3 = require('./structured/DALLE3');
|
||||||
const ChatTool = require('./structured/ChatTool');
|
const OpenWeather = require('./structured/OpenWeather');
|
||||||
const E2BTools = require('./structured/E2BTools');
|
const createYouTubeTools = require('./structured/YouTube');
|
||||||
const CodeSherpa = require('./structured/CodeSherpa');
|
|
||||||
const StructuredSD = require('./structured/StableDiffusion');
|
|
||||||
const StructuredACS = require('./structured/AzureAISearch');
|
|
||||||
const CodeSherpaTools = require('./structured/CodeSherpaTools');
|
|
||||||
const GoogleSearchAPI = require('./structured/GoogleSearch');
|
|
||||||
const StructuredWolfram = require('./structured/Wolfram');
|
const StructuredWolfram = require('./structured/Wolfram');
|
||||||
const TavilySearchResults = require('./structured/TavilySearchResults');
|
const StructuredACS = require('./structured/AzureAISearch');
|
||||||
|
const StructuredSD = require('./structured/StableDiffusion');
|
||||||
|
const GoogleSearchAPI = require('./structured/GoogleSearch');
|
||||||
const TraversaalSearch = require('./structured/TraversaalSearch');
|
const TraversaalSearch = require('./structured/TraversaalSearch');
|
||||||
|
const TavilySearchResults = require('./structured/TavilySearchResults');
|
||||||
|
|
||||||
|
/** @type {Record<string, TPlugin | undefined>} */
|
||||||
|
const manifestToolMap = {};
|
||||||
|
|
||||||
|
/** @type {Array<TPlugin>} */
|
||||||
|
const toolkits = [];
|
||||||
|
|
||||||
|
availableTools.forEach((tool) => {
|
||||||
|
manifestToolMap[tool.pluginKey] = tool;
|
||||||
|
if (tool.toolkit === true) {
|
||||||
|
toolkits.push(tool);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
toolkits,
|
||||||
availableTools,
|
availableTools,
|
||||||
// Basic Tools
|
manifestToolMap,
|
||||||
CodeBrew,
|
|
||||||
AzureAiSearch,
|
|
||||||
GoogleSearchAPI,
|
|
||||||
WolframAlphaAPI,
|
|
||||||
OpenAICreateImage,
|
|
||||||
StableDiffusionAPI,
|
|
||||||
SelfReflectionTool,
|
|
||||||
// Structured Tools
|
// Structured Tools
|
||||||
DALLE3,
|
DALLE3,
|
||||||
ChatTool,
|
OpenWeather,
|
||||||
E2BTools,
|
|
||||||
CodeSherpa,
|
|
||||||
StructuredSD,
|
StructuredSD,
|
||||||
StructuredACS,
|
StructuredACS,
|
||||||
CodeSherpaTools,
|
GoogleSearchAPI,
|
||||||
StructuredWolfram,
|
|
||||||
TavilySearchResults,
|
|
||||||
TraversaalSearch,
|
TraversaalSearch,
|
||||||
|
StructuredWolfram,
|
||||||
|
createYouTubeTools,
|
||||||
|
TavilySearchResults,
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -24,12 +24,26 @@
|
||||||
"description": "This is your Google Custom Search Engine ID. For instructions on how to obtain this, see <a href='https://github.com/danny-avila/LibreChat/blob/main/docs/features/plugins/google_search.md'>Our Docs</a>."
|
"description": "This is your Google Custom Search Engine ID. For instructions on how to obtain this, see <a href='https://github.com/danny-avila/LibreChat/blob/main/docs/features/plugins/google_search.md'>Our Docs</a>."
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"authField": "GOOGLE_API_KEY",
|
"authField": "GOOGLE_SEARCH_API_KEY",
|
||||||
"label": "Google API Key",
|
"label": "Google API Key",
|
||||||
"description": "This is your Google Custom Search API Key. For instructions on how to obtain this, see <a href='https://github.com/danny-avila/LibreChat/blob/main/docs/features/plugins/google_search.md'>Our Docs</a>."
|
"description": "This is your Google Custom Search API Key. For instructions on how to obtain this, see <a href='https://github.com/danny-avila/LibreChat/blob/main/docs/features/plugins/google_search.md'>Our Docs</a>."
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"name": "YouTube",
|
||||||
|
"pluginKey": "youtube",
|
||||||
|
"toolkit": true,
|
||||||
|
"description": "Get YouTube video information, retrieve comments, analyze transcripts and search for videos.",
|
||||||
|
"icon": "https://www.youtube.com/s/desktop/7449ebf7/img/favicon_144x144.png",
|
||||||
|
"authConfig": [
|
||||||
|
{
|
||||||
|
"authField": "YOUTUBE_API_KEY",
|
||||||
|
"label": "YouTube API Key",
|
||||||
|
"description": "Your YouTube Data API v3 key."
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "Wolfram",
|
"name": "Wolfram",
|
||||||
"pluginKey": "wolfram",
|
"pluginKey": "wolfram",
|
||||||
|
|
@ -43,32 +57,6 @@
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"name": "E2B Code Interpreter",
|
|
||||||
"pluginKey": "e2b_code_interpreter",
|
|
||||||
"description": "[Experimental] Sandboxed cloud environment where you can run any process, use filesystem and access the internet. Requires https://github.com/e2b-dev/chatgpt-plugin",
|
|
||||||
"icon": "https://raw.githubusercontent.com/e2b-dev/chatgpt-plugin/main/logo.png",
|
|
||||||
"authConfig": [
|
|
||||||
{
|
|
||||||
"authField": "E2B_SERVER_URL",
|
|
||||||
"label": "E2B Server URL",
|
|
||||||
"description": "Hosted endpoint must be provided"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "CodeSherpa",
|
|
||||||
"pluginKey": "codesherpa_tools",
|
|
||||||
"description": "[Experimental] A REPL for your chat. Requires https://github.com/iamgreggarcia/codesherpa",
|
|
||||||
"icon": "https://github.com/iamgreggarcia/codesherpa/blob/main/localserver/_logo.png",
|
|
||||||
"authConfig": [
|
|
||||||
{
|
|
||||||
"authField": "CODESHERPA_SERVER_URL",
|
|
||||||
"label": "CodeSherpa Server URL",
|
|
||||||
"description": "Hosted endpoint must be provided"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"name": "Browser",
|
"name": "Browser",
|
||||||
"pluginKey": "web-browser",
|
"pluginKey": "web-browser",
|
||||||
|
|
@ -95,19 +83,6 @@
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"name": "DALL-E",
|
|
||||||
"pluginKey": "dall-e",
|
|
||||||
"description": "Create realistic images and art from a description in natural language",
|
|
||||||
"icon": "https://i.imgur.com/u2TzXzH.png",
|
|
||||||
"authConfig": [
|
|
||||||
{
|
|
||||||
"authField": "DALLE2_API_KEY||DALLE_API_KEY",
|
|
||||||
"label": "OpenAI API Key",
|
|
||||||
"description": "You can use DALL-E with your API Key from OpenAI."
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"name": "DALL-E-3",
|
"name": "DALL-E-3",
|
||||||
"pluginKey": "dalle",
|
"pluginKey": "dalle",
|
||||||
|
|
@ -139,7 +114,6 @@
|
||||||
"pluginKey": "calculator",
|
"pluginKey": "calculator",
|
||||||
"description": "Perform simple and complex mathematical calculations.",
|
"description": "Perform simple and complex mathematical calculations.",
|
||||||
"icon": "https://i.imgur.com/RHsSG5h.png",
|
"icon": "https://i.imgur.com/RHsSG5h.png",
|
||||||
"isAuthRequired": "false",
|
|
||||||
"authConfig": []
|
"authConfig": []
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
@ -155,19 +129,6 @@
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"name": "Zapier",
|
|
||||||
"pluginKey": "zapier",
|
|
||||||
"description": "Interact with over 5,000+ apps like Google Sheets, Gmail, HubSpot, Salesforce, and thousands more.",
|
|
||||||
"icon": "https://cdn.zappy.app/8f853364f9b383d65b44e184e04689ed.png",
|
|
||||||
"authConfig": [
|
|
||||||
{
|
|
||||||
"authField": "ZAPIER_NLA_API_KEY",
|
|
||||||
"label": "Zapier API Key",
|
|
||||||
"description": "You can use Zapier with your API Key from Zapier."
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"name": "Azure AI Search",
|
"name": "Azure AI Search",
|
||||||
"pluginKey": "azure-ai-search",
|
"pluginKey": "azure-ai-search",
|
||||||
|
|
@ -187,15 +148,21 @@
|
||||||
{
|
{
|
||||||
"authField": "AZURE_AI_SEARCH_API_KEY",
|
"authField": "AZURE_AI_SEARCH_API_KEY",
|
||||||
"label": "Azure AI Search API Key",
|
"label": "Azure AI Search API Key",
|
||||||
"description": "You need to provideq your API Key for Azure AI Search."
|
"description": "You need to provide your API Key for Azure AI Search."
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "CodeBrew",
|
"name": "OpenWeather",
|
||||||
"pluginKey": "CodeBrew",
|
"pluginKey": "open_weather",
|
||||||
"description": "Use 'CodeBrew' to virtually interpret Python, Node, C, C++, Java, C#, PHP, MySQL, Rust or Go code.",
|
"description": "Get weather forecasts and historical data from the OpenWeather API",
|
||||||
"icon": "https://imgur.com/iLE5ceA.png",
|
"icon": "/assets/openweather.png",
|
||||||
"authConfig": []
|
"authConfig": [
|
||||||
|
{
|
||||||
|
"authField": "OPENWEATHER_API_KEY",
|
||||||
|
"label": "OpenWeather API Key",
|
||||||
|
"description": "Sign up at <a href=\"https://home.openweathermap.org/users/sign_up\" target=\"_blank\">OpenWeather</a>, then get your key at <a href=\"https://home.openweathermap.org/api_keys\" target=\"_blank\">API keys</a>."
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
const { z } = require('zod');
|
const { z } = require('zod');
|
||||||
const { StructuredTool } = require('langchain/tools');
|
const { Tool } = require('@langchain/core/tools');
|
||||||
const { SearchClient, AzureKeyCredential } = require('@azure/search-documents');
|
const { SearchClient, AzureKeyCredential } = require('@azure/search-documents');
|
||||||
const { logger } = require('~/config');
|
const { logger } = require('~/config');
|
||||||
|
|
||||||
class AzureAISearch extends StructuredTool {
|
class AzureAISearch extends Tool {
|
||||||
// Constants for default values
|
// Constants for default values
|
||||||
static DEFAULT_API_VERSION = '2023-11-01';
|
static DEFAULT_API_VERSION = '2023-11-01';
|
||||||
static DEFAULT_QUERY_TYPE = 'simple';
|
static DEFAULT_QUERY_TYPE = 'simple';
|
||||||
|
|
@ -83,7 +83,7 @@ class AzureAISearch extends StructuredTool {
|
||||||
try {
|
try {
|
||||||
const searchOption = {
|
const searchOption = {
|
||||||
queryType: this.queryType,
|
queryType: this.queryType,
|
||||||
top: this.top,
|
top: typeof this.top === 'string' ? Number(this.top) : this.top,
|
||||||
};
|
};
|
||||||
if (this.select) {
|
if (this.select) {
|
||||||
searchOption.select = this.select.split(',');
|
searchOption.select = this.select.split(',');
|
||||||
|
|
|
||||||
|
|
@ -1,23 +0,0 @@
|
||||||
const { StructuredTool } = require('langchain/tools');
|
|
||||||
const { z } = require('zod');
|
|
||||||
|
|
||||||
// proof of concept
|
|
||||||
class ChatTool extends StructuredTool {
|
|
||||||
constructor({ onAgentAction }) {
|
|
||||||
super();
|
|
||||||
this.handleAction = onAgentAction;
|
|
||||||
this.name = 'talk_to_user';
|
|
||||||
this.description =
|
|
||||||
'Use this to chat with the user between your use of other tools/plugins/APIs. You should explain your motive and thought process in a conversational manner, while also analyzing the output of tools/plugins, almost as a self-reflection step to communicate if you\'ve arrived at the correct answer or used the tools/plugins effectively.';
|
|
||||||
this.schema = z.object({
|
|
||||||
message: z.string().describe('Message to the user.'),
|
|
||||||
// next_step: z.string().optional().describe('The next step to take.'),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async _call({ message }) {
|
|
||||||
return `Message to user: ${message}`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = ChatTool;
|
|
||||||
|
|
@ -1,165 +0,0 @@
|
||||||
const { StructuredTool } = require('langchain/tools');
|
|
||||||
const axios = require('axios');
|
|
||||||
const { z } = require('zod');
|
|
||||||
|
|
||||||
const headers = {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
};
|
|
||||||
|
|
||||||
function getServerURL() {
|
|
||||||
const url = process.env.CODESHERPA_SERVER_URL || '';
|
|
||||||
if (!url) {
|
|
||||||
throw new Error('Missing CODESHERPA_SERVER_URL environment variable.');
|
|
||||||
}
|
|
||||||
return url;
|
|
||||||
}
|
|
||||||
|
|
||||||
class RunCode extends StructuredTool {
|
|
||||||
constructor() {
|
|
||||||
super();
|
|
||||||
this.name = 'RunCode';
|
|
||||||
this.description =
|
|
||||||
'Use this plugin to run code with the following parameters\ncode: your code\nlanguage: either Python, Rust, or C++.';
|
|
||||||
this.headers = headers;
|
|
||||||
this.schema = z.object({
|
|
||||||
code: z.string().describe('The code to be executed in the REPL-like environment.'),
|
|
||||||
language: z.string().describe('The programming language of the code to be executed.'),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async _call({ code, language = 'python' }) {
|
|
||||||
// logger.debug('<--------------- Running Code --------------->', { code, language });
|
|
||||||
const response = await axios({
|
|
||||||
url: `${this.url}/repl`,
|
|
||||||
method: 'post',
|
|
||||||
headers: this.headers,
|
|
||||||
data: { code, language },
|
|
||||||
});
|
|
||||||
// logger.debug('<--------------- Sucessfully ran Code --------------->', response.data);
|
|
||||||
return response.data.result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class RunCommand extends StructuredTool {
|
|
||||||
constructor() {
|
|
||||||
super();
|
|
||||||
this.name = 'RunCommand';
|
|
||||||
this.description =
|
|
||||||
'Runs the provided terminal command and returns the output or error message.';
|
|
||||||
this.headers = headers;
|
|
||||||
this.schema = z.object({
|
|
||||||
command: z.string().describe('The terminal command to be executed.'),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async _call({ command }) {
|
|
||||||
const response = await axios({
|
|
||||||
url: `${this.url}/command`,
|
|
||||||
method: 'post',
|
|
||||||
headers: this.headers,
|
|
||||||
data: {
|
|
||||||
command,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
return response.data.result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class CodeSherpa extends StructuredTool {
|
|
||||||
constructor(fields) {
|
|
||||||
super();
|
|
||||||
this.name = 'CodeSherpa';
|
|
||||||
this.url = fields.CODESHERPA_SERVER_URL || getServerURL();
|
|
||||||
// this.description = `A plugin for interactive code execution, and shell command execution.
|
|
||||||
|
|
||||||
// Run code: provide "code" and "language"
|
|
||||||
// - Execute Python code interactively for general programming, tasks, data analysis, visualizations, and more.
|
|
||||||
// - Pre-installed packages: matplotlib, seaborn, pandas, numpy, scipy, openpyxl. If you need to install additional packages, use the \`pip install\` command.
|
|
||||||
// - When a user asks for visualization, save the plot to \`static/images/\` directory, and embed it in the response using \`http://localhost:3333/static/images/\` URL.
|
|
||||||
// - Always save all media files created to \`static/images/\` directory, and embed them in responses using \`http://localhost:3333/static/images/\` URL.
|
|
||||||
|
|
||||||
// Run command: provide "command" only
|
|
||||||
// - Run terminal commands and interact with the filesystem, run scripts, and more.
|
|
||||||
// - Install python packages using \`pip install\` command.
|
|
||||||
// - Always embed media files created or uploaded using \`http://localhost:3333/static/images/\` URL in responses.
|
|
||||||
// - Access user-uploaded files in \`static/uploads/\` directory using \`http://localhost:3333/static/uploads/\` URL.`;
|
|
||||||
this.description = `This plugin allows interactive code and shell command execution.
|
|
||||||
|
|
||||||
To run code, supply "code" and "language". Python has pre-installed packages: matplotlib, seaborn, pandas, numpy, scipy, openpyxl. Additional ones can be installed via pip.
|
|
||||||
|
|
||||||
To run commands, provide "command" only. This allows interaction with the filesystem, script execution, and package installation using pip. Created or uploaded media files are embedded in responses using a specific URL.`;
|
|
||||||
this.schema = z.object({
|
|
||||||
code: z
|
|
||||||
.string()
|
|
||||||
.optional()
|
|
||||||
.describe(
|
|
||||||
`The code to be executed in the REPL-like environment. You must save all media files created to \`${this.url}/static/images/\` and embed them in responses with markdown`,
|
|
||||||
),
|
|
||||||
language: z
|
|
||||||
.string()
|
|
||||||
.optional()
|
|
||||||
.describe(
|
|
||||||
'The programming language of the code to be executed, you must also include code.',
|
|
||||||
),
|
|
||||||
command: z
|
|
||||||
.string()
|
|
||||||
.optional()
|
|
||||||
.describe(
|
|
||||||
'The terminal command to be executed. Only provide this if you want to run a command instead of code.',
|
|
||||||
),
|
|
||||||
});
|
|
||||||
|
|
||||||
this.RunCode = new RunCode({ url: this.url });
|
|
||||||
this.RunCommand = new RunCommand({ url: this.url });
|
|
||||||
this.runCode = this.RunCode._call.bind(this);
|
|
||||||
this.runCommand = this.RunCommand._call.bind(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
async _call({ code, language, command }) {
|
|
||||||
if (code?.length > 0) {
|
|
||||||
return await this.runCode({ code, language });
|
|
||||||
} else if (command) {
|
|
||||||
return await this.runCommand({ command });
|
|
||||||
} else {
|
|
||||||
return 'Invalid parameters provided.';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* TODO: support file upload */
|
|
||||||
// class UploadFile extends StructuredTool {
|
|
||||||
// constructor(fields) {
|
|
||||||
// super();
|
|
||||||
// this.name = 'UploadFile';
|
|
||||||
// this.url = fields.CODESHERPA_SERVER_URL || getServerURL();
|
|
||||||
// this.description = 'Endpoint to upload a file.';
|
|
||||||
// this.headers = headers;
|
|
||||||
// this.schema = z.object({
|
|
||||||
// file: z.string().describe('The file to be uploaded.'),
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
|
|
||||||
// async _call(data) {
|
|
||||||
// const formData = new FormData();
|
|
||||||
// formData.append('file', fs.createReadStream(data.file));
|
|
||||||
|
|
||||||
// const response = await axios({
|
|
||||||
// url: `${this.url}/upload`,
|
|
||||||
// method: 'post',
|
|
||||||
// headers: {
|
|
||||||
// ...this.headers,
|
|
||||||
// 'Content-Type': `multipart/form-data; boundary=${formData._boundary}`,
|
|
||||||
// },
|
|
||||||
// data: formData,
|
|
||||||
// });
|
|
||||||
// return response.data;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// module.exports = [
|
|
||||||
// RunCode,
|
|
||||||
// RunCommand,
|
|
||||||
// // UploadFile
|
|
||||||
// ];
|
|
||||||
|
|
||||||
module.exports = CodeSherpa;
|
|
||||||
|
|
@ -1,121 +0,0 @@
|
||||||
const { StructuredTool } = require('langchain/tools');
|
|
||||||
const axios = require('axios');
|
|
||||||
const { z } = require('zod');
|
|
||||||
|
|
||||||
function getServerURL() {
|
|
||||||
const url = process.env.CODESHERPA_SERVER_URL || '';
|
|
||||||
if (!url) {
|
|
||||||
throw new Error('Missing CODESHERPA_SERVER_URL environment variable.');
|
|
||||||
}
|
|
||||||
return url;
|
|
||||||
}
|
|
||||||
|
|
||||||
const headers = {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
};
|
|
||||||
|
|
||||||
class RunCode extends StructuredTool {
|
|
||||||
constructor(fields) {
|
|
||||||
super();
|
|
||||||
this.name = 'RunCode';
|
|
||||||
this.url = fields.CODESHERPA_SERVER_URL || getServerURL();
|
|
||||||
this.description_for_model = `// A plugin for interactive code execution
|
|
||||||
// Guidelines:
|
|
||||||
// Always provide code and language as such: {{"code": "print('Hello World!')", "language": "python"}}
|
|
||||||
// Execute Python code interactively for general programming, tasks, data analysis, visualizations, and more.
|
|
||||||
// Pre-installed packages: matplotlib, seaborn, pandas, numpy, scipy, openpyxl.If you need to install additional packages, use the \`pip install\` command.
|
|
||||||
// When a user asks for visualization, save the plot to \`static/images/\` directory, and embed it in the response using \`${this.url}/static/images/\` URL.
|
|
||||||
// Always save alls media files created to \`static/images/\` directory, and embed them in responses using \`${this.url}/static/images/\` URL.
|
|
||||||
// Always embed media files created or uploaded using \`${this.url}/static/images/\` URL in responses.
|
|
||||||
// Access user-uploaded files in\`static/uploads/\` directory using \`${this.url}/static/uploads/\` URL.
|
|
||||||
// Remember to save any plots/images created, so you can embed it in the response, to \`static/images/\` directory, and embed them as instructed before.`;
|
|
||||||
this.description =
|
|
||||||
'This plugin allows interactive code execution. Follow the guidelines to get the best results.';
|
|
||||||
this.headers = headers;
|
|
||||||
this.schema = z.object({
|
|
||||||
code: z.string().optional().describe('The code to be executed in the REPL-like environment.'),
|
|
||||||
language: z
|
|
||||||
.string()
|
|
||||||
.optional()
|
|
||||||
.describe('The programming language of the code to be executed.'),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async _call({ code, language = 'python' }) {
|
|
||||||
// logger.debug('<--------------- Running Code --------------->', { code, language });
|
|
||||||
const response = await axios({
|
|
||||||
url: `${this.url}/repl`,
|
|
||||||
method: 'post',
|
|
||||||
headers: this.headers,
|
|
||||||
data: { code, language },
|
|
||||||
});
|
|
||||||
// logger.debug('<--------------- Sucessfully ran Code --------------->', response.data);
|
|
||||||
return response.data.result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class RunCommand extends StructuredTool {
|
|
||||||
constructor(fields) {
|
|
||||||
super();
|
|
||||||
this.name = 'RunCommand';
|
|
||||||
this.url = fields.CODESHERPA_SERVER_URL || getServerURL();
|
|
||||||
this.description_for_model = `// Run terminal commands and interact with the filesystem, run scripts, and more.
|
|
||||||
// Guidelines:
|
|
||||||
// Always provide command as such: {{"command": "ls -l"}}
|
|
||||||
// Install python packages using \`pip install\` command.
|
|
||||||
// Always embed media files created or uploaded using \`${this.url}/static/images/\` URL in responses.
|
|
||||||
// Access user-uploaded files in\`static/uploads/\` directory using \`${this.url}/static/uploads/\` URL.`;
|
|
||||||
this.description =
|
|
||||||
'A plugin for interactive shell command execution. Follow the guidelines to get the best results.';
|
|
||||||
this.headers = headers;
|
|
||||||
this.schema = z.object({
|
|
||||||
command: z.string().describe('The terminal command to be executed.'),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async _call(data) {
|
|
||||||
const response = await axios({
|
|
||||||
url: `${this.url}/command`,
|
|
||||||
method: 'post',
|
|
||||||
headers: this.headers,
|
|
||||||
data,
|
|
||||||
});
|
|
||||||
return response.data.result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* TODO: support file upload */
|
|
||||||
// class UploadFile extends StructuredTool {
|
|
||||||
// constructor(fields) {
|
|
||||||
// super();
|
|
||||||
// this.name = 'UploadFile';
|
|
||||||
// this.url = fields.CODESHERPA_SERVER_URL || getServerURL();
|
|
||||||
// this.description = 'Endpoint to upload a file.';
|
|
||||||
// this.headers = headers;
|
|
||||||
// this.schema = z.object({
|
|
||||||
// file: z.string().describe('The file to be uploaded.'),
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
|
|
||||||
// async _call(data) {
|
|
||||||
// const formData = new FormData();
|
|
||||||
// formData.append('file', fs.createReadStream(data.file));
|
|
||||||
|
|
||||||
// const response = await axios({
|
|
||||||
// url: `${this.url}/upload`,
|
|
||||||
// method: 'post',
|
|
||||||
// headers: {
|
|
||||||
// ...this.headers,
|
|
||||||
// 'Content-Type': `multipart/form-data; boundary=${formData._boundary}`,
|
|
||||||
// },
|
|
||||||
// data: formData,
|
|
||||||
// });
|
|
||||||
// return response.data;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
module.exports = [
|
|
||||||
RunCode,
|
|
||||||
RunCommand,
|
|
||||||
// UploadFile
|
|
||||||
];
|
|
||||||
|
|
@ -2,7 +2,7 @@ const { z } = require('zod');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const OpenAI = require('openai');
|
const OpenAI = require('openai');
|
||||||
const { v4: uuidv4 } = require('uuid');
|
const { v4: uuidv4 } = require('uuid');
|
||||||
const { Tool } = require('langchain/tools');
|
const { Tool } = require('@langchain/core/tools');
|
||||||
const { HttpsProxyAgent } = require('https-proxy-agent');
|
const { HttpsProxyAgent } = require('https-proxy-agent');
|
||||||
const { FileContext } = require('librechat-data-provider');
|
const { FileContext } = require('librechat-data-provider');
|
||||||
const { getImageBasename } = require('~/server/services/Files/images');
|
const { getImageBasename } = require('~/server/services/Files/images');
|
||||||
|
|
@ -12,14 +12,17 @@ const { logger } = require('~/config');
|
||||||
class DALLE3 extends Tool {
|
class DALLE3 extends Tool {
|
||||||
constructor(fields = {}) {
|
constructor(fields = {}) {
|
||||||
super();
|
super();
|
||||||
/* Used to initialize the Tool without necessary variables. */
|
/** @type {boolean} Used to initialize the Tool without necessary variables. */
|
||||||
this.override = fields.override ?? false;
|
this.override = fields.override ?? false;
|
||||||
/* Necessary for output to contain all image metadata. */
|
/** @type {boolean} Necessary for output to contain all image metadata. */
|
||||||
this.returnMetadata = fields.returnMetadata ?? false;
|
this.returnMetadata = fields.returnMetadata ?? false;
|
||||||
|
|
||||||
this.userId = fields.userId;
|
this.userId = fields.userId;
|
||||||
this.fileStrategy = fields.fileStrategy;
|
this.fileStrategy = fields.fileStrategy;
|
||||||
|
/** @type {boolean} */
|
||||||
|
this.isAgent = fields.isAgent;
|
||||||
if (fields.processFileURL) {
|
if (fields.processFileURL) {
|
||||||
|
/** @type {processFileURL} Necessary for output to contain all image metadata. */
|
||||||
this.processFileURL = fields.processFileURL.bind(this);
|
this.processFileURL = fields.processFileURL.bind(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -43,6 +46,7 @@ class DALLE3 extends Tool {
|
||||||
config.httpAgent = new HttpsProxyAgent(process.env.PROXY);
|
config.httpAgent = new HttpsProxyAgent(process.env.PROXY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @type {OpenAI} */
|
||||||
this.openai = new OpenAI(config);
|
this.openai = new OpenAI(config);
|
||||||
this.name = 'dalle';
|
this.name = 'dalle';
|
||||||
this.description = `Use DALLE to create images from text descriptions.
|
this.description = `Use DALLE to create images from text descriptions.
|
||||||
|
|
@ -106,6 +110,19 @@ class DALLE3 extends Tool {
|
||||||
return ``;
|
return ``;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
returnValue(value) {
|
||||||
|
if (this.isAgent === true && typeof value === 'string') {
|
||||||
|
return [value, {}];
|
||||||
|
} else if (this.isAgent === true && typeof value === 'object') {
|
||||||
|
return [
|
||||||
|
'DALL-E displayed an image. All generated images are already plainly visible, so don\'t repeat the descriptions in detail. Do not list download links as they are available in the UI already. The user may download the images by clicking on them, but do not mention anything about downloading to the user.',
|
||||||
|
value,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
async _call(data) {
|
async _call(data) {
|
||||||
const { prompt, quality = 'standard', size = '1024x1024', style = 'vivid' } = data;
|
const { prompt, quality = 'standard', size = '1024x1024', style = 'vivid' } = data;
|
||||||
if (!prompt) {
|
if (!prompt) {
|
||||||
|
|
@ -124,18 +141,23 @@ class DALLE3 extends Tool {
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error('[DALL-E-3] Problem generating the image:', error);
|
logger.error('[DALL-E-3] Problem generating the image:', error);
|
||||||
return `Something went wrong when trying to generate the image. The DALL-E API may be unavailable:
|
return this
|
||||||
Error Message: ${error.message}`;
|
.returnValue(`Something went wrong when trying to generate the image. The DALL-E API may be unavailable:
|
||||||
|
Error Message: ${error.message}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!resp) {
|
if (!resp) {
|
||||||
return 'Something went wrong when trying to generate the image. The DALL-E API may be unavailable';
|
return this.returnValue(
|
||||||
|
'Something went wrong when trying to generate the image. The DALL-E API may be unavailable',
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const theImageUrl = resp.data[0].url;
|
const theImageUrl = resp.data[0].url;
|
||||||
|
|
||||||
if (!theImageUrl) {
|
if (!theImageUrl) {
|
||||||
return 'No image URL returned from OpenAI API. There may be a problem with the API or your configuration.';
|
return this.returnValue(
|
||||||
|
'No image URL returned from OpenAI API. There may be a problem with the API or your configuration.',
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const imageBasename = getImageBasename(theImageUrl);
|
const imageBasename = getImageBasename(theImageUrl);
|
||||||
|
|
@ -155,22 +177,16 @@ Error Message: ${error.message}`;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const result = await this.processFileURL({
|
const result = await this.processFileURL({
|
||||||
fileStrategy: this.fileStrategy,
|
|
||||||
userId: this.userId,
|
|
||||||
URL: theImageUrl,
|
URL: theImageUrl,
|
||||||
fileName: imageName,
|
|
||||||
basePath: 'images',
|
basePath: 'images',
|
||||||
|
userId: this.userId,
|
||||||
|
fileName: imageName,
|
||||||
|
fileStrategy: this.fileStrategy,
|
||||||
context: FileContext.image_generation,
|
context: FileContext.image_generation,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (this.returnMetadata) {
|
if (this.returnMetadata) {
|
||||||
this.result = {
|
this.result = result;
|
||||||
file_id: result.file_id,
|
|
||||||
filename: result.filename,
|
|
||||||
filepath: result.filepath,
|
|
||||||
height: result.height,
|
|
||||||
width: result.width,
|
|
||||||
};
|
|
||||||
} else {
|
} else {
|
||||||
this.result = this.wrapInMarkdown(result.filepath);
|
this.result = this.wrapInMarkdown(result.filepath);
|
||||||
}
|
}
|
||||||
|
|
@ -179,7 +195,7 @@ Error Message: ${error.message}`;
|
||||||
this.result = `Failed to save the image locally. ${error.message}`;
|
this.result = `Failed to save the image locally. ${error.message}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.result;
|
return this.returnValue(this.result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,155 +0,0 @@
|
||||||
const { z } = require('zod');
|
|
||||||
const axios = require('axios');
|
|
||||||
const { StructuredTool } = require('langchain/tools');
|
|
||||||
const { PromptTemplate } = require('langchain/prompts');
|
|
||||||
// const { ChatOpenAI } = require('langchain/chat_models/openai');
|
|
||||||
const { createExtractionChainFromZod } = require('./extractionChain');
|
|
||||||
const { logger } = require('~/config');
|
|
||||||
|
|
||||||
const envs = ['Nodejs', 'Go', 'Bash', 'Rust', 'Python3', 'PHP', 'Java', 'Perl', 'DotNET'];
|
|
||||||
const env = z.enum(envs);
|
|
||||||
|
|
||||||
const template = `Extract the correct environment for the following code.
|
|
||||||
|
|
||||||
It must be one of these values: ${envs.join(', ')}.
|
|
||||||
|
|
||||||
Code:
|
|
||||||
{input}
|
|
||||||
`;
|
|
||||||
|
|
||||||
const prompt = PromptTemplate.fromTemplate(template);
|
|
||||||
|
|
||||||
// const schema = {
|
|
||||||
// type: 'object',
|
|
||||||
// properties: {
|
|
||||||
// env: { type: 'string' },
|
|
||||||
// },
|
|
||||||
// required: ['env'],
|
|
||||||
// };
|
|
||||||
|
|
||||||
const zodSchema = z.object({
|
|
||||||
env: z.string(),
|
|
||||||
});
|
|
||||||
|
|
||||||
async function extractEnvFromCode(code, model) {
|
|
||||||
// const chatModel = new ChatOpenAI({ openAIApiKey, modelName: 'gpt-4-0613', temperature: 0 });
|
|
||||||
const chain = createExtractionChainFromZod(zodSchema, model, { prompt, verbose: true });
|
|
||||||
const result = await chain.run(code);
|
|
||||||
logger.debug('<--------------- extractEnvFromCode --------------->');
|
|
||||||
logger.debug(result);
|
|
||||||
return result.env;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getServerURL() {
|
|
||||||
const url = process.env.E2B_SERVER_URL || '';
|
|
||||||
if (!url) {
|
|
||||||
throw new Error('Missing E2B_SERVER_URL environment variable.');
|
|
||||||
}
|
|
||||||
return url;
|
|
||||||
}
|
|
||||||
|
|
||||||
const headers = {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'openai-conversation-id': 'some-uuid',
|
|
||||||
};
|
|
||||||
|
|
||||||
class RunCommand extends StructuredTool {
|
|
||||||
constructor(fields) {
|
|
||||||
super();
|
|
||||||
this.name = 'RunCommand';
|
|
||||||
this.url = fields.E2B_SERVER_URL || getServerURL();
|
|
||||||
this.description =
|
|
||||||
'This plugin allows interactive code execution by allowing terminal commands to be ran in the requested environment. To be used in tandem with WriteFile and ReadFile for Code interpretation and execution.';
|
|
||||||
this.headers = headers;
|
|
||||||
this.headers['openai-conversation-id'] = fields.conversationId;
|
|
||||||
this.schema = z.object({
|
|
||||||
command: z.string().describe('Terminal command to run, appropriate to the environment'),
|
|
||||||
workDir: z.string().describe('Working directory to run the command in'),
|
|
||||||
env: env.describe('Environment to run the command in'),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async _call(data) {
|
|
||||||
logger.debug(`<--------------- Running ${data} --------------->`);
|
|
||||||
const response = await axios({
|
|
||||||
url: `${this.url}/commands`,
|
|
||||||
method: 'post',
|
|
||||||
headers: this.headers,
|
|
||||||
data,
|
|
||||||
});
|
|
||||||
return JSON.stringify(response.data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class ReadFile extends StructuredTool {
|
|
||||||
constructor(fields) {
|
|
||||||
super();
|
|
||||||
this.name = 'ReadFile';
|
|
||||||
this.url = fields.E2B_SERVER_URL || getServerURL();
|
|
||||||
this.description =
|
|
||||||
'This plugin allows reading a file from requested environment. To be used in tandem with WriteFile and RunCommand for Code interpretation and execution.';
|
|
||||||
this.headers = headers;
|
|
||||||
this.headers['openai-conversation-id'] = fields.conversationId;
|
|
||||||
this.schema = z.object({
|
|
||||||
path: z.string().describe('Path of the file to read'),
|
|
||||||
env: env.describe('Environment to read the file from'),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async _call(data) {
|
|
||||||
logger.debug(`<--------------- Reading ${data} --------------->`);
|
|
||||||
const response = await axios.get(`${this.url}/files`, { params: data, headers: this.headers });
|
|
||||||
return response.data;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class WriteFile extends StructuredTool {
|
|
||||||
constructor(fields) {
|
|
||||||
super();
|
|
||||||
this.name = 'WriteFile';
|
|
||||||
this.url = fields.E2B_SERVER_URL || getServerURL();
|
|
||||||
this.model = fields.model;
|
|
||||||
this.description =
|
|
||||||
'This plugin allows interactive code execution by first writing to a file in the requested environment. To be used in tandem with ReadFile and RunCommand for Code interpretation and execution.';
|
|
||||||
this.headers = headers;
|
|
||||||
this.headers['openai-conversation-id'] = fields.conversationId;
|
|
||||||
this.schema = z.object({
|
|
||||||
path: z.string().describe('Path to write the file to'),
|
|
||||||
content: z.string().describe('Content to write in the file. Usually code.'),
|
|
||||||
env: env.describe('Environment to write the file to'),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async _call(data) {
|
|
||||||
let { env, path, content } = data;
|
|
||||||
logger.debug(`<--------------- environment ${env} typeof ${typeof env}--------------->`);
|
|
||||||
if (env && !envs.includes(env)) {
|
|
||||||
logger.debug(`<--------------- Invalid environment ${env} --------------->`);
|
|
||||||
env = await extractEnvFromCode(content, this.model);
|
|
||||||
} else if (!env) {
|
|
||||||
logger.debug('<--------------- Undefined environment --------------->');
|
|
||||||
env = await extractEnvFromCode(content, this.model);
|
|
||||||
}
|
|
||||||
|
|
||||||
const payload = {
|
|
||||||
params: {
|
|
||||||
path,
|
|
||||||
env,
|
|
||||||
},
|
|
||||||
data: {
|
|
||||||
content,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
logger.debug('Writing to file', JSON.stringify(payload));
|
|
||||||
|
|
||||||
await axios({
|
|
||||||
url: `${this.url}/files`,
|
|
||||||
method: 'put',
|
|
||||||
headers: this.headers,
|
|
||||||
...payload,
|
|
||||||
});
|
|
||||||
return `Successfully written to ${path} in ${env}`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = [RunCommand, ReadFile, WriteFile];
|
|
||||||
|
|
@ -4,17 +4,24 @@ const { getEnvironmentVariable } = require('@langchain/core/utils/env');
|
||||||
|
|
||||||
class GoogleSearchResults extends Tool {
|
class GoogleSearchResults extends Tool {
|
||||||
static lc_name() {
|
static lc_name() {
|
||||||
return 'GoogleSearchResults';
|
return 'google';
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(fields = {}) {
|
constructor(fields = {}) {
|
||||||
super(fields);
|
super(fields);
|
||||||
this.envVarApiKey = 'GOOGLE_API_KEY';
|
this.name = 'google';
|
||||||
|
this.envVarApiKey = 'GOOGLE_SEARCH_API_KEY';
|
||||||
this.envVarSearchEngineId = 'GOOGLE_CSE_ID';
|
this.envVarSearchEngineId = 'GOOGLE_CSE_ID';
|
||||||
this.override = fields.override ?? false;
|
this.override = fields.override ?? false;
|
||||||
this.apiKey = fields.apiKey ?? getEnvironmentVariable(this.envVarApiKey);
|
this.apiKey = fields[this.envVarApiKey] ?? getEnvironmentVariable(this.envVarApiKey);
|
||||||
this.searchEngineId =
|
this.searchEngineId =
|
||||||
fields.searchEngineId ?? getEnvironmentVariable(this.envVarSearchEngineId);
|
fields[this.envVarSearchEngineId] ?? getEnvironmentVariable(this.envVarSearchEngineId);
|
||||||
|
|
||||||
|
if (!this.override && (!this.apiKey || !this.searchEngineId)) {
|
||||||
|
throw new Error(
|
||||||
|
`Missing ${this.envVarApiKey} or ${this.envVarSearchEngineId} environment variable.`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
this.kwargs = fields?.kwargs ?? {};
|
this.kwargs = fields?.kwargs ?? {};
|
||||||
this.name = 'google';
|
this.name = 'google';
|
||||||
|
|
|
||||||
317
api/app/clients/tools/structured/OpenWeather.js
Normal file
317
api/app/clients/tools/structured/OpenWeather.js
Normal file
|
|
@ -0,0 +1,317 @@
|
||||||
|
const { Tool } = require('@langchain/core/tools');
|
||||||
|
const { z } = require('zod');
|
||||||
|
const { getEnvironmentVariable } = require('@langchain/core/utils/env');
|
||||||
|
const fetch = require('node-fetch');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Map user-friendly units to OpenWeather units.
|
||||||
|
* Defaults to Celsius if not specified.
|
||||||
|
*/
|
||||||
|
function mapUnitsToOpenWeather(unit) {
|
||||||
|
if (!unit) {
|
||||||
|
return 'metric';
|
||||||
|
} // Default to Celsius
|
||||||
|
switch (unit) {
|
||||||
|
case 'Celsius':
|
||||||
|
return 'metric';
|
||||||
|
case 'Kelvin':
|
||||||
|
return 'standard';
|
||||||
|
case 'Fahrenheit':
|
||||||
|
return 'imperial';
|
||||||
|
default:
|
||||||
|
return 'metric'; // fallback
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Recursively round temperature fields in the API response.
|
||||||
|
*/
|
||||||
|
function roundTemperatures(obj) {
|
||||||
|
const tempKeys = new Set([
|
||||||
|
'temp',
|
||||||
|
'feels_like',
|
||||||
|
'dew_point',
|
||||||
|
'day',
|
||||||
|
'min',
|
||||||
|
'max',
|
||||||
|
'night',
|
||||||
|
'eve',
|
||||||
|
'morn',
|
||||||
|
'afternoon',
|
||||||
|
'morning',
|
||||||
|
'evening',
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (Array.isArray(obj)) {
|
||||||
|
return obj.map((item) => roundTemperatures(item));
|
||||||
|
} else if (obj && typeof obj === 'object') {
|
||||||
|
for (const key of Object.keys(obj)) {
|
||||||
|
const value = obj[key];
|
||||||
|
if (value && typeof value === 'object') {
|
||||||
|
obj[key] = roundTemperatures(value);
|
||||||
|
} else if (typeof value === 'number' && tempKeys.has(key)) {
|
||||||
|
obj[key] = Math.round(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
class OpenWeather extends Tool {
|
||||||
|
name = 'open_weather';
|
||||||
|
description =
|
||||||
|
'Provides weather data from OpenWeather One Call API 3.0. ' +
|
||||||
|
'Actions: help, current_forecast, timestamp, daily_aggregation, overview. ' +
|
||||||
|
'If lat/lon not provided, specify "city" for geocoding. ' +
|
||||||
|
'Units: "Celsius", "Kelvin", or "Fahrenheit" (default: Celsius). ' +
|
||||||
|
'For timestamp action, use "date" in YYYY-MM-DD format.';
|
||||||
|
|
||||||
|
schema = z.object({
|
||||||
|
action: z.enum(['help', 'current_forecast', 'timestamp', 'daily_aggregation', 'overview']),
|
||||||
|
city: z.string().optional(),
|
||||||
|
lat: z.number().optional(),
|
||||||
|
lon: z.number().optional(),
|
||||||
|
exclude: z.string().optional(),
|
||||||
|
units: z.enum(['Celsius', 'Kelvin', 'Fahrenheit']).optional(),
|
||||||
|
lang: z.string().optional(),
|
||||||
|
date: z.string().optional(), // For timestamp and daily_aggregation
|
||||||
|
tz: z.string().optional(),
|
||||||
|
});
|
||||||
|
|
||||||
|
constructor(fields = {}) {
|
||||||
|
super();
|
||||||
|
this.envVar = 'OPENWEATHER_API_KEY';
|
||||||
|
this.override = fields.override ?? false;
|
||||||
|
this.apiKey = fields[this.envVar] ?? this.getApiKey();
|
||||||
|
}
|
||||||
|
|
||||||
|
getApiKey() {
|
||||||
|
const key = getEnvironmentVariable(this.envVar);
|
||||||
|
if (!key && !this.override) {
|
||||||
|
throw new Error(`Missing ${this.envVar} environment variable.`);
|
||||||
|
}
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
async geocodeCity(city) {
|
||||||
|
const geocodeUrl = `https://api.openweathermap.org/geo/1.0/direct?q=${encodeURIComponent(
|
||||||
|
city,
|
||||||
|
)}&limit=1&appid=${this.apiKey}`;
|
||||||
|
const res = await fetch(geocodeUrl);
|
||||||
|
const data = await res.json();
|
||||||
|
if (!res.ok || !Array.isArray(data) || data.length === 0) {
|
||||||
|
throw new Error(`Could not find coordinates for city: ${city}`);
|
||||||
|
}
|
||||||
|
return { lat: data[0].lat, lon: data[0].lon };
|
||||||
|
}
|
||||||
|
|
||||||
|
convertDateToUnix(dateStr) {
|
||||||
|
const parts = dateStr.split('-');
|
||||||
|
if (parts.length !== 3) {
|
||||||
|
throw new Error('Invalid date format. Expected YYYY-MM-DD.');
|
||||||
|
}
|
||||||
|
const year = parseInt(parts[0], 10);
|
||||||
|
const month = parseInt(parts[1], 10);
|
||||||
|
const day = parseInt(parts[2], 10);
|
||||||
|
if (isNaN(year) || isNaN(month) || isNaN(day)) {
|
||||||
|
throw new Error('Invalid date format. Expected YYYY-MM-DD with valid numbers.');
|
||||||
|
}
|
||||||
|
|
||||||
|
const dateObj = new Date(Date.UTC(year, month - 1, day, 0, 0, 0));
|
||||||
|
if (isNaN(dateObj.getTime())) {
|
||||||
|
throw new Error('Invalid date provided. Cannot parse into a valid date.');
|
||||||
|
}
|
||||||
|
|
||||||
|
return Math.floor(dateObj.getTime() / 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
async _call(args) {
|
||||||
|
try {
|
||||||
|
const { action, city, lat, lon, exclude, units, lang, date, tz } = args;
|
||||||
|
const owmUnits = mapUnitsToOpenWeather(units);
|
||||||
|
|
||||||
|
if (action === 'help') {
|
||||||
|
return JSON.stringify(
|
||||||
|
{
|
||||||
|
title: 'OpenWeather One Call API 3.0 Help',
|
||||||
|
description: 'Guidance on using the OpenWeather One Call API 3.0.',
|
||||||
|
endpoints: {
|
||||||
|
current_and_forecast: {
|
||||||
|
endpoint: 'data/3.0/onecall',
|
||||||
|
data_provided: [
|
||||||
|
'Current weather',
|
||||||
|
'Minute forecast (1h)',
|
||||||
|
'Hourly forecast (48h)',
|
||||||
|
'Daily forecast (8 days)',
|
||||||
|
'Government weather alerts',
|
||||||
|
],
|
||||||
|
required_params: [['lat', 'lon'], ['city']],
|
||||||
|
optional_params: ['exclude', 'units (Celsius/Kelvin/Fahrenheit)', 'lang'],
|
||||||
|
usage_example: {
|
||||||
|
city: 'Knoxville, Tennessee',
|
||||||
|
units: 'Fahrenheit',
|
||||||
|
lang: 'en',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
weather_for_timestamp: {
|
||||||
|
endpoint: 'data/3.0/onecall/timemachine',
|
||||||
|
data_provided: [
|
||||||
|
'Historical weather (since 1979-01-01)',
|
||||||
|
'Future forecast up to 4 days ahead',
|
||||||
|
],
|
||||||
|
required_params: [
|
||||||
|
['lat', 'lon', 'date (YYYY-MM-DD)'],
|
||||||
|
['city', 'date (YYYY-MM-DD)'],
|
||||||
|
],
|
||||||
|
optional_params: ['units (Celsius/Kelvin/Fahrenheit)', 'lang'],
|
||||||
|
usage_example: {
|
||||||
|
city: 'Knoxville, Tennessee',
|
||||||
|
date: '2020-03-04',
|
||||||
|
units: 'Fahrenheit',
|
||||||
|
lang: 'en',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
daily_aggregation: {
|
||||||
|
endpoint: 'data/3.0/onecall/day_summary',
|
||||||
|
data_provided: [
|
||||||
|
'Aggregated weather data for a specific date (1979-01-02 to 1.5 years ahead)',
|
||||||
|
],
|
||||||
|
required_params: [
|
||||||
|
['lat', 'lon', 'date (YYYY-MM-DD)'],
|
||||||
|
['city', 'date (YYYY-MM-DD)'],
|
||||||
|
],
|
||||||
|
optional_params: ['units (Celsius/Kelvin/Fahrenheit)', 'lang', 'tz'],
|
||||||
|
usage_example: {
|
||||||
|
city: 'Knoxville, Tennessee',
|
||||||
|
date: '2020-03-04',
|
||||||
|
units: 'Celsius',
|
||||||
|
lang: 'en',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
weather_overview: {
|
||||||
|
endpoint: 'data/3.0/onecall/overview',
|
||||||
|
data_provided: ['Human-readable weather summary (today/tomorrow)'],
|
||||||
|
required_params: [['lat', 'lon'], ['city']],
|
||||||
|
optional_params: ['date (YYYY-MM-DD)', 'units (Celsius/Kelvin/Fahrenheit)'],
|
||||||
|
usage_example: {
|
||||||
|
city: 'Knoxville, Tennessee',
|
||||||
|
date: '2024-05-13',
|
||||||
|
units: 'Celsius',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
notes: [
|
||||||
|
'If lat/lon not provided, you can specify a city name and it will be geocoded.',
|
||||||
|
'For the timestamp action, provide a date in YYYY-MM-DD format instead of a Unix timestamp.',
|
||||||
|
'By default, temperatures are returned in Celsius.',
|
||||||
|
'You can specify units as Celsius, Kelvin, or Fahrenheit.',
|
||||||
|
'All temperatures are rounded to the nearest degree.',
|
||||||
|
],
|
||||||
|
errors: [
|
||||||
|
'400: Bad Request (missing/invalid params)',
|
||||||
|
'401: Unauthorized (check API key)',
|
||||||
|
'404: Not Found (no data or city)',
|
||||||
|
'429: Too many requests',
|
||||||
|
'5xx: Internal error',
|
||||||
|
],
|
||||||
|
},
|
||||||
|
null,
|
||||||
|
2,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
let finalLat = lat;
|
||||||
|
let finalLon = lon;
|
||||||
|
|
||||||
|
// If lat/lon not provided but city is given, geocode it
|
||||||
|
if ((finalLat == null || finalLon == null) && city) {
|
||||||
|
const coords = await this.geocodeCity(city);
|
||||||
|
finalLat = coords.lat;
|
||||||
|
finalLon = coords.lon;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (['current_forecast', 'timestamp', 'daily_aggregation', 'overview'].includes(action)) {
|
||||||
|
if (typeof finalLat !== 'number' || typeof finalLon !== 'number') {
|
||||||
|
return 'Error: lat and lon are required and must be numbers for this action (or specify \'city\').';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const baseUrl = 'https://api.openweathermap.org/data/3.0';
|
||||||
|
let endpoint = '';
|
||||||
|
const params = new URLSearchParams({ appid: this.apiKey, units: owmUnits });
|
||||||
|
|
||||||
|
let dt;
|
||||||
|
if (action === 'timestamp') {
|
||||||
|
if (!date) {
|
||||||
|
return 'Error: For timestamp action, a \'date\' in YYYY-MM-DD format is required.';
|
||||||
|
}
|
||||||
|
dt = this.convertDateToUnix(date);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (action === 'daily_aggregation' && !date) {
|
||||||
|
return 'Error: date (YYYY-MM-DD) is required for daily_aggregation action.';
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (action) {
|
||||||
|
case 'current_forecast':
|
||||||
|
endpoint = '/onecall';
|
||||||
|
params.append('lat', String(finalLat));
|
||||||
|
params.append('lon', String(finalLon));
|
||||||
|
if (exclude) {
|
||||||
|
params.append('exclude', exclude);
|
||||||
|
}
|
||||||
|
if (lang) {
|
||||||
|
params.append('lang', lang);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'timestamp':
|
||||||
|
endpoint = '/onecall/timemachine';
|
||||||
|
params.append('lat', String(finalLat));
|
||||||
|
params.append('lon', String(finalLon));
|
||||||
|
params.append('dt', String(dt));
|
||||||
|
if (lang) {
|
||||||
|
params.append('lang', lang);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'daily_aggregation':
|
||||||
|
endpoint = '/onecall/day_summary';
|
||||||
|
params.append('lat', String(finalLat));
|
||||||
|
params.append('lon', String(finalLon));
|
||||||
|
params.append('date', date);
|
||||||
|
if (lang) {
|
||||||
|
params.append('lang', lang);
|
||||||
|
}
|
||||||
|
if (tz) {
|
||||||
|
params.append('tz', tz);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'overview':
|
||||||
|
endpoint = '/onecall/overview';
|
||||||
|
params.append('lat', String(finalLat));
|
||||||
|
params.append('lon', String(finalLon));
|
||||||
|
if (date) {
|
||||||
|
params.append('date', date);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return `Error: Unknown action: ${action}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
const url = `${baseUrl}${endpoint}?${params.toString()}`;
|
||||||
|
const response = await fetch(url);
|
||||||
|
const json = await response.json();
|
||||||
|
if (!response.ok) {
|
||||||
|
return `Error: OpenWeather API request failed with status ${response.status}: ${
|
||||||
|
json.message || JSON.stringify(json)
|
||||||
|
}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
const roundedJson = roundTemperatures(json);
|
||||||
|
return JSON.stringify(roundedJson);
|
||||||
|
} catch (err) {
|
||||||
|
return `Error: ${err.message}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = OpenWeather;
|
||||||
|
|
@ -4,14 +4,27 @@ const { z } = require('zod');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const axios = require('axios');
|
const axios = require('axios');
|
||||||
const sharp = require('sharp');
|
const sharp = require('sharp');
|
||||||
const { StructuredTool } = require('langchain/tools');
|
const { v4: uuidv4 } = require('uuid');
|
||||||
|
const { Tool } = require('@langchain/core/tools');
|
||||||
|
const { FileContext } = require('librechat-data-provider');
|
||||||
|
const paths = require('~/config/paths');
|
||||||
const { logger } = require('~/config');
|
const { logger } = require('~/config');
|
||||||
|
|
||||||
class StableDiffusionAPI extends StructuredTool {
|
class StableDiffusionAPI extends Tool {
|
||||||
constructor(fields) {
|
constructor(fields) {
|
||||||
super();
|
super();
|
||||||
/* Used to initialize the Tool without necessary variables. */
|
/** @type {string} User ID */
|
||||||
|
this.userId = fields.userId;
|
||||||
|
/** @type {Express.Request | undefined} Express Request object, only provided by ToolService */
|
||||||
|
this.req = fields.req;
|
||||||
|
/** @type {boolean} Used to initialize the Tool without necessary variables. */
|
||||||
this.override = fields.override ?? false;
|
this.override = fields.override ?? false;
|
||||||
|
/** @type {boolean} Necessary for output to contain all image metadata. */
|
||||||
|
this.returnMetadata = fields.returnMetadata ?? false;
|
||||||
|
if (fields.uploadImageBuffer) {
|
||||||
|
/** @type {uploadImageBuffer} Necessary for output to contain all image metadata. */
|
||||||
|
this.uploadImageBuffer = fields.uploadImageBuffer.bind(this);
|
||||||
|
}
|
||||||
|
|
||||||
this.name = 'stable-diffusion';
|
this.name = 'stable-diffusion';
|
||||||
this.url = fields.SD_WEBUI_URL || this.getServerURL();
|
this.url = fields.SD_WEBUI_URL || this.getServerURL();
|
||||||
|
|
@ -47,7 +60,7 @@ class StableDiffusionAPI extends StructuredTool {
|
||||||
|
|
||||||
getMarkdownImageUrl(imageName) {
|
getMarkdownImageUrl(imageName) {
|
||||||
const imageUrl = path
|
const imageUrl = path
|
||||||
.join(this.relativeImageUrl, imageName)
|
.join(this.relativePath, this.userId, imageName)
|
||||||
.replace(/\\/g, '/')
|
.replace(/\\/g, '/')
|
||||||
.replace('public/', '');
|
.replace('public/', '');
|
||||||
return ``;
|
return ``;
|
||||||
|
|
@ -67,52 +80,78 @@ class StableDiffusionAPI extends StructuredTool {
|
||||||
const payload = {
|
const payload = {
|
||||||
prompt,
|
prompt,
|
||||||
negative_prompt,
|
negative_prompt,
|
||||||
sampler_index: 'DPM++ 2M Karras',
|
|
||||||
cfg_scale: 4.5,
|
cfg_scale: 4.5,
|
||||||
steps: 22,
|
steps: 22,
|
||||||
width: 1024,
|
width: 1024,
|
||||||
height: 1024,
|
height: 1024,
|
||||||
};
|
};
|
||||||
const response = await axios.post(`${url}/sdapi/v1/txt2img`, payload);
|
let generationResponse;
|
||||||
const image = response.data.images[0];
|
try {
|
||||||
const pngPayload = { image: `data:image/png;base64,${image}` };
|
generationResponse = await axios.post(`${url}/sdapi/v1/txt2img`, payload);
|
||||||
const response2 = await axios.post(`${url}/sdapi/v1/png-info`, pngPayload);
|
} catch (error) {
|
||||||
const info = response2.data.info;
|
logger.error('[StableDiffusion] Error while generating image:', error);
|
||||||
|
return 'Error making API request.';
|
||||||
|
}
|
||||||
|
const image = generationResponse.data.images[0];
|
||||||
|
|
||||||
// Generate unique name
|
/** @type {{ height: number, width: number, seed: number, infotexts: string[] }} */
|
||||||
const imageName = `${Date.now()}.png`;
|
let info = {};
|
||||||
this.outputPath = path.resolve(
|
try {
|
||||||
__dirname,
|
info = JSON.parse(generationResponse.data.info);
|
||||||
'..',
|
} catch (error) {
|
||||||
'..',
|
logger.error('[StableDiffusion] Error while getting image metadata:', error);
|
||||||
'..',
|
}
|
||||||
'..',
|
|
||||||
'..',
|
|
||||||
'client',
|
|
||||||
'public',
|
|
||||||
'images',
|
|
||||||
);
|
|
||||||
const appRoot = path.resolve(__dirname, '..', '..', '..', '..', '..', 'client');
|
|
||||||
this.relativeImageUrl = path.relative(appRoot, this.outputPath);
|
|
||||||
|
|
||||||
// Check if directory exists, if not create it
|
const file_id = uuidv4();
|
||||||
if (!fs.existsSync(this.outputPath)) {
|
const imageName = `${file_id}.png`;
|
||||||
fs.mkdirSync(this.outputPath, { recursive: true });
|
const { imageOutput: imageOutputPath, clientPath } = paths;
|
||||||
|
const filepath = path.join(imageOutputPath, this.userId, imageName);
|
||||||
|
this.relativePath = path.relative(clientPath, imageOutputPath);
|
||||||
|
|
||||||
|
if (!fs.existsSync(path.join(imageOutputPath, this.userId))) {
|
||||||
|
fs.mkdirSync(path.join(imageOutputPath, this.userId), { recursive: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const buffer = Buffer.from(image.split(',', 1)[0], 'base64');
|
const buffer = Buffer.from(image.split(',', 1)[0], 'base64');
|
||||||
|
if (this.returnMetadata && this.uploadImageBuffer && this.req) {
|
||||||
|
const file = await this.uploadImageBuffer({
|
||||||
|
req: this.req,
|
||||||
|
context: FileContext.image_generation,
|
||||||
|
resize: false,
|
||||||
|
metadata: {
|
||||||
|
buffer,
|
||||||
|
height: info.height,
|
||||||
|
width: info.width,
|
||||||
|
bytes: Buffer.byteLength(buffer),
|
||||||
|
filename: imageName,
|
||||||
|
type: 'image/png',
|
||||||
|
file_id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const generationInfo = info.infotexts[0].split('\n').pop();
|
||||||
|
return {
|
||||||
|
...file,
|
||||||
|
prompt,
|
||||||
|
metadata: {
|
||||||
|
negative_prompt,
|
||||||
|
seed: info.seed,
|
||||||
|
info: generationInfo,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
await sharp(buffer)
|
await sharp(buffer)
|
||||||
.withMetadata({
|
.withMetadata({
|
||||||
iptcpng: {
|
iptcpng: {
|
||||||
parameters: info,
|
parameters: info.infotexts[0],
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
.toFile(this.outputPath + '/' + imageName);
|
.toFile(filepath);
|
||||||
this.result = this.getMarkdownImageUrl(imageName);
|
this.result = this.getMarkdownImageUrl(imageName);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error('[StableDiffusion] Error while saving the image:', error);
|
logger.error('[StableDiffusion] Error while saving the image:', error);
|
||||||
// this.result = theImageUrl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.result;
|
return this.result;
|
||||||
|
|
|
||||||
70
api/app/clients/tools/structured/TavilySearch.js
Normal file
70
api/app/clients/tools/structured/TavilySearch.js
Normal file
|
|
@ -0,0 +1,70 @@
|
||||||
|
const { z } = require('zod');
|
||||||
|
const { tool } = require('@langchain/core/tools');
|
||||||
|
const { getApiKey } = require('./credentials');
|
||||||
|
|
||||||
|
function createTavilySearchTool(fields = {}) {
|
||||||
|
const envVar = 'TAVILY_API_KEY';
|
||||||
|
const override = fields.override ?? false;
|
||||||
|
const apiKey = fields.apiKey ?? getApiKey(envVar, override);
|
||||||
|
const kwargs = fields?.kwargs ?? {};
|
||||||
|
|
||||||
|
return tool(
|
||||||
|
async (input) => {
|
||||||
|
const { query, ...rest } = input;
|
||||||
|
|
||||||
|
const requestBody = {
|
||||||
|
api_key: apiKey,
|
||||||
|
query,
|
||||||
|
...rest,
|
||||||
|
...kwargs,
|
||||||
|
};
|
||||||
|
|
||||||
|
const response = await fetch('https://api.tavily.com/search', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
body: JSON.stringify(requestBody),
|
||||||
|
});
|
||||||
|
|
||||||
|
const json = await response.json();
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`Request failed with status ${response.status}: ${json.error}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return JSON.stringify(json);
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'tavily_search_results_json',
|
||||||
|
description:
|
||||||
|
'A search engine optimized for comprehensive, accurate, and trusted results. Useful for when you need to answer questions about current events.',
|
||||||
|
schema: z.object({
|
||||||
|
query: z.string().min(1).describe('The search query string.'),
|
||||||
|
max_results: z
|
||||||
|
.number()
|
||||||
|
.min(1)
|
||||||
|
.max(10)
|
||||||
|
.optional()
|
||||||
|
.describe('The maximum number of search results to return. Defaults to 5.'),
|
||||||
|
search_depth: z
|
||||||
|
.enum(['basic', 'advanced'])
|
||||||
|
.optional()
|
||||||
|
.describe(
|
||||||
|
'The depth of the search, affecting result quality and response time (`basic` or `advanced`). Default is basic for quick results and advanced for indepth high quality results but longer response time. Advanced calls equals 2 requests.',
|
||||||
|
),
|
||||||
|
include_images: z
|
||||||
|
.boolean()
|
||||||
|
.optional()
|
||||||
|
.describe(
|
||||||
|
'Whether to include a list of query-related images in the response. Default is False.',
|
||||||
|
),
|
||||||
|
include_answer: z
|
||||||
|
.boolean()
|
||||||
|
.optional()
|
||||||
|
.describe('Whether to include answers in the search results. Default is False.'),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = createTavilySearchTool;
|
||||||
|
|
@ -12,7 +12,7 @@ class TavilySearchResults extends Tool {
|
||||||
this.envVar = 'TAVILY_API_KEY';
|
this.envVar = 'TAVILY_API_KEY';
|
||||||
/* Used to initialize the Tool without necessary variables. */
|
/* Used to initialize the Tool without necessary variables. */
|
||||||
this.override = fields.override ?? false;
|
this.override = fields.override ?? false;
|
||||||
this.apiKey = fields.apiKey ?? this.getApiKey();
|
this.apiKey = fields[this.envVar] ?? this.getApiKey();
|
||||||
|
|
||||||
this.kwargs = fields?.kwargs ?? {};
|
this.kwargs = fields?.kwargs ?? {};
|
||||||
this.name = 'tavily_search_results_json';
|
this.name = 'tavily_search_results_json';
|
||||||
|
|
@ -82,7 +82,9 @@ class TavilySearchResults extends Tool {
|
||||||
|
|
||||||
const json = await response.json();
|
const json = await response.json();
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`Request failed with status ${response.status}: ${json.error}`);
|
throw new Error(
|
||||||
|
`Request failed with status ${response.status}: ${json?.detail?.error || json?.error}`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return JSON.stringify(json);
|
return JSON.stringify(json);
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,10 @@
|
||||||
/* eslint-disable no-useless-escape */
|
/* eslint-disable no-useless-escape */
|
||||||
const axios = require('axios');
|
const axios = require('axios');
|
||||||
const { z } = require('zod');
|
const { z } = require('zod');
|
||||||
const { StructuredTool } = require('langchain/tools');
|
const { Tool } = require('@langchain/core/tools');
|
||||||
const { logger } = require('~/config');
|
const { logger } = require('~/config');
|
||||||
|
|
||||||
class WolframAlphaAPI extends StructuredTool {
|
class WolframAlphaAPI extends Tool {
|
||||||
constructor(fields) {
|
constructor(fields) {
|
||||||
super();
|
super();
|
||||||
/* Used to initialize the Tool without necessary variables. */
|
/* Used to initialize the Tool without necessary variables. */
|
||||||
|
|
|
||||||
203
api/app/clients/tools/structured/YouTube.js
Normal file
203
api/app/clients/tools/structured/YouTube.js
Normal file
|
|
@ -0,0 +1,203 @@
|
||||||
|
const { z } = require('zod');
|
||||||
|
const { tool } = require('@langchain/core/tools');
|
||||||
|
const { youtube } = require('@googleapis/youtube');
|
||||||
|
const { YoutubeTranscript } = require('youtube-transcript');
|
||||||
|
const { getApiKey } = require('./credentials');
|
||||||
|
const { logger } = require('~/config');
|
||||||
|
|
||||||
|
function extractVideoId(url) {
|
||||||
|
const rawIdRegex = /^[a-zA-Z0-9_-]{11}$/;
|
||||||
|
if (rawIdRegex.test(url)) {
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
|
const regex = new RegExp(
|
||||||
|
'(?:youtu\\.be/|youtube(?:\\.com)?/(?:' +
|
||||||
|
'(?:watch\\?v=)|(?:embed/)|(?:shorts/)|(?:live/)|(?:v/)|(?:/))?)' +
|
||||||
|
'([a-zA-Z0-9_-]{11})(?:\\S+)?$',
|
||||||
|
);
|
||||||
|
const match = url.match(regex);
|
||||||
|
return match ? match[1] : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseTranscript(transcriptResponse) {
|
||||||
|
if (!Array.isArray(transcriptResponse)) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
return transcriptResponse
|
||||||
|
.map((entry) => entry.text.trim())
|
||||||
|
.filter((text) => text)
|
||||||
|
.join(' ')
|
||||||
|
.replaceAll('&#39;', '\'');
|
||||||
|
}
|
||||||
|
|
||||||
|
function createYouTubeTools(fields = {}) {
|
||||||
|
const envVar = 'YOUTUBE_API_KEY';
|
||||||
|
const override = fields.override ?? false;
|
||||||
|
const apiKey = fields.apiKey ?? fields[envVar] ?? getApiKey(envVar, override);
|
||||||
|
|
||||||
|
const youtubeClient = youtube({
|
||||||
|
version: 'v3',
|
||||||
|
auth: apiKey,
|
||||||
|
});
|
||||||
|
|
||||||
|
const searchTool = tool(
|
||||||
|
async ({ query, maxResults = 5 }) => {
|
||||||
|
const response = await youtubeClient.search.list({
|
||||||
|
part: 'snippet',
|
||||||
|
q: query,
|
||||||
|
type: 'video',
|
||||||
|
maxResults: maxResults || 5,
|
||||||
|
});
|
||||||
|
const result = response.data.items.map((item) => ({
|
||||||
|
title: item.snippet.title,
|
||||||
|
description: item.snippet.description,
|
||||||
|
url: `https://www.youtube.com/watch?v=${item.id.videoId}`,
|
||||||
|
}));
|
||||||
|
return JSON.stringify(result, null, 2);
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'youtube_search',
|
||||||
|
description: `Search for YouTube videos by keyword or phrase.
|
||||||
|
- Required: query (search terms to find videos)
|
||||||
|
- Optional: maxResults (number of videos to return, 1-50, default: 5)
|
||||||
|
- Returns: List of videos with titles, descriptions, and URLs
|
||||||
|
- Use for: Finding specific videos, exploring content, research
|
||||||
|
Example: query="cooking pasta tutorials" maxResults=3`,
|
||||||
|
schema: z.object({
|
||||||
|
query: z.string().describe('Search query terms'),
|
||||||
|
maxResults: z.number().int().min(1).max(50).optional().describe('Number of results (1-50)'),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
const infoTool = tool(
|
||||||
|
async ({ url }) => {
|
||||||
|
const videoId = extractVideoId(url);
|
||||||
|
if (!videoId) {
|
||||||
|
throw new Error('Invalid YouTube URL or video ID');
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await youtubeClient.videos.list({
|
||||||
|
part: 'snippet,statistics',
|
||||||
|
id: videoId,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.data.items?.length) {
|
||||||
|
throw new Error('Video not found');
|
||||||
|
}
|
||||||
|
const video = response.data.items[0];
|
||||||
|
|
||||||
|
const result = {
|
||||||
|
title: video.snippet.title,
|
||||||
|
description: video.snippet.description,
|
||||||
|
views: video.statistics.viewCount,
|
||||||
|
likes: video.statistics.likeCount,
|
||||||
|
comments: video.statistics.commentCount,
|
||||||
|
};
|
||||||
|
return JSON.stringify(result, null, 2);
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'youtube_info',
|
||||||
|
description: `Get detailed metadata and statistics for a specific YouTube video.
|
||||||
|
- Required: url (full YouTube URL or video ID)
|
||||||
|
- Returns: Video title, description, view count, like count, comment count
|
||||||
|
- Use for: Getting video metrics and basic metadata
|
||||||
|
- DO NOT USE FOR VIDEO SUMMARIES, USE TRANSCRIPTS FOR COMPREHENSIVE ANALYSIS
|
||||||
|
- Accepts both full URLs and video IDs
|
||||||
|
Example: url="https://youtube.com/watch?v=abc123" or url="abc123"`,
|
||||||
|
schema: z.object({
|
||||||
|
url: z.string().describe('YouTube video URL or ID'),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
const commentsTool = tool(
|
||||||
|
async ({ url, maxResults = 10 }) => {
|
||||||
|
const videoId = extractVideoId(url);
|
||||||
|
if (!videoId) {
|
||||||
|
throw new Error('Invalid YouTube URL or video ID');
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await youtubeClient.commentThreads.list({
|
||||||
|
part: 'snippet',
|
||||||
|
videoId,
|
||||||
|
maxResults: maxResults || 10,
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = response.data.items.map((item) => ({
|
||||||
|
author: item.snippet.topLevelComment.snippet.authorDisplayName,
|
||||||
|
text: item.snippet.topLevelComment.snippet.textDisplay,
|
||||||
|
likes: item.snippet.topLevelComment.snippet.likeCount,
|
||||||
|
}));
|
||||||
|
return JSON.stringify(result, null, 2);
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'youtube_comments',
|
||||||
|
description: `Retrieve top-level comments from a YouTube video.
|
||||||
|
- Required: url (full YouTube URL or video ID)
|
||||||
|
- Optional: maxResults (number of comments, 1-50, default: 10)
|
||||||
|
- Returns: Comment text, author names, like counts
|
||||||
|
- Use for: Sentiment analysis, audience feedback, engagement review
|
||||||
|
Example: url="abc123" maxResults=20`,
|
||||||
|
schema: z.object({
|
||||||
|
url: z.string().describe('YouTube video URL or ID'),
|
||||||
|
maxResults: z
|
||||||
|
.number()
|
||||||
|
.int()
|
||||||
|
.min(1)
|
||||||
|
.max(50)
|
||||||
|
.optional()
|
||||||
|
.describe('Number of comments to retrieve'),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
const transcriptTool = tool(
|
||||||
|
async ({ url }) => {
|
||||||
|
const videoId = extractVideoId(url);
|
||||||
|
if (!videoId) {
|
||||||
|
throw new Error('Invalid YouTube URL or video ID');
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
try {
|
||||||
|
const transcript = await YoutubeTranscript.fetchTranscript(videoId, { lang: 'en' });
|
||||||
|
return parseTranscript(transcript);
|
||||||
|
} catch (e) {
|
||||||
|
logger.error(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const transcript = await YoutubeTranscript.fetchTranscript(videoId, { lang: 'de' });
|
||||||
|
return parseTranscript(transcript);
|
||||||
|
} catch (e) {
|
||||||
|
logger.error(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
const transcript = await YoutubeTranscript.fetchTranscript(videoId);
|
||||||
|
return parseTranscript(transcript);
|
||||||
|
} catch (error) {
|
||||||
|
throw new Error(`Failed to fetch transcript: ${error.message}`);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'youtube_transcript',
|
||||||
|
description: `Fetch and parse the transcript/captions of a YouTube video.
|
||||||
|
- Required: url (full YouTube URL or video ID)
|
||||||
|
- Returns: Full video transcript as plain text
|
||||||
|
- Use for: Content analysis, summarization, translation reference
|
||||||
|
- This is the "Go-to" tool for analyzing actual video content
|
||||||
|
- Attempts to fetch English first, then German, then any available language
|
||||||
|
Example: url="https://youtube.com/watch?v=abc123"`,
|
||||||
|
schema: z.object({
|
||||||
|
url: z.string().describe('YouTube video URL or ID'),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
return [searchTool, infoTool, commentsTool, transcriptTool];
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = createYouTubeTools;
|
||||||
13
api/app/clients/tools/structured/credentials.js
Normal file
13
api/app/clients/tools/structured/credentials.js
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
const { getEnvironmentVariable } = require('@langchain/core/utils/env');
|
||||||
|
|
||||||
|
function getApiKey(envVar, override) {
|
||||||
|
const key = getEnvironmentVariable(envVar);
|
||||||
|
if (!key && !override) {
|
||||||
|
throw new Error(`Missing ${envVar} environment variable.`);
|
||||||
|
}
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
getApiKey,
|
||||||
|
};
|
||||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue