From 820a79db6ef5263c4a18d56c7f9e9120af19f378 Mon Sep 17 00:00:00 2001 From: Anindya Maiti Date: Sat, 15 Mar 2025 09:19:06 -0500 Subject: [PATCH] example api usage of chat.secretlab.page endpoints --- api-openwebui/deepseek_classify.py | 75 ++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 api-openwebui/deepseek_classify.py diff --git a/api-openwebui/deepseek_classify.py b/api-openwebui/deepseek_classify.py new file mode 100644 index 0000000..2f9631f --- /dev/null +++ b/api-openwebui/deepseek_classify.py @@ -0,0 +1,75 @@ +import os +import requests +from dotenv import load_dotenv + +def main(): + # 1. Load the environment file with your API key + home_dir = os.path.expanduser("~") + env_file = os.path.join(home_dir, ".secretlab.env") + load_dotenv(env_file) + api_key = os.getenv("SECRETLAB_API_KEY") + + if not api_key: + raise ValueError("SECRETLAB_API_KEY not found in ~/.secretlab.env") + + # 2. The correct endpoint + url = "https://chat.secretlab.page/api/chat/completions" + + # 3. Sentences to classify + sentences = [ + "I love sunny days!", + "This is a bland sandwich.", + "I really hate waiting in long lines.", + "The meeting was okay, nothing special.", + "I am so excited about the upcoming holidays!" + ] + + # 4. For each sentence, send a separate request + for index, sentence in enumerate(sentences, start=1): + # Prepare the request payload + # We use Chat Completion format, but the system message strictly instructs the assistant + # to return only "Positive" or "Negative" with no extra explanation. + payload = { + "model": "deepseek-r1:32b", + "messages": [ + { + "role": "system", + "content": ( + "You are a helpful text classifier. You must respond with only " + "'Positive' or 'Negative' for the given text. No other words or punctuation." + ) + }, + { + "role": "user", + "content": f"Classify this sentence as Positive or Negative:\n{sentence}" + } + ], + "temperature": 0.0 + } + + # Set the headers, including authorization + headers = { + "Content-Type": "application/json", + "Authorization": f"Bearer {api_key}" + } + + # POST the request + response = requests.post(url, headers=headers, json=payload) + + # Check for success + if response.status_code == 200: + completion = response.json() + # Extract the assistant's response text + result_text = completion["choices"][0]["message"]["content"].strip() + + # Print the classification result for each sentence + print(f"Sentence {index}: {sentence}") + print(f"Classification: {result_text}") + print("---------") + else: + print(f"Request for sentence {index} failed with status code: {response.status_code}") + print("Response:", response.text) + print("---------") + +if __name__ == "__main__": + main()