-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(prompt_path): path issue if pandasai used outside of pandas env #909
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,6 +1,8 @@ | ||||||||||||||||||||||||||||||||||||
import re | ||||||||||||||||||||||||||||||||||||
from jinja2 import Environment, FileSystemLoader | ||||||||||||||||||||||||||||||||||||
from typing import Optional | ||||||||||||||||||||||||||||||||||||
import os | ||||||||||||||||||||||||||||||||||||
from pathlib import Path | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
class BasePrompt: | ||||||||||||||||||||||||||||||||||||
|
@@ -20,7 +22,10 @@ def __init__(self, **kwargs): | |||||||||||||||||||||||||||||||||||
env = Environment() | ||||||||||||||||||||||||||||||||||||
self.prompt = env.from_string(self.template) | ||||||||||||||||||||||||||||||||||||
elif self.template_path: | ||||||||||||||||||||||||||||||||||||
env = Environment(loader=FileSystemLoader("pandasai/prompts/templates")) | ||||||||||||||||||||||||||||||||||||
# find path to template file | ||||||||||||||||||||||||||||||||||||
current_dir_path = Path(__file__).parent | ||||||||||||||||||||||||||||||||||||
path_to_template = os.path.join(current_dir_path, "templates") | ||||||||||||||||||||||||||||||||||||
env = Environment(loader=FileSystemLoader(path_to_template)) | ||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (llm): Ensure that the
Comment on lines
+26
to
+28
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logic to dynamically locate the template file path using - path_to_template = os.path.join(current_dir_path, "templates")
+ path_to_template = current_dir_path / "templates" Committable suggestion
Suggested change
The approach to finding the template path and loading it is sound, but it's important to handle potential exceptions that could arise from file not found or access errors when attempting to load the template. Consider adding error handling around the template loading logic to provide more informative error messages to the user or to log such incidents for debugging purposes. try:
env = Environment(loader=FileSystemLoader(path_to_template))
self.prompt = env.get_template(self.template_path)
except TemplateNotFound as e:
# Handle or log the error appropriately
raise CustomException(f"Template not found: {self.template_path}") from e Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||
self.prompt = env.get_template(self.template_path) | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
def render(self): | ||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (llm): Using
os.path.join
withpathlib.Path
objects is mixing path handling libraries. Consider usingPath
methods for consistency and clarity.