Update services/client.py
Browse files- services/client.py +68 -71
services/client.py
CHANGED
|
@@ -1,6 +1,4 @@
|
|
| 1 |
from concurrent.futures import ThreadPoolExecutor
|
| 2 |
-
from multiprocessing.pool import ThreadPool
|
| 3 |
-
from pydoc import cli
|
| 4 |
from openai import OpenAI
|
| 5 |
import os
|
| 6 |
from dotenv import load_dotenv
|
|
@@ -13,87 +11,86 @@ BASE_URL = "https://api.novita.ai/v3/openai"
|
|
| 13 |
MODEL_NAME = "meta-llama/llama-3.2-1b-instruct"
|
| 14 |
|
| 15 |
client = OpenAI(
|
| 16 |
-
base_url=
|
| 17 |
api_key=os.environ.get("NOVITA_API_KEY"),
|
| 18 |
)
|
| 19 |
|
| 20 |
-
def generate_commented_code(code:str, max_token:int):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
try:
|
| 22 |
chat_responses = client.chat.completions.create(
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
{
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
"Inline comments explaining non-obvious or critical logic\n\n"
|
| 36 |
-
"Strictly do not do anything else than writing comments. Stricltly do not add or provide alternative code"
|
| 37 |
-
|
| 38 |
-
"Example response:"
|
| 39 |
-
'''
|
| 40 |
-
def add(a, b):
|
| 41 |
-
"""
|
| 42 |
-
Returns the sum of two numbers.
|
| 43 |
-
|
| 44 |
-
Args:
|
| 45 |
-
a (int): The first number.
|
| 46 |
-
b (int): The second number.
|
| 47 |
-
|
| 48 |
-
Returns:
|
| 49 |
-
int: The sum of a and b.
|
| 50 |
-
|
| 51 |
-
Raises:
|
| 52 |
-
TypeError: If either a or b is not an integer.
|
| 53 |
-
ValueError: If either a or b is negative.
|
| 54 |
-
"""
|
| 55 |
-
# Check if both inputs are integers
|
| 56 |
-
if not isinstance(a, int) or not isinstance(b, int):
|
| 57 |
-
raise TypeError("Both inputs must be integers.")
|
| 58 |
-
# Check if both inputs are non-negative
|
| 59 |
-
if a < 0 or b < 0:
|
| 60 |
-
raise ValueError("Both inputs must be non-negative.")
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
{"role": "user", "content": code}
|
| 69 |
]
|
| 70 |
)
|
| 71 |
-
|
| 72 |
-
result = chat_responses.choices[0].message.content
|
| 73 |
-
return result
|
| 74 |
except Exception as ex:
|
| 75 |
-
return f"Error generating
|
| 76 |
|
| 77 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
try:
|
| 79 |
chat_responses = client.chat.completions.create(
|
| 80 |
-
model=
|
| 81 |
temperature=0.7,
|
| 82 |
-
stream=
|
| 83 |
-
max_tokens=
|
| 84 |
-
messages
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from concurrent.futures import ThreadPoolExecutor
|
|
|
|
|
|
|
| 2 |
from openai import OpenAI
|
| 3 |
import os
|
| 4 |
from dotenv import load_dotenv
|
|
|
|
| 11 |
MODEL_NAME = "meta-llama/llama-3.2-1b-instruct"
|
| 12 |
|
| 13 |
client = OpenAI(
|
| 14 |
+
base_url=BASE_URL,
|
| 15 |
api_key=os.environ.get("NOVITA_API_KEY"),
|
| 16 |
)
|
| 17 |
|
| 18 |
+
def generate_commented_code(code: str, max_token: int):
|
| 19 |
+
system_prompt = (
|
| 20 |
+
"You are a senior software engineer skilled in writing precise comments only. "
|
| 21 |
+
"Add clear, concise, and professional comments to the provided code. "
|
| 22 |
+
"Include docstrings for all functions/classes and inline comments for complex logic. "
|
| 23 |
+
"Strictly do not add or provide alternative code."
|
| 24 |
+
)
|
| 25 |
try:
|
| 26 |
chat_responses = client.chat.completions.create(
|
| 27 |
+
model=MODEL_NAME,
|
| 28 |
+
temperature=0.7,
|
| 29 |
+
stream=False,
|
| 30 |
+
max_tokens=max_token,
|
| 31 |
+
messages=[
|
| 32 |
+
{"role": "system", "content": system_prompt},
|
| 33 |
+
{"role": "user", "content": code}
|
| 34 |
+
]
|
| 35 |
+
)
|
| 36 |
+
return chat_responses.choices[0].message.content
|
| 37 |
+
except Exception as ex:
|
| 38 |
+
return f"Error generating comments: {ex}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
def generate_documentation(code: str, max_token: int):
|
| 41 |
+
system_prompt = (
|
| 42 |
+
"You are a senior software engineer skilled in writing structured documentation only. "
|
| 43 |
+
"Produce a strictly formatted JSON object with the following keys: "
|
| 44 |
+
"'name', 'description', 'parameters', 'returns', 'exceptions', and 'examples'."
|
| 45 |
+
)
|
| 46 |
+
try:
|
| 47 |
+
chat_responses = client.chat.completions.create(
|
| 48 |
+
model=MODEL_NAME,
|
| 49 |
+
temperature=0.7,
|
| 50 |
+
stream=False,
|
| 51 |
+
max_tokens=max_token,
|
| 52 |
+
messages=[
|
| 53 |
+
{"role": "system", "content": system_prompt},
|
| 54 |
{"role": "user", "content": code}
|
| 55 |
]
|
| 56 |
)
|
| 57 |
+
return chat_responses.choices[0].message.content
|
|
|
|
|
|
|
| 58 |
except Exception as ex:
|
| 59 |
+
return f"Error generating documentation: {ex}"
|
| 60 |
|
| 61 |
+
def generate_improved_code(code: str, max_token: int):
|
| 62 |
+
system_prompt = (
|
| 63 |
+
"You are a senior software engineer. Analyze the code for possible improvements. "
|
| 64 |
+
"If optimization is possible, provide a cleaner or more efficient version. "
|
| 65 |
+
"Otherwise, write 'Already optimal'. "
|
| 66 |
+
"Always report the time complexity of both the original and improved versions."
|
| 67 |
+
)
|
| 68 |
try:
|
| 69 |
chat_responses = client.chat.completions.create(
|
| 70 |
+
model=MODEL_NAME,
|
| 71 |
temperature=0.7,
|
| 72 |
+
stream=False,
|
| 73 |
+
max_tokens=max_token,
|
| 74 |
+
messages=[
|
| 75 |
+
{"role": "system", "content": system_prompt},
|
| 76 |
+
{"role": "user", "content": code}
|
| 77 |
+
]
|
| 78 |
+
)
|
| 79 |
+
return chat_responses.choices[0].message.content
|
| 80 |
+
except Exception as ex:
|
| 81 |
+
return f"Error generating improved code: {ex}"
|
| 82 |
+
|
| 83 |
+
def generate_code_comments_and_docs(code: str, max_token: int = 1024) -> List:
|
| 84 |
+
try:
|
| 85 |
+
with ThreadPoolExecutor() as executor:
|
| 86 |
+
future1 = executor.submit(generate_commented_code, code, max_token)
|
| 87 |
+
future2 = executor.submit(generate_documentation, code, max_token)
|
| 88 |
+
future3 = executor.submit(generate_improved_code, code, max_token)
|
| 89 |
|
| 90 |
+
commented = future1.result()
|
| 91 |
+
documentation = future2.result()
|
| 92 |
+
improved = future3.result()
|
| 93 |
+
|
| 94 |
+
return [commented, documentation, improved]
|
| 95 |
+
except Exception as ex:
|
| 96 |
+
return [f"Error generating comments: {ex}", "{}", f"Error: {ex}"]
|