File size: 6,243 Bytes
c78d99c
 
 
 
 
 
 
 
 
 
fe3f135
c78d99c
 
3ac9eaa
 
c78d99c
 
 
 
3ac9eaa
 
 
c78d99c
 
3ac9eaa
 
c78d99c
 
 
b98550c
 
 
 
 
 
 
 
 
 
 
 
 
 
c78d99c
 
 
 
 
 
 
3ac9eaa
 
 
c78d99c
 
 
3ac9eaa
c78d99c
 
 
c19f5de
 
11e9350
 
 
c19f5de
11e9350
 
c19f5de
11e9350
 
c78d99c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ac9eaa
 
 
c78d99c
 
 
 
 
 
 
c19f5de
 
 
 
 
 
 
 
 
 
 
 
 
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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}",
                },
            ],
        )
        # Extract feedback text from the response
        review = 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):
            review = "\n\n".join(
                [
                    text_block.text
                    for text_block in response.content
                    if hasattr(text_block, "text")
                ]
            )

        return review
    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}",
                },
            ],
        )
        # Check if feedback is a Message object and extract text if necessary
        if hasattr(response, "content") and isinstance(response.content, list):
            # Join the text blocks into a single string with line breaks
            refactor = "\n".join(
                [text_block.text for text_block in response.content if hasattr(text_block, "text")]
            )
        else:
            refactor = response.text if hasattr(response, "text") else str(response)

        # Return the formatted string, ensuring it maintains line breaks
        return refactor.strip()
    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}",
                },
            ],
        )
        code_errors = 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):
            code_errors = "\n\n".join(
                [
                    text_block.text
                    for text_block in response.content
                    if hasattr(text_block, "text")
                ]
            )

        return code_errors
    except Exception as e:
        return "Sorry, an error occurred while removing code errors. Please try again later."