File size: 4,663 Bytes
08dd874
 
20fe8c3
08dd874
 
 
 
 
 
 
 
 
47c7f9e
 
08dd874
 
47c7f9e
08dd874
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47c7f9e
 
08dd874
 
47c7f9e
08dd874
 
 
 
 
 
47c7f9e
 
fbf5201
08dd874
fbf5201
47c7f9e
 
08dd874
 
 
 
 
3583222
08dd874
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64e1ffe
 
 
 
08dd874
 
 
 
 
 
 
 
20fe8c3
3583222
08dd874
47c7f9e
 
 
64e1ffe
47c7f9e
 
 
 
 
 
 
08dd874
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fbf5201
08dd874
 
 
 
 
 
 
 
47c7f9e
08dd874
 
 
 
 
 
 
 
 
 
 
47c7f9e
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
import gradio as gr
import os
from callback_handler import QueueCallback
from collections.abc import Generator
from queue import Queue, Empty
from threading import Thread


from dotenv import load_dotenv

load_dotenv()

from call_openai import call_openai

import pinecone
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import Pinecone


OPENAI_API_KEY=os.environ["OPENAI_API_KEY"]
PINECONE_API_KEY=os.environ["PINECONE_API_KEY"]
PINECONE_ENV=os.environ["PINECONE_ENV"]
PINECONE_INDEX=os.environ["PINECONE_INDEX"]

# TOOL
#####################################################################
pinecone.init(
    api_key=PINECONE_API_KEY, 
    environment=PINECONE_ENV
)

index = pinecone.Index(PINECONE_INDEX)
embedder = OpenAIEmbeddings()


class PineconeSearch:
    docsearch = None
    topk = 2

    def __init__(
        self,
        namespace,
        topk
        ):
        self.docsearch = Pinecone.from_existing_index(PINECONE_INDEX, embedder, namespace=namespace)
        self.topk=topk

    def __call__(self,query):
        docs = self.docsearch.similarity_search(query=query, k=self.topk)
        context = "ARTICLES:\n\n"
        for doc in docs:
            context += f"Content:\n{doc.page_content}\n\n"
            context += f"Source: {doc.metadata['url']}\n"
            context += "----"
        return context



def query_tool(category, pinecone_topk, query):
    print(query)
    data = {
        "1_D3_receptor": "demo-richter-target-400-30-1",
        "2_dopamine": "demo-richter-target-400-30-2",
        "3_mitochondrial": "demo-richter-target-400-30-3"
    }
    
    pinecone_namespace = data[category]

    search_tool = PineconeSearch(
        namespace=pinecone_namespace,
        topk=pinecone_topk,
    )

    return search_tool(query)



def print_token_and_price(response):
    inp = sum(response["token_usage"]["prompt_tokens"])
    out = sum( response["token_usage"]["completion_tokens"])
    print(f"Token usage: {inp+out}")
    price = inp/1000*0.01 + out/1000*0.03
    print(f"Total price: {price*370:.2f} Ft")
    print("===================================")

agent_prompt = """You are an expert research assistant. You can access information about articles via your tool. 
Use information ONLY from this tool. Do not invent or add any more knowladge, be strict for the articles.
Answer the question in a few brief sentence based on the piece of article you get from your tool. 
Quote the used sources in [bracket] next to the facts, and at the end of your answer write them out"""


def stream(input_text, history, user_prompt, topic, topk) -> Generator:
    # Create a Queue
    q = Queue()
    job_done = object()

    # Create a funciton to call - this will run in a thread
    def task():        
        tool_resp = query_tool(topic, topk, str(input_text))

        response = call_openai(
            messages=[{"role": "system", "content": agent_prompt},
             {"role": "user", "content": input_text},
             {"role": "user", "content": tool_resp}
             ],
             stream="token",
             model="gpt-4-1106-preview",
             callback=QueueCallback(q)
        )

        print(response)

        #print_token_and_price(response=response)
        q.put(job_done)

    # Create a thread and start the function
    t = Thread(target=task)
    t.start()

    content = ""

    # Get each new token from the queue and yield for our generator
    counter = 0
    while True:
        try:
            next_token = q.get(True, timeout=1)
            if next_token is job_done:
                break
            content += next_token
            counter += 1
            if counter == 20:
                content += "\n"
                counter = 0
            if "\n" in next_token:
                counter = 0
            yield next_token, content
        except Empty:
            continue

def ask_llm(message, history, prompt, topic, topk):
    for next_token, content in stream(message, history, prompt, topic, topk):
        yield(content)


agent_prompt_textbox = gr.Textbox(
    label = "Set the behaviour of the agent",
    lines = 2,
    value = "NOT WORKING"
)
namespace_drobdown = gr.Dropdown(
    ["1_D3_receptor", "2_dopamine", "3_mitochondrial"],
    label="Choose a topic",
    value="1_D3_receptor"
    )
topk_slider = gr.Slider(
    minimum=10,
    maximum=100,
    value=70,
    step=10
)


additional_inputs = [agent_prompt_textbox, namespace_drobdown, topk_slider]

chatInterface = gr.ChatInterface(
    fn=ask_llm,
    additional_inputs=additional_inputs,
    additional_inputs_accordion_name="Agent parameters"
    ).queue().launch()