Может ли кто-нибудь помочь мне разобраться в проблеме с моим кодом? Я пытаюсь разработать чат-бота, который может решать задачи по алгебре с помощью Sympy, но постоянно получаю одни и те же ошибки и не могу в этом разобраться. сообщение об ошибке
*# Ниже приведен мой код: *
import os
import json
from sympy import symbols, Eq, solve
from sympy.parsing.sympy_parser import standard_transformations, implicit_multiplication_application, parse_expr
from transformers import (
AutoTokenizer,
AutoModelForCausalLM,
pipeline
)
import re
def normalize_text(text):
"""Normalize text for consistent matching."""
text = text.lower().strip()
text = re.sub(r'[^\w\s]', '', text) # Remove punctuation
return text
# Predefined Responses
def get_predefined_response(user_query, file_path="algebra_data.json"):
with open(file_path, "r") as f:
predefined_responses = json.load(f)
user_query_normalized = normalize_text(user_query)
for entry in predefined_responses:
if normalize_text(entry["prompt"]) == user_query_normalized:
return entry["response"]
return None
# SymPy Solver
def solve_algebra(problem):
try:
# Define the variable(s) used in the equation
x = symbols('x')
# Replace '^' with '**' for SymPy compatibility
problem = problem.replace("^", "**").replace("Solve", "").strip()
# Debug: Print the original input problem
print(f"Debug: Original problem: {problem}")
# Enable implicit multiplication and other transformations
transformations = (standard_transformations + (implicit_multiplication_application,))
# Extract and parse the equation
if "=" in problem:
left, right = problem.split("=")
left_expr = parse_expr(left.strip(), transformations=transformations)
right_expr = parse_expr(right.strip(), transformations=transformations)
equation = Eq(left_expr, right_expr)
else:
equation = parse_expr(problem.strip(), transformations=transformations)
# Debug: Print the parsed equation
print(f"Debug: Parsed equation: {equation}")
# Solve the equation
solutions = solve(equation, x)
# Debug: Print the solutions and their type
print(f"Debug: Solutions: {solutions}, Type: {type(solutions)}")
# Handle solutions
if isinstance(solutions, list): # Multiple solutions or single solution in a list
if not solutions: # Empty list
return "No solutions found."
return f"The solutions are: {', '.join(map(str, solutions))}"
elif hasattr(solutions, 'is_real') and not solutions.is_real: # Complex solution
return f"The solution is a complex number: {solutions}"
elif isinstance(solutions, (int, float, symbols)): # Single solution (non-list)
return f"The solution is: {solutions}"
else:
# Handle unexpected return types
return f"The solution is: {solutions}"
except Exception as e:
# Debug: Print the error
print(f"Debug: Error in solve_algebra: {e}")
return f"Sorry, I couldn't solve that problem. Error: {e}"
# Load Pretrained Model and Tokenizer
model_name = "microsoft/DialoGPT-medium"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
# Add a padding token if not present
if tokenizer.pad_token is None:
tokenizer.add_special_tokens({'pad_token': '[PAD]'})
model.resize_token_embeddings(len(tokenizer)) # Resize the embeddings
# Test the Fine-Tuned Chatbot
print("\nTesting the chatbot interactively...\n")
chatbot = pipeline("text-generation", model="./algebra_chatbot", tokenizer="./algebra_chatbot")
# Interactive Chat Loop
print("Algebra Chatbot: Hello! I can help you with basic algebra. Type 'exit' to quit.\n")
while True:
user_input = input("User: ")
if user_input.lower() in ["exit", "quit", "bye"]:
print("Algebra Chatbot: Goodbye! Keep practicing algebra!")
break
# Check for predefined responses
predefined_response = get_predefined_response(user_input)
if predefined_response:
print(f"Algebra Chatbot: {predefined_response}")
# Check for algebraic problems and solve them dynamically
elif "solve" in user_input.lower() or "=" in user_input:
print(f"Algebra Chatbot: {solve_algebra(user_input)}")
# Use the fine-tuned model for general questions
else:
response = chatbot(
f"User: {user_input}\nBot:",
max_length=100,
num_return_sequences=1,
truncation=True # Add truncation explicitly
)
bot_response = response[0]["generated_text"].split("Bot:")[-1].strip()
print(f"Algebra Chatbot: {bot_response}")`
Я пытался добавить обработчик неожиданных решений и явно проверять списки, целые числа, числа с плавающей точкой и символические решения, но по какой-то причине это не работает.>
Может ли кто-нибудь помочь мне разобраться в проблеме с моим кодом? Я пытаюсь разработать чат-бота, который может решать задачи по алгебре с помощью Sympy, но постоянно получаю одни и те же ошибки и не могу в этом разобраться. сообщение об ошибке *# Ниже приведен мой код: * [code] import os import json from sympy import symbols, Eq, solve from sympy.parsing.sympy_parser import standard_transformations, implicit_multiplication_application, parse_expr from transformers import ( AutoTokenizer, AutoModelForCausalLM, pipeline ) import re
def normalize_text(text): """Normalize text for consistent matching.""" text = text.lower().strip() text = re.sub(r'[^\w\s]', '', text) # Remove punctuation return text
# Predefined Responses def get_predefined_response(user_query, file_path="algebra_data.json"): with open(file_path, "r") as f: predefined_responses = json.load(f)
for entry in predefined_responses: if normalize_text(entry["prompt"]) == user_query_normalized: return entry["response"]
return None
# SymPy Solver
def solve_algebra(problem): try: # Define the variable(s) used in the equation x = symbols('x')
# Replace '^' with '**' for SymPy compatibility problem = problem.replace("^", "**").replace("Solve", "").strip()
# Debug: Print the original input problem print(f"Debug: Original problem: {problem}")
# Enable implicit multiplication and other transformations transformations = (standard_transformations + (implicit_multiplication_application,))
# Extract and parse the equation if "=" in problem: left, right = problem.split("=") left_expr = parse_expr(left.strip(), transformations=transformations) right_expr = parse_expr(right.strip(), transformations=transformations) equation = Eq(left_expr, right_expr) else: equation = parse_expr(problem.strip(), transformations=transformations)
# Debug: Print the parsed equation print(f"Debug: Parsed equation: {equation}")
# Solve the equation solutions = solve(equation, x)
# Debug: Print the solutions and their type print(f"Debug: Solutions: {solutions}, Type: {type(solutions)}")
# Handle solutions if isinstance(solutions, list): # Multiple solutions or single solution in a list if not solutions: # Empty list return "No solutions found." return f"The solutions are: {', '.join(map(str, solutions))}" elif hasattr(solutions, 'is_real') and not solutions.is_real: # Complex solution return f"The solution is a complex number: {solutions}" elif isinstance(solutions, (int, float, symbols)): # Single solution (non-list) return f"The solution is: {solutions}" else: # Handle unexpected return types return f"The solution is: {solutions}"
except Exception as e: # Debug: Print the error print(f"Debug: Error in solve_algebra: {e}") return f"Sorry, I couldn't solve that problem. Error: {e}"
# Load Pretrained Model and Tokenizer model_name = "microsoft/DialoGPT-medium" tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForCausalLM.from_pretrained(model_name)
# Add a padding token if not present if tokenizer.pad_token is None: tokenizer.add_special_tokens({'pad_token': '[PAD]'}) model.resize_token_embeddings(len(tokenizer)) # Resize the embeddings
# Test the Fine-Tuned Chatbot print("\nTesting the chatbot interactively...\n") chatbot = pipeline("text-generation", model="./algebra_chatbot", tokenizer="./algebra_chatbot")
# Interactive Chat Loop print("Algebra Chatbot: Hello! I can help you with basic algebra. Type 'exit' to quit.\n") while True: user_input = input("User: ") if user_input.lower() in ["exit", "quit", "bye"]: print("Algebra Chatbot: Goodbye! Keep practicing algebra!") break
# Check for predefined responses predefined_response = get_predefined_response(user_input) if predefined_response: print(f"Algebra Chatbot: {predefined_response}") # Check for algebraic problems and solve them dynamically elif "solve" in user_input.lower() or "=" in user_input: print(f"Algebra Chatbot: {solve_algebra(user_input)}") # Use the fine-tuned model for general questions else: response = chatbot( f"User: {user_input}\nBot:", max_length=100, num_return_sequences=1, truncation=True # Add truncation explicitly ) bot_response = response[0]["generated_text"].split("Bot:")[-1].strip() print(f"Algebra Chatbot: {bot_response}")` [/code] Я пытался добавить обработчик неожиданных решений и явно проверять списки, целые числа, числа с плавающей точкой и символические решения, но по какой-то причине это не работает.>