|
| 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() |
0 commit comments