-
Notifications
You must be signed in to change notification settings - Fork 12
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
Add configurable prompts #35
base: main
Are you sure you want to change the base?
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 |
---|---|---|
|
@@ -33,28 +33,33 @@ def __init__( | |
chat_session: GenerativeModelChatSession, | ||
config: dict = None, | ||
last_answer: str = None, | ||
cypher_prompt: str = None, | ||
) -> None: | ||
self.ontology = ontology | ||
self.config = config or {} | ||
self.graph = graph | ||
self.chat_session = chat_session | ||
self.last_answer = last_answer | ||
self.cypher_prompt = cypher_prompt | ||
|
||
def run(self, question: str, retries: int = 5): | ||
error = False | ||
|
||
cypher = "" | ||
while error is not None and retries > 0: | ||
try: | ||
cypher_prompt = ( | ||
(CYPHER_GEN_PROMPT.format(question=question) | ||
if self.last_answer is None | ||
else CYPHER_GEN_PROMPT_WITH_HISTORY.format(question=question, last_answer=self.last_answer)) | ||
if error is False | ||
else CYPHER_GEN_PROMPT_WITH_ERROR.format( | ||
question=question, error=error | ||
) | ||
) | ||
if self.cypher_prompt is not None: | ||
cypher_prompt = self.cypher_prompt | ||
else: | ||
cypher_prompt = ( | ||
(CYPHER_GEN_PROMPT.format(question=question) | ||
if self.last_answer is None | ||
else CYPHER_GEN_PROMPT_WITH_HISTORY.format(question=question, last_answer=self.last_answer)) | ||
if error is False | ||
else CYPHER_GEN_PROMPT_WITH_ERROR.format( | ||
question=question, error=error | ||
) | ||
) | ||
Comment on lines
+51
to
+62
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. 🛠️ Refactor suggestion Refactor nested ternary expressions for better readability. The current implementation uses nested ternary expressions which can be hard to read and maintain. Consider extracting the logic into a separate method for better clarity. Here's a suggested refactor: - if self.cypher_prompt is not None:
- cypher_prompt = self.cypher_prompt
- else:
- cypher_prompt = (
- (CYPHER_GEN_PROMPT.format(question=question)
- if self.last_answer is None
- else CYPHER_GEN_PROMPT_WITH_HISTORY.format(question=question, last_answer=self.last_answer))
- if error is False
- else CYPHER_GEN_PROMPT_WITH_ERROR.format(
- question=question, error=error
- )
- )
+ cypher_prompt = self._get_cypher_prompt(question, error)
+
+ def _get_cypher_prompt(self, question: str, error: Exception | bool) -> str:
+ if self.cypher_prompt is not None:
+ return self.cypher_prompt
+
+ if error:
+ return CYPHER_GEN_PROMPT_WITH_ERROR.format(
+ question=question,
+ error=error
+ )
+
+ if self.last_answer is None:
+ return CYPHER_GEN_PROMPT.format(question=question)
+
+ return CYPHER_GEN_PROMPT_WITH_HISTORY.format(
+ question=question,
+ last_answer=self.last_answer
+ )
|
||
logger.debug(f"Cypher Prompt: {cypher_prompt}") | ||
cypher_statement_response = self.chat_session.send_message( | ||
cypher_prompt, | ||
|
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.
🛠️ Refactor suggestion
Standardize ontology injection method.
The code uses two different approaches to inject the ontology:
replace("#ONTOLOGY", str(ontology.to_json()))
for the default case"\nOntology:\n"
for custom instructionsThis inconsistency could lead to formatting differences and maintenance issues.
Consider standardizing the approach: