What I’m trying to do: I coded GPT in my Anvil webpage, now I am trying to get GPT to read .txt files directly from anvil source data files and generate an answer from the files only
What I’ve tried and what’s not working: This code line
“file_text = anvil.server.call(‘return_text_from_file’)” is inserted with the GPT code block(did not work) and also inserted in the button_pressed code block (did not work). I put the codeline in multiple places and GPT cannot locate or read my file directly
Code Sample:
I wrote code on Form side when button is clicked:
def button_1_click(self, **event_args):
“”“This method is called when the button is clicked”“”
input = f’{self.text_area_1.text, self.text_area_2.text}’
file_text = anvil.server.call(‘return_text_from_file’)
qb_stat = anvil.server.call(“generate_qb”, input)
self.rich_text_1.content = qb_stat
I wrote this code in server module: @anvil.server.callable
def return_text_from_file():
# Read the contents of a file
with open(data_files[‘FootballQBtxt.txt’]) as f:
text = f.read()
return text
@anvil.server.callable
def generate_qb(input):
file_text = anvil.server.call(‘return_text_from_file’)
messages = [
{“role”: “system”,
“content”: “”"Read and search file ‘FootballQBtxt.txt’ for keywords and answers.
Note: I’m just a beginner, if you need more info, let me know where. If you don’t understand because it’s too vague, it’s fine too. It’s just a small project. Basically, I want GPT to read my “QB stats sheet” that I custom-made and show answers to the user based on my file only.
Is there any way you can make a clone link? Maybe clone your own app yourself an then take the GPT credentials out of it and wipe the info from your football text file and re-share a clone link of that “cleaned” app with no personal info?
You’ll find this will work more effectively if you use the Assistant API over at OpenAI and pass your file as media as the retrieval file. I’ve just done a project using this and it’s very easy to work with.
Thank you so much. I appreciate it. My roadblock is now trying to transfer it to anvil coding, and then get user input from a text box and insert that text to GPT. I still got a lot of work to figure out. Thank you so much again.
This will set up OpenAI in your Anvil app and let you pass inputs to the agent. Instead of user_input, you’d be passing the text_box_input
# Install openAi in the app / Settings / Python versions
from openai import OpenAI
client = OpenAI(api_key=secrets.get_secret("open_ai_key")) # Save your openAI key in the 'Secrets' module and call it "open_ai_key"
@anvil.server.callable
def openAI_chat(user_input):
completion = client.chat.completions.create(
model="gpt-3.5-turbo-16k", # Add or adjust the model you want to use here. See "https://platform.openai.com/docs/models" for the model list
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": user_input}
],
)
# Extract the response content and token count
agent_response = completion.choices[0].message.content
print(agent_response)
return agent_response, token_count
Hopefully, that will get you started.
Ping me if you need more help on this as I have several chat functions running with different LLMs so I may have come across some of the issues you’re facing.
Hi, I’m working on the Assistant API to Anvil platform. That code format is different than the OpenAI code you posted. I’ve found a way to get Assistant API working through Anvil. My two key issues now:
I can’t get my Assistant API code to read my ‘text_area_1’. It reads the file, but not the User input. 2nd issure: The loading time “anvil.server.timeouterror: server code took too long”, but I’m on a personal plan so that’s sort of fixed(LOL).
Here’s the code I’m having trouble with:Client Code:
def button_1_click(self, **event_args):
“”“This method is called when the button is clicked”“” #my variables - 2 text_areas
player_name= self.text_area_1.text
player_name2= self.text_area_2.text
input = f’{self.text_area_1.text, self.text_area_2.text}’ #calls to server-side functions
anvil.server.call(‘generate_GPT’, input)
qb_stat = anvil.server.call(‘run_GPT’, input) #Display Assistant API content back to User through rich_text UI
self.rich_text_1.content = qb_stat
Server-Side code: #Calls for Assistant API to create a thread
def generate_GPT(input):
message = client.beta.threads.messages.create(
thread_id=Thread.id,
role = “user”,
content = “Compare stats {input} using file-only.”
) #Calls for API to complete the thread and return the message to User’s rich_text @anvil.server.callable
def run_GPT(input):
run = client.beta.threads.runs.create(
thread_id=Thread.id,
assistant_id=assistant
)
while run.status != “completed”:
run = client.beta.threads.runs.retrieve(
thread_id=Thread.id,
run_id=run.id
) #print(run.status) #time.sleep(20)
messages = client.beta.threads.messages.list(
thread_id=Thread.id
)
for message in reversed(list(messages)):
return message.role + ": " + message.content[0].text.value
Hope this is clear! I will definitely be replying and picking your brain if I have more questions.