Skip to content

Commit af0773b

Browse files
authored
Add hugging face to the quickstart guide (#726)
Adds an example how to use E2B with Hugging Face's serverless inference API to the quickstart guide.
1 parent fd0a097 commit af0773b

File tree

1 file changed

+62
-1
lines changed
  • apps/web/src/app/(docs)/docs/quickstart/connect-llms

1 file changed

+62
-1
lines changed

apps/web/src/app/(docs)/docs/quickstart/connect-llms/page.mdx

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ If the LLM doesn't support tool use, you can, for example, prompt the LLM to out
1313
- [LangChain](#langchain)
1414
- [LlamaIndex](#llamaindex)
1515
- [Ollama](#ollama)
16+
- [Hugging Face](#hugging-face)
1617

1718
---
1819

@@ -503,7 +504,7 @@ from crewai import Agent, Task, Crew, LLM
503504
from e2b_code_interpreter import Sandbox
504505

505506
# Update tool definition using the decorator
506-
@tool("Python Interpreter")
507+
@tool("Python Interpreter")
507508
def execute_python(code: str) -> str:
508509
"""
509510
Execute Python code and return the results.
@@ -741,3 +742,63 @@ with Sandbox() as sandbox:
741742
print(result)
742743
```
743744
</CodeGroup>
745+
746+
---
747+
748+
## Hugging Face
749+
750+
Hugging Face offers support for serverless inference for models on their model hub with [Hugging Face's Inference API](https://huggingface.co/docs/inference-providers/en/index).
751+
752+
<Note>
753+
Note that not every model on Hugging Face has native support for tool use and function calling.
754+
</Note>
755+
756+
<CodeGroup>
757+
```python
758+
from huggingface_hub import InferenceClient
759+
from e2b_code_interpreter import Sandbox
760+
import re
761+
762+
# Not all models are capable of direct tools use - we need to extract the code block manually and prompting the LLM to generate the code.
763+
def match_code_block(llm_response):
764+
pattern = re.compile(r'```python\n(.*?)\n```', re.DOTALL) # Match everything in between ```python and ```
765+
match = pattern.search(llm_response)
766+
if match:
767+
code = match.group(1)
768+
print(code)
769+
return code
770+
return ""
771+
772+
773+
system_prompt = """You are a helpful coding assistant that can execute python code in a Jupyter notebook. You are given tasks to complete and you run Python code to solve them.
774+
Generally, you follow these rules:
775+
- ALWAYS FORMAT YOUR RESPONSE IN MARKDOWN
776+
- ALWAYS RESPOND ONLY WITH CODE IN CODE BLOCK LIKE THIS:
777+
\`\`\`python
778+
{code}
779+
\`\`\`
780+
"""
781+
prompt = "Calculate how many r's are in the word 'strawberry.'"
782+
783+
# Initialize the client
784+
client = InferenceClient(
785+
provider="hf-inference",
786+
api_key="HF_INFERENCE_API_KEY"
787+
)
788+
789+
completion = client.chat.completions.create(
790+
model="Qwen/Qwen3-235B-A22B", # Or use any other model from Hugging Face
791+
messages=[
792+
{"role": "system", "content": system_prompt},
793+
{"role": "user", "content": prompt},
794+
]
795+
)
796+
797+
content = completion.choices[0].message.content
798+
code = match_code_block(content)
799+
800+
with Sandbox() as sandbox:
801+
execution = sandbox.run_code(code)
802+
print(execution)
803+
```
804+
</CodeGroup>

0 commit comments

Comments
 (0)