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