File size: 5,555 Bytes
2e4a01c e9af4ac 5ab9d35 9c14c56 e9af4ac 9c14c56 2e4a01c 5ab9d35 2e4a01c 9c14c56 2e4a01c 5ab9d35 9c14c56 e457bf4 9c14c56 5ab9d35 2e4a01c e9af4ac 00770ab 2e4a01c 5ab9d35 2e4a01c 5ab9d35 2e4a01c f98ec9a 2e4a01c e9af4ac 5ab9d35 e9af4ac 5ab9d35 e9af4ac 5ab9d35 e9af4ac 5ab9d35 e9af4ac 5ab9d35 e9af4ac 5ab9d35 a18c696 | 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 174 | import gradio as gr
import openai
import json
import time
import sys
from gpt_index import SimpleDirectoryReader, GPTListIndex, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
from langchain.chat_models import ChatOpenAI
import gradio as gr
import openai
from openai import OpenAI
import os
import time
import random
import math
os.environ["OPENAI_API_KEY"] = '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'
client = OpenAI()
def findOccurrences(s, ch):
return [i for i, letter in enumerate(s) if letter == ch]
def format(data: str):
data = data.replace("['", "")
data = data.replace("']", "")
data = data.replace("[\"", "")
data = data.replace("\"]", "")
data = data.replace("\\\'", "\'")
data = data.replace("\\n", "")
data = data.replace("**", "")
data = data.replace(".", ". ")
data = data.replace("#", "")
data = data.replace(" ", " ")
sourceErrorInstances = findOccurrences(data, "ใ")
lSourceErrorInstances = findOccurrences(data, "ใ")
for i in range(len(sourceErrorInstances)):
index = int(sourceErrorInstances[i])
lindex = int(lSourceErrorInstances[i])
data[index:lindex] = ""
return data
def runbot():
global iface
iface = gr.Interface(fn=chatbot,
inputs=gr.components.Textbox(lines=7, label="Enter your text"),
outputs="text",
title="Blue BotBuilders AI")
global thread
global my_assistant
global persona
print("\n\n\n\n\nSTACK and INFO:\n\n")
# Get persona
persona = open("data/persona.txt")
persona = persona.readlines()
# Get files and send them
print("(1/3) Sending data to OpenAI, this may take a while...\n")
up_files = []
files = client.files.create(
file=open("docs/data.txt", "rb"),
purpose="assistants"
)
up_files.append(files.id)
files = client.files.create(
file=open("docs/customdata.txt", "rb"),
purpose="assistants"
)
up_files.append(files.id)
my_assistant = client.beta.assistants.create(
instructions=f"{persona}",
name="Blube AI",
tools=[{"type": "retrieval"}],
model="gpt-3.5-turbo-1106",
file_ids=up_files
)
print("(2/3) Files sent, creating thread...\n")
# Create a thread for the assistant
thread = client.beta.threads.create()
print("(3/3) Finished creating thread, launching interface...\n")
iface.launch(share=False)
def getAssistant():
return my_assistant
def getPersona():
return persona
def getThread():
return thread
def chatbot(input_text):
stime = time.time()
my_assistant = getAssistant()
persona = getPersona()
thread = getThread()
print("(1/4) Message sent, getting response...\n")
message = client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content=f"{input_text}"
)
print("(2/4) Thread created, running thread...\n")
# Run the thread
run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=my_assistant.id,
instructions=f"{persona}"
)
print("(3/4) Thread active, getting thread...\n")
# Get the thread
run = client.beta.threads.runs.retrieve(
thread_id=thread.id,
run_id=run.id
)
print("(4/4) Thread received, awaiting chat completion\n")
# Wait for completion
t: int = 0
tt = 0
errorCode = 0
while run.status != "completed":
run = client.beta.threads.runs.retrieve(
thread_id=thread.id, run_id=run.id
)
s = run.status
if s == "failed":
print(f"Process failed after {tt} ticks at {run.failed_at} with error stack:\n{run.last_error}\n\n")
errorCode = 1
break
elif s == "cancelled":
print(f"Process cancelled after {tt} ticks at {run.cancelled_at} with error stack:\n{run.last_error}\n\n")
errorCode = 2
elif s == "in_progress" or s == "queued":
t = t + 1
tt = tt + 1
else:
print(s)
if t > 15:
print(f"Time taken is over 15 ticks, average time is 11, check issues.\n\nINFO: Status is {s} and the total ticks is {tt}")
t = 0
if errorCode == 1:
raise RuntimeError("\n\nThe process finished with an error: Process failed unexpectedly. Check the command prompt for more details. This error is most likely a overuse error, so the program will sleep for 20 seconds to avoid more errors\n\n")
time.sleep(20)
print("\n20 seconds is over: requests are up")
elif errorCode == 2:
raise RuntimeError("The process finished with an error: Process cancelled unexpectedly. Check the command prompt for more details.")
else:
messages = client.beta.threads.messages.list(
thread_id=thread.id
)
messages = messages.json()
print(f"Process completed in {tt} ticks at {run.completed_at}")
mesf = open("messages.json", "w")
mesf.write(messages)
mesf.close()
mesf = open("./messages.json", "rt")
mess = json.load(mesf)
mess = mess["data"]
finalMsg = []
finalMsg.append(mess[0]["content"][0]["text"]["value"])
finalMsg = str(finalMsg)
finalMsg = format(finalMsg)
etime = time.time()
ttime = etime - stime
print(f"Finished in {ttime} seconds.")
return finalMsg |