Skip to content

Commit 37f77b3

Browse files
committed
huggingface chat completion
1 parent a46da8e commit 37f77b3

File tree

4 files changed

+925
-0
lines changed

4 files changed

+925
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import os
2+
3+
import sentry_sdk
4+
from sentry_sdk.integrations.huggingface_hub import HuggingfaceHubIntegration
5+
6+
from huggingface_hub import InferenceClient
7+
8+
9+
TOOLS = [
10+
{
11+
"type": "function",
12+
"function": {
13+
"name": "get_weather",
14+
"description": "Get current temperature for a given location.",
15+
"parameters": {
16+
"type": "object",
17+
"properties": {
18+
"location": {
19+
"type": "string",
20+
"description": "City and country e.g. Paris, France"
21+
}
22+
},
23+
"required": ["location"],
24+
},
25+
}
26+
}
27+
]
28+
29+
30+
def main():
31+
sentry_sdk.init(
32+
dsn=os.getenv("SENTRY_DSN", None),
33+
environment=os.getenv("ENV", "local"),
34+
traces_sample_rate=1.0,
35+
send_default_pii=True,
36+
debug=True,
37+
integrations=[
38+
HuggingfaceHubIntegration(include_prompts=True),
39+
],
40+
)
41+
42+
model = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
43+
provider = "featherless-ai"
44+
45+
client = InferenceClient(
46+
token=os.getenv("HF_TOKEN", None),
47+
provider=provider,
48+
)
49+
50+
response = client.chat_completion(
51+
model=model,
52+
messages=[
53+
{
54+
"role": "user",
55+
"content": "What's the weather like the next 3 days in London, UK?"
56+
}
57+
],
58+
tools=TOOLS,
59+
tool_choice="auto",
60+
frequency_penalty=0.1,
61+
max_tokens=50,
62+
presence_penalty=0.2,
63+
top_p=0.9,
64+
temperature=0.2,
65+
stream=False,
66+
)
67+
print("--------------------------------")
68+
print("Output client.chat_completion:")
69+
print(response)
70+
71+
# OpenAI compatible API
72+
response = client.chat.completions.create(
73+
model=model,
74+
messages=[
75+
{"role": "system", "content": "You are a helpful assistant."},
76+
{"role": "user", "content": "Count to 10"},
77+
],
78+
tools=TOOLS,
79+
tool_choice="auto",
80+
frequency_penalty=0.1,
81+
max_tokens=50,
82+
presence_penalty=0.2,
83+
top_p=0.9,
84+
temperature=0.2,
85+
stream=False,
86+
)
87+
print("--------------------------------")
88+
print("Output client.chat.completions.create:")
89+
print(response)
90+
91+
92+
if __name__ == "__main__":
93+
main()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[project]
2+
name = "test"
3+
version = "0"
4+
requires-python = ">=3.12"
5+
6+
dependencies = [
7+
"huggingface-hub[inference]",
8+
"ipdb>=0.13.13",
9+
"sentry-sdk",
10+
]
11+
12+
[tool.uv.sources]
13+
sentry-sdk = { path = "../../sentry-python", editable = true }
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env bash
2+
3+
# exit on first error
4+
set -euo pipefail
5+
6+
# Install uv if it's not installed
7+
if ! command -v uv &> /dev/null; then
8+
curl -LsSf https://astral.sh/uv/install.sh | sh
9+
fi
10+
11+
# login to huggingface
12+
# uv run hf auth login
13+
14+
# Run the script
15+
uv run python main.py

0 commit comments

Comments
 (0)