File size: 6,411 Bytes
ef85c96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9778dd
ef85c96
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
from openai import OpenAI
import gradio as gr
import os
from dotenv import load_dotenv
from fetch_all_proteins import *
import json
import requests


load_dotenv()

email = os.getenv("EMAIL")



client = OpenAI(
     api_key=os.environ.get("OPENAI_API_KEY"),
 )

def chatApiCall(messages):
    payload = {
            "messages": messages,
            "web_access": False
        }
    url = "https://open-ai21.p.rapidapi.com/claude3"
    headers = {
            "x-rapidapi-key": os.environ.get("OPENAI_API_KEY"),
            "x-rapidapi-host": "open-ai21.p.rapidapi.com",
            "Content-Type": "application/json"
        }
    # response = requests.post(url, json=payload, headers=headers)
    response = client.chat.completions.create(
        messages=messages,
        model="gpt-4o-mini",
    )
    res_json=response.choices[0].message.content
    print("response",res_json)
    return res_json

## Create a function that determines whether the request relates to a protein or not
def IsProteinRequest(history, message): 
    prompt = """Respond only with true when the conditions below are met otherwise respond only with false. The conditions
     are as follows: 
      1: Within the context of the chat history, the message refers to a specific protein.
       2: A specific protein name is mentioned in the message. GCPR proteins alone does not count. 
        3: If there are no proteins mentioned in the chat history, there should be a specific protein name mentioned in the message.
         4: If there are no chat histories, look at the following message. 
         5: If you detect any generalized requests like "Tell me about proteins" "Tell me about receptors" or any request that has no 
         specific protein mentioned like Rhodopsin or OR51E2, respond with false. 
           The message is as follows: """ + message
    history.append({"role":"user", "content": f"{prompt}"})
    # function_client = OpenAI(
    #     api_key=os.environ.get("OPENAI_API_KEY")
    # )
    # function_client = OpenAI()
    # response = function_client.chat.completions.create(
    #     model="gpt-4o-mini",
    #     messages=history,

    # )
    response=chatApiCall(history)
    # print("prompt",history,"res",response)
    return response

## Create a function that returns the name of the protein 
def ProteinName(history, message):
    prompt = """Respond only with the name of the protein the message is referring to with respect to both
    the chat history above and the message itself. The message is as follows: """ + message
    
    history.append({"role":"user", "content": f"{prompt}"})
    # function_client = OpenAI(
    #     api_key=os.environ.get("OPENAI_API_KEY")
    # )
    # function_client = OpenAI()
    # response = function_client.chat.completions.create(
    #     model="gpt-4o-mini",
    #     messages=history,

    # )
    response=chatApiCall(history)
    # print("prompt protein name",history,"res",response)
    return response


## Create a function that takes in a protein name and returns protein info 
def ProteinInfo(protein): 
    print("caall hua hai",protein)
    accession, full_name = fetch_protein_info(protein)
    all_data = {
        "uniprot": fetch_uniprot_info(accession, email),
        "interpro": fetch_comprehensive_interpro_info(accession, email),
        # "string": fetch_string_info(accession, 9606, email),  # Assuming human (9606)
        "quickgo": {}
    }
    
    go_terms = fetch_protein_go_terms(accession, email)
    all_data["quickgo"]["go_terms"] = go_terms
    # for go_term in go_terms:
    #     all_data["quickgo"][go_term] = fetch_go_info(go_term, email)
    # print(all_data)
    # with open(f"{protein}.json",'w') as f:
    #     json.dump(all_data,f)
    return json.dumps(all_data)

## Create a function that takes in a message and a protein information and returns an informed response
def InformedResponse(proteinInfo, message):
    prompt = f"{proteinInfo} From the following information given, answer this question: " + message
    history=[]
    Agent = {"role": "system", "content": "You are a helpful assistant with extensive background in protein analysis."}
    history.append(Agent)
    history.append({"role":"user", "content": f"{prompt}"})
    # function_client = OpenAI(
    #     api_key=os.environ.get("OPENAI_API_KEY")
    # )
    # function_client = OpenAI()
    # response = function_client.chat.completions.create(
    #     model="gpt-4o-mini",
    #     messages=history,
    # )
    response=chatApiCall(history)
    # print("prompt",history,"res",response)
    return response

def HistoryConverter(history): 
    Agent = {"role": "system", "content": "You are a helpful assistant with extensive background in protein analysis."}

    formatted_history = []
    formatted_history.append(Agent)
    for each in history:
        formatted_history.append({"role": "user", "content": f"{each[0]}"})
        formatted_history.append({"role": "assistant", "content": f"{each[1]}"})
    return formatted_history



def openai_chatbot(message, history):
    formatted_history = HistoryConverter(history=history)

    isProteinRequest = IsProteinRequest(history=formatted_history, message=message)

    if isProteinRequest == "true": 
        proteinName = ProteinName(history=formatted_history, message=message)
        proteinInfo = ProteinInfo(protein=proteinName)
        print(proteinName,proteinInfo)
        return InformedResponse(proteinInfo=proteinInfo, message=message)
    else:
        # client = OpenAI()
        messages = HistoryConverter(history=history)
        messages.append({"role":"user","content": f"{message}"})
        # payload = {
        #     "messages": messages,
        #     "web_access": False
        # }
        # response=chatApiCall({"messages":history})


        # response = client.chat.completions.create(
        # model="gpt-4o-mini",
        # # messages=[{"role":"system", "content": "You are a helpful assistant"}, {"role":"user", "content":"Tell me about peter pan"}]
        # messages = messages

        # )
        
        response=chatApiCall(messages)
        # print("prompt",messages,"res",response)
        return response

if __name__=="__main__":
    demo_chatbot = gr.ChatInterface(openai_chatbot, title="ProteinSage", description="Ask anything about proteins, we fetch you data from major protein databases ;)")
    demo_chatbot.launch()