Spaces:
Sleeping
Sleeping
Delete openai_client.py
Browse files- openai_client.py +0 -148
openai_client.py
DELETED
@@ -1,148 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
from dotenv import load_dotenv
|
3 |
-
from openai import OpenAI
|
4 |
-
import anthropic
|
5 |
-
|
6 |
-
# Load environment variables from .env file
|
7 |
-
load_dotenv()
|
8 |
-
|
9 |
-
client = OpenAI(
|
10 |
-
api_key=os.getenv("OPENAI_API_KEY"),
|
11 |
-
base_url=os.getenv("OPENAI_API_BASE"), # Uncomment if using a custom base URL
|
12 |
-
)
|
13 |
-
|
14 |
-
|
15 |
-
# Function to get GPT-4o Mini response
|
16 |
-
def get_code_review_response(prompt, max_tokens=1000):
|
17 |
-
try:
|
18 |
-
response = client.chat.completions.create(
|
19 |
-
model="gpt-4o-mini",
|
20 |
-
messages=[
|
21 |
-
{
|
22 |
-
"role": "system",
|
23 |
-
"content": "You are an AI assistant who helps users in code reviews by deep thinking in points max 5-6 point shortly.",
|
24 |
-
},
|
25 |
-
{"role": "user", "content": prompt},
|
26 |
-
],
|
27 |
-
max_tokens=max_tokens,
|
28 |
-
)
|
29 |
-
return response.choices[0].message.content
|
30 |
-
except Exception as e:
|
31 |
-
return "Sorry, an error occurred while generating your idea. Please try again later."
|
32 |
-
|
33 |
-
|
34 |
-
# Function to refactor code
|
35 |
-
def refactor_code(code_snippet):
|
36 |
-
try:
|
37 |
-
response = client.chat.completions.create(
|
38 |
-
model="gpt-4o-2024-08-06",
|
39 |
-
messages=[
|
40 |
-
{
|
41 |
-
"role": "system",
|
42 |
-
"content": "You are an expert code refactoring assistant.",
|
43 |
-
},
|
44 |
-
{
|
45 |
-
"role": "user",
|
46 |
-
"content": f"Refactor the following code. Do not provide any explanation or comments, just return the refactored code.\n{code_snippet}",
|
47 |
-
},
|
48 |
-
],
|
49 |
-
)
|
50 |
-
refactored_code = response.choices[0].message.content
|
51 |
-
return refactored_code
|
52 |
-
except Exception as e:
|
53 |
-
return "Sorry, an error occurred while refactoring your code. Please try again later."
|
54 |
-
|
55 |
-
|
56 |
-
# Initialize the Anthropic client
|
57 |
-
anthropic_client = anthropic.Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))
|
58 |
-
|
59 |
-
|
60 |
-
# Function to get feedback on code using Anthropic
|
61 |
-
def code_feedback(code_snippet):
|
62 |
-
try:
|
63 |
-
response = anthropic_client.messages.create(
|
64 |
-
model="claude-3-5-sonnet-20240620",
|
65 |
-
max_tokens=1024,
|
66 |
-
messages=[
|
67 |
-
{
|
68 |
-
"role": "user",
|
69 |
-
"content": f"Please provide feedback on the given code, don't refactor the code:\n{code_snippet}",
|
70 |
-
},
|
71 |
-
],
|
72 |
-
)
|
73 |
-
|
74 |
-
# Extract feedback text from the response
|
75 |
-
feedback = response.text if hasattr(response, "text") else str(response)
|
76 |
-
|
77 |
-
# Check if feedback is a Message object and extract text if necessary
|
78 |
-
if hasattr(response, "content") and isinstance(response.content, list):
|
79 |
-
feedback = "\n\n".join(
|
80 |
-
[
|
81 |
-
text_block.text
|
82 |
-
for text_block in response.content
|
83 |
-
if hasattr(text_block, "text")
|
84 |
-
]
|
85 |
-
)
|
86 |
-
|
87 |
-
return feedback
|
88 |
-
|
89 |
-
except Exception as e:
|
90 |
-
return "Sorry, an error occurred while getting feedback on your code. Please try again later."
|
91 |
-
|
92 |
-
|
93 |
-
# Function to suggest best coding practices based on given code
|
94 |
-
def suggest_best_practices(code_snippet):
|
95 |
-
try:
|
96 |
-
response = anthropic_client.messages.create(
|
97 |
-
model="claude-3-5-sonnet-20240620",
|
98 |
-
max_tokens=1024,
|
99 |
-
messages=[
|
100 |
-
{
|
101 |
-
"role": "user",
|
102 |
-
"content": (
|
103 |
-
f"Based on the following code, suggest best practices max 5-6 point shortly"
|
104 |
-
f"for coding patterns that align with industry standards: \n{code_snippet}"
|
105 |
-
),
|
106 |
-
},
|
107 |
-
],
|
108 |
-
)
|
109 |
-
|
110 |
-
# Extract suggestions from the response
|
111 |
-
best_practices = response.text if hasattr(response, "text") else str(response)
|
112 |
-
|
113 |
-
# Check if the feedback is a Message object and extract text if necessary
|
114 |
-
if hasattr(response, "content") and isinstance(response.content, list):
|
115 |
-
best_practices = "\n\n".join(
|
116 |
-
[
|
117 |
-
text_block.text
|
118 |
-
for text_block in response.content
|
119 |
-
if hasattr(text_block, "text")
|
120 |
-
]
|
121 |
-
)
|
122 |
-
|
123 |
-
return best_practices
|
124 |
-
|
125 |
-
except Exception as e:
|
126 |
-
return "Sorry, an error occurred while suggesting best practices. Please try again later."
|
127 |
-
|
128 |
-
|
129 |
-
# Function to remove code errors
|
130 |
-
def remove_code_errors(code_snippet):
|
131 |
-
try:
|
132 |
-
response = client.chat.completions.create(
|
133 |
-
model="gpt-4o-mini",
|
134 |
-
messages=[
|
135 |
-
{
|
136 |
-
"role": "system",
|
137 |
-
"content": "You are an expert in debugging code. Provide concise suggestions to remove errors from the following code snippet.",
|
138 |
-
},
|
139 |
-
{
|
140 |
-
"role": "user",
|
141 |
-
"content": f"Identify and suggest fixes for errors in the following code:\n{code_snippet}",
|
142 |
-
},
|
143 |
-
],
|
144 |
-
)
|
145 |
-
error_removal_suggestions = response.choices[0].message.content
|
146 |
-
return error_removal_suggestions
|
147 |
-
except Exception as e:
|
148 |
-
return "Sorry, an error occurred while removing code errors. Please try again later."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|