ultralytics-actions 0.0.40__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,45 @@
1
+ # Ultralytics Actions 🚀, AGPL-3.0 license https://ultralytics.com/license
2
+
3
+ import os
4
+ import time
5
+ from typing import Dict, List
6
+
7
+ import requests
8
+
9
+ from actions.utils.common_utils import check_links_in_string
10
+
11
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
12
+ OPENAI_MODEL = os.getenv("OPENAI_MODEL", "gpt-4o-2024-11-20")
13
+
14
+
15
+ def get_completion(
16
+ messages: List[Dict[str, str]],
17
+ check_links: bool = True,
18
+ remove: List[str] = (" @giscus[bot]",), # strings to remove from response
19
+ ) -> str:
20
+ """Generates a completion using OpenAI's API based on input messages."""
21
+ assert OPENAI_API_KEY, "OpenAI API key is required."
22
+ url = "https://api.openai.com/v1/chat/completions"
23
+ headers = {"Authorization": f"Bearer {OPENAI_API_KEY}", "Content-Type": "application/json"}
24
+
25
+ content = ""
26
+ max_retries = 2
27
+ for attempt in range(max_retries + 2): # attempt = [0, 1, 2, 3], 2 random retries before asking for no links
28
+ data = {"model": OPENAI_MODEL, "messages": messages, "seed": int(time.time() * 1000)}
29
+
30
+ r = requests.post(url, headers=headers, json=data)
31
+ r.raise_for_status()
32
+ content = r.json()["choices"][0]["message"]["content"].strip()
33
+ for x in remove:
34
+ content = content.replace(x, "")
35
+ if not check_links or check_links_in_string(content): # if no checks or checks are passing return response
36
+ return content
37
+
38
+ if attempt < max_retries:
39
+ print(f"Attempt {attempt + 1}: Found bad URLs. Retrying with a new random seed.")
40
+ else:
41
+ print("Max retries reached. Updating prompt to exclude links.")
42
+ messages.append({"role": "user", "content": "Please provide a response without any URLs or links in it."})
43
+ check_links = False # automatically accept the last message
44
+
45
+ return content