Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Add provider ordering; Add Replicate and Hugging Face models provider [WIP]

Tomas Valenta14c7e83e2415
diff --git a/api-service/models/base.py b/api-service/models/base.py
index 400ecc0a7..243178284 100644
--- a/api-service/models/base.py
+++ b/api-service/models/base.py
@@ -1,16 +1,19 @@
from typing import TypedDict, Dict, Any
from enum import Enum
from langchain.chat_models import ChatOpenAI
-from langchain.llms import Anthropic
+from langchain.llms import Anthropic, HuggingFaceEndpoint
from langchain.schema import BaseLanguageModel
from langchain.callbacks.base import BaseCallbackManager
from .providers.replicate import ReplicateFix
+from .providers.hugging_face import HuggingFaceHubFix
+
class ModelProvider(Enum):
OpenAI = "OpenAI"
Replicate = "Replicate"
Anthropic = "Anthropic"
+ HuggingFace = "HuggingFace"
class ModelConfig(TypedDict):
@@ -47,5 +50,22 @@ def get_model(
verbose=True,
callback_manager=callback_manager,
)
+ case ModelProvider.HuggingFace.value:
+ if config["args"].get("endpoint_url"):
+ return HuggingFaceEndpoint(
+ **config["args"],
+ verbose=True,
+ callback_manager=callback_manager,
+ )
+ elif config["args"].get("repo_id"):
+ return HuggingFaceHubFix(
+ **config["args"],
+ verbose=True,
+ callback_manager=callback_manager,
+ )
+ raise ValueError(
+ f"Missing endpoint_url or repo_id for the HuggingFace integration."
+ )
+
case _:
raise ValueError(f"Provider {config['provider']} no found.")
diff --git a/api-service/models/providers/hugging_face.py b/api-service/models/providers/hugging_face.py
new file mode 100644
index 000000000..2f8b63b50
--- /dev/null
+++ b/api-service/models/providers/hugging_face.py
@@ -0,0 +1,40 @@
+from typing import List, Optional
+from langchain.llms.base import LLM
+from langchain.llms import HuggingFaceEndpoint, HuggingFaceHub
+
+
+class HuggingFaceHubFix(HuggingFaceHub):
+ async def _acall(self, prompt: str, stop: Optional[List[str]] = None) -> str:
+ output = self._call(prompt, stop)
+ if self.callback_manager.is_async:
+ await self.callback_manager.on_llm_new_token(
+ output,
+ verbose=self.verbose,
+ # We explicitly flush the logs in log queue because the calls to this model are not actually async so they block.
+ flush=True,
+ )
+ else:
+ self.callback_manager.on_llm_new_token(
+ output,
+ verbose=self.verbose,
+ )
+
+ return output
+
+class HuggingFaceEndpointFix(HuggingFaceEndpoint):
+ async def _acall(self, prompt: str, stop: Optional[List[str]] = None) -> str:
+ output = self._call(prompt, stop)
+ if self.callback_manager.is_async:
+ await self.callback_manager.on_llm_new_token(
+ output,
+ verbose=self.verbose,
+ # We explicitly flush the logs in log queue because the calls to this model are not actually async so they block.
+ flush=True,
+ )
+ else:
+ self.callback_manager.on_llm_new_token(
+ output,
+ verbose=self.verbose,
+ )
+
+ return output
diff --git a/components/Editor/Sidebar/Model/index.tsx b/components/Editor/Sidebar/Model/index.tsx
index 8e9781401..b8f1fef01 100644
--- a/components/Editor/Sidebar/Model/index.tsx
+++ b/components/Editor/Sidebar/Model/index.tsx
@@ -1,10 +1,19 @@
import Text from 'components/Text'
import { useStateStore } from 'state/StoreProvider'
-import { ModelProvider, modelTemplates } from 'state/model'
+import { ModelProvider, modelTemplates, ProviderTemplate } from 'state/model'
import useModelProviderArgs from 'hooks/useModelProviderArgs'
+import { sortByOrder } from 'utils/sortByOrder'
import ProviderCard from './ProviderCard'
+const modelProviderOrder = sortByOrder<ModelProvider, [string, ProviderTemplate]>(
+ [
+ ModelProvider.OpenAI,
+ ModelProvider.Anthropic,
+ ModelProvider.HuggingFace,
+ ModelProvider.Replicate,
+ ], i => i[0] as ModelProvider)
+
export interface Props { }
function Model({ }: Props) {
@@ -52,7 +61,7 @@ function Model({ }: Props) {
>
{Object.entries(modelTemplates)
.slice()
- .sort((t1, t2) => t1[0].localeCompare(t2[0]))
+ .sort(modelProviderOrder)
.map(([provider, template], i, a) =>
<div
key={provider}
@@ -69,11 +78,12 @@ function Model({ }: Props) {
/>
{i !== a.length - 1 &&
<div className="
- w-full
- border-b
- border-slate-300
- pt-2
- " />}
+ w-full
+ border-b
+ border-slate-300
+ pt-2
+ "
+ />}
</div>
)}
</div>
diff --git a/components/icons/ProviderIcon.tsx b/components/icons/ProviderIcon.tsx
index 9ac99a8e3..00770f044 100644
--- a/components/icons/ProviderIcon.tsx
+++ b/components/icons/ProviderIcon.tsx
@@ -9,6 +9,7 @@ const iconPaths: {
[ModelProvider.OpenAI]: '/open-ai.png',
[ModelProvider.Replicate]: '/replicate.png',
[ModelProvider.Anthropic]: '/anthropic.png',
+ [ModelProvider.HuggingFace]: '/hugging-face.png',
}
export const providerIcons = Object
diff --git a/public/huggingface.png b/public/hugging-face.png
similarity index 100%
rename from public/huggingface.png
rename to public/hugging-face.png
diff --git a/state/model.ts b/state/model.ts
index c2bdc8d5c..fbf078370 100644
--- a/state/model.ts
+++ b/state/model.ts
@@ -6,6 +6,7 @@ export enum ModelProvider {
OpenAI = 'OpenAI',
Replicate = 'Replicate',
Anthropic = 'Anthropic',
+ HuggingFace = 'HuggingFace',
}
export type ArgValue = string | number
@@ -50,6 +51,7 @@ export const modelTemplates: {
[provider in keyof typeof ModelProvider]?: ProviderTemplate
} = {
[ModelProvider.Anthropic]: {
+ link: 'https://anthropic.com',
creds: {
anthropic_api_key: {
label: 'Anthropic API Key',
@@ -145,65 +147,99 @@ export const modelTemplates: {
},
],
},
- // [ModelProvider.Replicate]: {
- // link: 'https://replicate.com',
- // creds: {
- // replicate_api_token: {
- // label: 'Replicate API Key',
- // type: 'string',
- // },
- // },
- // models: [
- // {
- // name: 'Hosted model',
- // args: {
- // model: {
- // placeholder: 'owner-name/model-name:version',
- // editable: true,
- // type: 'string',
- // label: 'Model'
- // },
- // max_length: {
- // type: 'number',
- // editable: true,
- // value: 2500,
- // step: 1,
- // min: 1,
- // },
- // temperature: {
- // editable: true,
- // type: 'number',
- // value: 0.5,
- // min: 0,
- // step: 0.01,
- // },
- // top_p: {
- // editable: true,
- // type: 'number',
- // value: 0.9,
- // min: 0,
- // max: 1,
- // step: 0.01,
- // },
- // top_k: {
- // editable: true,
- // type: 'number',
- // value: 0,
- // min: 0,
- // step: 0.01,
- // },
- // repetition_penalty: {
- // editable: true,
- // type: 'number',
- // value: 1,
- // min: 0,
- // step: 0.01,
- // },
- // },
- // },
- // ],
- // },
+ [ModelProvider.HuggingFace]: {
+ link: 'https://huggingface.co',
+ creds: {
+ huggingfacehub_api_token: {
+ label: 'Hugging Face API Key',
+ type: 'string',
+ },
+ },
+ models: [
+ {
+ name: 'Hub model',
+ args: {
+ repo_id: {
+ editable: true,
+ type: 'string',
+ label: 'Repo ID',
+ placeholder: 'owner-name/repo-name',
+ },
+ },
+ },
+ {
+ name: 'Inference Endpoints model',
+ args: {
+ endpoint_url: {
+ editable: true,
+ type: 'string',
+ label: 'Endpoint URL',
+ placeholder: '...endpoints.huggingface.cloud',
+ },
+ },
+ },
+ ],
+ },
+ [ModelProvider.Replicate]: {
+ link: 'https://replicate.com',
+ creds: {
+ replicate_api_token: {
+ label: 'Replicate API Key',
+ type: 'string',
+ },
+ },
+ models: [
+ {
+ name: 'Hosted model',
+ args: {
+ model: {
+ placeholder: 'owner-name/model-name:version',
+ editable: true,
+ type: 'string',
+ label: 'Model'
+ },
+ max_length: {
+ type: 'number',
+ editable: true,
+ value: 2500,
+ step: 1,
+ min: 1,
+ },
+ temperature: {
+ editable: true,
+ type: 'number',
+ value: 0.5,
+ min: 0,
+ step: 0.01,
+ },
+ top_p: {
+ editable: true,
+ type: 'number',
+ value: 0.9,
+ min: 0,
+ max: 1,
+ step: 0.01,
+ },
+ top_k: {
+ editable: true,
+ type: 'number',
+ value: 0,
+ min: 0,
+ step: 0.01,
+ },
+ repetition_penalty: {
+ editable: true,
+ type: 'number',
+ value: 1,
+ min: 0,
+ step: 0.01,
+ },
+ },
+ },
+ ],
+ },
[ModelProvider.OpenAI]: {
+ link: 'https://openai.com',
creds: {
openai_api_key: {
label: 'OpenAI API Key',
diff --git a/state/prompt.ts b/state/prompt.ts
index d5d8d5651..9d653fb97 100644
--- a/state/prompt.ts
+++ b/state/prompt.ts
@@ -12,6 +12,7 @@ export function getDescription(part: PromptPart) {
return 'Specify how the model should call tools (like WriteJavaScriptCode)'
}
if (part.role === 'user' && part.type === 'prefix') {
+ // https://github.com/janl/mustache.js#templates add docs ref
return 'Describe the context and the start of the task the model should do.\nTo use information from the user prompt (like the steps from "Step-by-step instructions") add them like this: `{{Instructions}}`.\nYou can add Description, RequestBody, Instructions, Method, and Route this way.'
}
diff --git a/utils/sortByOrder.ts b/utils/sortByOrder.ts
new file mode 100644
index 000000000..8bf17f615
--- /dev/null
+++ b/utils/sortByOrder.ts
@@ -0,0 +1,8 @@
+export function sortByOrder<O, I>(order: O[], getID: (item: I) => O) {
+ return (a: I, b: I) => {
+ const aID = getID(a)
+ const bID = getID(b)
+
+ return order.indexOf(aID) - order.indexOf(bID)
+ }
+}