(neo4j_graph, llm_chain, input_question)
| 181 | |
| 182 | |
| 183 | def generate_ticket(neo4j_graph, llm_chain, input_question): |
| 184 | # Get high ranked questions |
| 185 | records = neo4j_graph.query( |
| 186 | "MATCH (q:Question) RETURN q.title AS title, q.body AS body ORDER BY q.score DESC LIMIT 3" |
| 187 | ) |
| 188 | questions = [] |
| 189 | for i, question in enumerate(records, start=1): |
| 190 | questions.append((question["title"], question["body"])) |
| 191 | # Ask LLM to generate new question in the same style |
| 192 | questions_prompt = "" |
| 193 | for i, question in enumerate(questions, start=1): |
| 194 | questions_prompt += f"{i}. \n{question[0]}\n----\n\n" |
| 195 | questions_prompt += f"{question[1][:150]}\n\n" |
| 196 | questions_prompt += "----\n\n" |
| 197 | |
| 198 | gen_system_template = f""" |
| 199 | You're an expert in formulating high quality questions. |
| 200 | Formulate a question in the same style and tone as the following example questions. |
| 201 | {questions_prompt} |
| 202 | --- |
| 203 | |
| 204 | Don't make anything up, only use information in the following question. |
| 205 | Return a title for the question, and the question post itself. |
| 206 | |
| 207 | Return format template: |
| 208 | --- |
| 209 | Title: This is a new title |
| 210 | Question: This is a new question |
| 211 | --- |
| 212 | """ |
| 213 | # we need jinja2 since the questions themselves contain curly braces |
| 214 | system_prompt = SystemMessagePromptTemplate.from_template( |
| 215 | gen_system_template, template_format="jinja2" |
| 216 | ) |
| 217 | chat_prompt = ChatPromptTemplate.from_messages( |
| 218 | [ |
| 219 | system_prompt, |
| 220 | SystemMessagePromptTemplate.from_template( |
| 221 | """ |
| 222 | Respond in the following template format or you will be unplugged. |
| 223 | --- |
| 224 | Title: New title |
| 225 | Question: New question |
| 226 | --- |
| 227 | """ |
| 228 | ), |
| 229 | HumanMessagePromptTemplate.from_template("{question}"), |
| 230 | ] |
| 231 | ) |
| 232 | llm_response = llm_chain( |
| 233 | f"Here's the question to rewrite in the expected format: ```{input_question}```", |
| 234 | [], |
| 235 | chat_prompt, |
| 236 | ) |
| 237 | new_title, new_question = extract_title_and_question(llm_response["answer"]) |
| 238 | return (new_title, new_question) |
no test coverage detected