|
78 | 78 | "source": [ |
79 | 79 | "class RagPipeline:\n", |
80 | 80 | " def __init__(self, context_path: str):\n", |
81 | | - " # Wrap OpenAI client with Openlayer's `trace_openai` to trace it \n", |
| 81 | + " # Wrap OpenAI client with Openlayer's `trace_openai` to trace it\n", |
82 | 82 | " self.openai_client = trace_openai(OpenAI())\n", |
83 | | - " \n", |
| 83 | + "\n", |
84 | 84 | " self.vectorizer = TfidfVectorizer()\n", |
85 | | - " with open(context_path, 'r', encoding='utf-8') as file:\n", |
86 | | - " self.context_sections = file.read().split('\\n\\n') \n", |
| 85 | + " with open(context_path, \"r\", encoding=\"utf-8\") as file:\n", |
| 86 | + " self.context_sections = file.read().split(\"\\n\\n\")\n", |
87 | 87 | " self.tfidf_matrix = self.vectorizer.fit_transform(self.context_sections)\n", |
88 | 88 | "\n", |
89 | 89 | " # Decorate the functions you'd like to trace with @trace()\n", |
|
100 | 100 | "\n", |
101 | 101 | " @trace()\n", |
102 | 102 | " def retrieve_context(self, query: str) -> str:\n", |
103 | | - " \"\"\"Context retriever. \n", |
104 | | - " \n", |
| 103 | + " \"\"\"Context retriever.\n", |
| 104 | + "\n", |
105 | 105 | " Given the query, returns the most similar context (using TFIDF).\n", |
106 | 106 | " \"\"\"\n", |
107 | 107 | " query_vector = self.vectorizer.transform([query])\n", |
|
115 | 115 | " the prompt (formatted to conform with OpenAI models).\"\"\"\n", |
116 | 116 | " return [\n", |
117 | 117 | " {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n", |
118 | | - " {\"role\": \"user\", \"content\": f\"Answer the user query using only the following context: {context}. \\nUser query: {query}\"}\n", |
| 118 | + " {\n", |
| 119 | + " \"role\": \"user\",\n", |
| 120 | + " \"content\": f\"Answer the user query using only the following context: {context}. \\nUser query: {query}\",\n", |
| 121 | + " },\n", |
119 | 122 | " ]\n", |
120 | 123 | "\n", |
121 | 124 | " @trace()\n", |
|
0 commit comments