Search code examples
javascriptpythonflaskgoogle-gemini

Why is Gemini API returning the results of older prompts as well when new prompts are given?


So I made a web application using Flask, where I can select certain text and use Gemini API to paraphrase it. The paraphrasing is working fine however, let's say I asked it to paraphrase string A first and it did that successfully. Now, when I ask it to paraphrase string B, it returns the result of paraphrase (string A + string B), which I don't want.

I am getting the currently selected text using a javascript button

var selectedText = window.getSelection().toString();

and sending it to Flask, where the implemented code is

@app.route('/paraphrase', methods=['POST'])
def paraphrase():
    genai.configure(api_key=API_KEY)
    try:
        text = request.get_json().get('text')
        contents_ = contents.copy()
        #contents_[0]['parts'].append({'text': text})
        contents_[0]['parts'].append({'text': ' '})
        contents_[0]['parts'][-1]['text'] = "paraphrase : " + text
        gemini = genai.GenerativeModel(model_name=model)
        response = gemini.generate_content(
            contents_,
            generation_config=generation_config,
            safety_settings=safety_settings,
            stream=False)
        return response.candidates[0].content.parts[0].text
    except:
        return text

Here the 'contents' contain the initial prompt

contents = [{'parts': [{'text': 'Your job is to paraphrase the given texts into easier language. Also remove any spelling and grammar mistakes if you find one. You will be given input in form of paraphrase: {text}, and your output should only contain the paraphrased text, no other text, not a thing unrelated'}]}]

As you can see from the Flask code, I have tried to create a copy of this 'contents' at start of each API call, tried to delete the 'contents_' variable, and finally tried to replace the text, but nothing changes. It still gives the paraphrased text of all the previously called texts along with new one.


Solution

  • It sounds like the copy() function is making a shallow copy of the list. So while it creates a new list, all the members of that list are the same members as in the original list. It hasn't made copies of them.

    So when you create contents_, the dictionary in content is the same dictionary. Then when you add to it - you're adding to both of them. (This is true for arrays as well.)

    You need a version of copy that makes a deep copy instead of a shallow copy. You can get this from the deepcopy() function that is part of the copy module. So you can do

    import copy
    
    ...
    
    contents_ = copy.deepcopy(contents)
    

    and contents_ will get a clean, deep, copy of contents without subsequent changes to contents_ changing the original.