Create services/client.py
Browse files- services/client.py +99 -0
services/client.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 7 |
+
from typing import List
|
| 8 |
+
|
| 9 |
+
load_dotenv()
|
| 10 |
+
|
| 11 |
+
# constants
|
| 12 |
+
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= 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 |
+
model= MODEL_NAME,
|
| 24 |
+
temperature=0.7,
|
| 25 |
+
stream= False,
|
| 26 |
+
max_tokens= max_token,
|
| 27 |
+
messages = [
|
| 28 |
+
{
|
| 29 |
+
"role": "system",
|
| 30 |
+
"content": (
|
| 31 |
+
"You are a senior software engineer skilled in writing precise comments only. Follow these instructions strictly:\n\n"
|
| 32 |
+
"Identify the programming language used and then do following:\n"
|
| 33 |
+
"Add **clear, concise, and professional comments** to the provided code. Include:\n"
|
| 34 |
+
"Docstrings for all functions and classes\n"
|
| 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 |
+
# Use the built-in addition operator to perform the calculation
|
| 63 |
+
# This is more efficient and readable than using a loop or recursive function
|
| 64 |
+
return a + b
|
| 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 comments {ex}"
|
| 76 |
+
|
| 77 |
+
def generate_documentation(code:str, max_token:int):
|
| 78 |
+
try:
|
| 79 |
+
chat_responses = client.chat.completions.create(
|
| 80 |
+
model= MODEL_NAME,
|
| 81 |
+
temperature=0.7,
|
| 82 |
+
stream= False,
|
| 83 |
+
max_tokens= max_token,
|
| 84 |
+
messages = [
|
| 85 |
+
{
|
| 86 |
+
"role": "system",
|
| 87 |
+
"content": (
|
| 88 |
+
"You are a senior software engineer skilled in writing structured documentation only. Follow these instructions strictly:\n\n"
|
| 89 |
+
"Identify the programming language used and then do following:\n"
|
| 90 |
+
"Produce a **strictly formatted JSON** object with:\n"
|
| 91 |
+
"name: Function or class name\n"
|
| 92 |
+
"description: Brief summary of what it does\n"
|
| 93 |
+
"parameters: Dictionary with param name, type, and description\n"
|
| 94 |
+
"returns: Return name, type, and description\n"
|
| 95 |
+
"exceptions (optional): Raised exceptions with reasons\n"
|
| 96 |
+
"examples: Code usage examples with expected output\n\n"
|
| 97 |
+
|
| 98 |
+
"Example response: "
|
| 99 |
+
"""
|