File size: 3,588 Bytes
cfa46ba
36d40ce
 
cfa46ba
 
8d7f636
36d40ce
cfa46ba
6d8fc35
31edff4
6d8fc35
 
8bd086d
 
 
 
 
 
 
 
 
 
 
 
6d8fc35
 
81928e3
8bd086d
81928e3
 
 
 
 
 
 
 
 
 
6d8fc35
 
 
 
8bd086d
 
81928e3
 
 
 
 
 
 
1f92f29
8bd086d
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
import os
from dotenv import load_dotenv
load_dotenv()
import openai
import gradio

openai.api_key = os.getenv("OPENAI_API_KEY")

messages = [
    {"role": "system", "content": "You are a Competitive Programming expert that specializes in coding and Competitive Data structures and Algorithms.For any help whether we need help understanding a particular concept, optimizing our code, or solving a specific problem, and you will do your best to assist us.You can perform all these:Algorithmic problem-solving: you can provide explanations, code implementations, and optimizations for various algorithms and data structures commonly used in competitive programming, such as sorting, searching, dynamic programming, graph algorithms, and more.Code review and optimization: If we have a piece of code that we'd like to improve, you can review it and suggest optimizations to make it more efficient, both in terms of time complexity and memory usage.Problem analysis and approach: If we're stuck on a particular problem, you can help you analyze the problem requirements and devise an appropriate approach to solve it.Language-specific help: If we have questions or issues related to a specific programming language commonly used in competitive programming, such as C++, Python, Java, or others, you can provide guidance and solutions specific to that language.Best practices and techniques: you can share tips, tricks, and best practices for competitive programming, such as modular code design, debugging techniques, handling edge cases, and efficient implementation strategies.Interview preparation: If we're preparing for coding interviews, you can help you practice common interview questions and provide guidance on problem-solving techniques frequently encountered in technical interviews."}
]

def count_tokens(messages):
    total_tokens = 0
    for message in messages:
        content = message.get("content", "")
        tokens = len(content.split())
        total_tokens += tokens
    return total_tokens

def truncate_messages(messages, max_tokens):
    while count_tokens(messages) > max_tokens:
        messages.pop(0)

def CustomChatGPT(user_input):
    messages.append({"role": "user", "content": user_input})
    max_tokens = 4096  # Maximum token limit for GPT-3.5 Turbo model
    truncate_messages(messages, max_tokens)
    
    # Truncate the last message if it exceeds the limit
    if count_tokens(messages) > max_tokens:
        last_message = messages[-1]
        last_message_content = last_message.get("content", "")
        last_message_tokens = len(last_message_content.split())
        excess_tokens = count_tokens(messages) - max_tokens
        truncated_content = " ".join(last_message_content.split()[:-excess_tokens])
        last_message["content"] = truncated_content
    
    response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages)
    ChatGPT_reply = response["choices"][0]["message"]["content"]
    messages.append({"role": "assistant", "content": ChatGPT_reply})

    return ChatGPT_reply

demo = gradio.Interface(
    fn=CustomChatGPT,
    inputs="text",
    outputs="text",
    title="CodeGPT",
    description="CodeGPT is a GPT-3.5 Turbo-based coding assistant for general programming jobs, data structures, algorithms, and competitive programming. You can extract text responses to coding-related prompts using this model.Using OpenAI's potent GPT-3.5 Turbo model, CodeGPT was created. We would like to thank OpenAI for their contributions to the machine learning and natural language processing fields."
)

demo.launch()