I try to make a chatbot but have a error but no problem there

What I’m trying to do:
I try to make a Chatbot with the Openai API. I clone the Chrismast chat bot you ready have in Anvil -
the error msg
SyntaxError: invalid syntax (<string>, line 1)

  • at <string>:1
  • called from Form1, line 30

What I’ve tried and what’s not working:
I try to add the from anvil_openai import AnvilOpenAI
but not found, Maybe the problem is no have in the front end the openai api access

Code Sample:

# this is a formatted code snippet.
# paste your code between ``` from ._anvil_designer import Form1Template
from anvil import *
import anvil.server
import anvil.tables as tables
import anvil.tables.query as q
from anvil.tables import app_tables

class Form1(Form1Template):
    def __init__(self, **properties):
        # Set Form properties and Data Bindings.
        self.init_components(**properties)

        # Any code you write here will run when the form opens.
        self.conversation = anvil.server.call('get_conversation')
        self.refresh_conversation()

    def refresh_conversation(self):
        messages = []
        user_inputs = self.conversation["past_user_inputs"]
        responses = self.conversation["generated_responses"]
        for idx in range(len(user_inputs)):
            messages.append({"from": "user", "text": user_inputs[idx]})
            messages.append({"from": "bot", "text": responses[idx]})
        self.repeating_panel_1.items = messages

    def send_btn_click(self, **event_args):
        """This method is called when the button is clicked"""
        self.conversation = anvil.server.call('send_message', self.new_message_box.text)
        self.new_message_box.text = ""
        self.refresh_conversation()
        self.send_btn.scroll_into_view()

    def new_message_box_pressed_enter(self, **event_args):
        """This method is called when the user presses Enter in this text box"""
        self.send_btn_click(**event_args)
we

Clone link: - Anvil | Login
share a copy of your app

The cloned app doesn’t have any syntax errors.

When you get the error, click on the link and the line with the error will be selected.

Hello - Thanks for Reply - yes If you see I copy the error
the error show a sintax error in Line 1. but there no error, maybe some library left, but I not sure.
this is line 1
from ._anvil_designer import Form1Template

This is what I see in the cloned app, no syntax error:

image


This is what I see if I add xxx and cause a syntax error, I see a red wiggle:

image


Where do you see the error?
Do you see any red wiggle?

here, when I run, try to send some msg and error. - yes maybe is my env.

If I add a made up key and try the same, I get an error because I got the wrong key, but in order to get that error it is going past your syntax error:
image


If I add a few random characters on line 3 of ServerModule I get an error message that (1) includes the module name and (2) prevents the form from appearing and me from testing it:
image
image

I don’t understand why it’s telling you there is an error at… line 3 of module string?!


Try to add a few print statements to see which lines of code have been executed, something like this:
image

I also not understand, I was thinking some library left, but also can not install open Ai, maybe is my trial the env have some left. but not sure for that I make the post.

Try to add those print statements and see what you get.

here the result - and yes error change from line 1 to line 3 - but all time maintain line 28 and 35
only print the 3

Try to add these prints:
image

all that ok

but yes the error also change

I change the Funcstions and have new error

SyntaxError: unexpected EOF while parsing (<string>, line 1)

  • at <string>:1
  • called from Form1, line 28
  • called from Form1, line 35

I had not noticed that you are using eval, that’s the problem!!

The string that you are passing to eval must be a valid python expression.

Try to print it before evaluating it:
image

Now have that, that means all is work… I think is only ENV problem, for my trial I can not add to the ENV the openai library maybe that can solve all - but I try to one of people of anvil give me the info.

It looks like get_resonse returns a text: return response.choices[0].text.strip()

Then the text is stored in model_output = get_response(...

Then it is evaluated, assuming that it’s a valid Python expression: model_output_dict = eval(model_output)

I see two problems here:

  1. The text that I see printed on your snapshot is not a valid Python expression. To fix your problem perhaps you need to create a function that converts that text into a dictionary?
  2. Using eval with a text returned by an external library is dangerous. You risk that the library gives you an expression designed to delete your database or send them your secrets.
1 Like

I feel like I don’t have the knowledge for replicate :smiling_face_with_tear:

Ok now I modify the code, remove the Eval and make again all the front side of the app. but still have some problem I can not solve

Error msg
SyntaxError: invalid character '¿' (U+00BF) (<string>, line 1)

  • at <string>:1
  • called from Form1, line 28

Clone of the app