[Python] Issues with same question but different answer? using PandasAi LLM:OpenAI

Discussão em 'Python' iniciado por Stack, Dezembro 18, 2024.

  1. Stack

    Stack Membro Participativo

    I have make some python code that able to analyze question and give out the answer. The problem is every time i tried the tools with the same question, the answer sometimes correct and sometimes wrong. How to deal with this? Im using PandasAi since im working on large data and use OpenAI as LLM to it.

    openai.api_key = "API-Key" # Replace with your OpenAI API key
    llm = OpenAI(api_token=openai.api_key, temperature=0) # Reduced randomness

    def analyze_question(question, smart_df):
    """Analyze the given question using PandasAI and return the result."""
    try:
    question = preprocess_question(question)
    prompt = f"Analyze the dataset and answer: {question}"
    result = smart_df.chat(prompt)

    # Serialize the result safely to string
    if isinstance(result, pd.DataFrame):
    result_str = result.to_string(index=False)
    else:
    result_str = str(result)

    # Save data as JSON-compatible dictionary
    history.append({"question": question, "answer": result_str})
    save_history()
    return pd.DataFrame([[result_str]], columns=["Answer"])
    except Exception as e:
    error_message = f"Error analyzing question: {e}"
    history.append({"question": question, "answer": error_message})
    save_history()
    return pd.DataFrame([[error_message]], columns=["Error"])

    def ask_question():
    """Handle the user's question and display the result."""
    question = question_entry.get()
    if question:
    global last_result
    last_result = analyze_question(question, smart_df)
    answer_text.config(state=tk.NORMAL)
    answer_text.delete("1.0", tk.END)
    if isinstance(last_result, pd.DataFrame):
    answer_text.insert(tk.END, last_result.to_string(index=False))
    else:
    answer_text.insert(tk.END, last_result)
    answer_text.config(state=tk.DISABLED)
    else:
    messagebox.showwarning("Input Error", "Please enter a question.")

    Continue reading...

Compartilhe esta Página