From a9af6c5de09f8769202520d0657202041b2c6ca5 Mon Sep 17 00:00:00 2001
From: Marcus Schiesser <mail@marcusschiesser.de>
Date: Tue, 26 Dec 2023 16:43:34 +0700
Subject: [PATCH] fix: use base service context

---
 .../components/vectordbs/python/none/context.py      | 12 ++----------
 templates/types/simple/fastapi/app/context.py        | 11 +++++++++++
 templates/types/simple/fastapi/app/engine/index.py   | 11 +----------
 templates/types/simple/fastapi/main.py               |  1 +
 templates/types/streaming/fastapi/app/context.py     | 11 +++++++++++
 .../types/streaming/fastapi/app/engine/index.py      | 11 +----------
 6 files changed, 27 insertions(+), 30 deletions(-)
 create mode 100644 templates/types/simple/fastapi/app/context.py
 create mode 100644 templates/types/streaming/fastapi/app/context.py

diff --git a/templates/components/vectordbs/python/none/context.py b/templates/components/vectordbs/python/none/context.py
index 271ac872..4756d813 100644
--- a/templates/components/vectordbs/python/none/context.py
+++ b/templates/components/vectordbs/python/none/context.py
@@ -1,15 +1,7 @@
-import os
-
 from llama_index import ServiceContext
-from llama_index.llms import OpenAI
-from app.engine.constants import CHUNK_SIZE, CHUNK_OVERLAP
-
 
-def create_base_context():
-    model = os.getenv("MODEL", "gpt-3.5-turbo")
-    return ServiceContext.from_defaults(
-        llm=OpenAI(model=model),
-    )
+from app.context import create_base_context
+from app.engine.constants import CHUNK_SIZE, CHUNK_OVERLAP
 
 
 def create_service_context():
diff --git a/templates/types/simple/fastapi/app/context.py b/templates/types/simple/fastapi/app/context.py
new file mode 100644
index 00000000..ae00de21
--- /dev/null
+++ b/templates/types/simple/fastapi/app/context.py
@@ -0,0 +1,11 @@
+import os
+
+from llama_index import ServiceContext
+from llama_index.llms import OpenAI
+
+
+def create_base_context():
+    model = os.getenv("MODEL", "gpt-3.5-turbo")
+    return ServiceContext.from_defaults(
+        llm=OpenAI(model=model),
+    )
diff --git a/templates/types/simple/fastapi/app/engine/index.py b/templates/types/simple/fastapi/app/engine/index.py
index 47a9b083..663b595a 100644
--- a/templates/types/simple/fastapi/app/engine/index.py
+++ b/templates/types/simple/fastapi/app/engine/index.py
@@ -1,15 +1,6 @@
-import os
-
-from llama_index import ServiceContext
 from llama_index.chat_engine import SimpleChatEngine
-from llama_index.llms import OpenAI
-
 
-def create_base_context():
-    model = os.getenv("MODEL", "gpt-3.5-turbo")
-    return ServiceContext.from_defaults(
-        llm=OpenAI(model=model),
-    )
+from app.context import create_base_context
 
 
 def get_chat_engine():
diff --git a/templates/types/simple/fastapi/main.py b/templates/types/simple/fastapi/main.py
index 00cb79c4..ba56f034 100644
--- a/templates/types/simple/fastapi/main.py
+++ b/templates/types/simple/fastapi/main.py
@@ -1,4 +1,5 @@
 from dotenv import load_dotenv
+
 load_dotenv()
 
 import logging
diff --git a/templates/types/streaming/fastapi/app/context.py b/templates/types/streaming/fastapi/app/context.py
new file mode 100644
index 00000000..ae00de21
--- /dev/null
+++ b/templates/types/streaming/fastapi/app/context.py
@@ -0,0 +1,11 @@
+import os
+
+from llama_index import ServiceContext
+from llama_index.llms import OpenAI
+
+
+def create_base_context():
+    model = os.getenv("MODEL", "gpt-3.5-turbo")
+    return ServiceContext.from_defaults(
+        llm=OpenAI(model=model),
+    )
diff --git a/templates/types/streaming/fastapi/app/engine/index.py b/templates/types/streaming/fastapi/app/engine/index.py
index 47a9b083..663b595a 100644
--- a/templates/types/streaming/fastapi/app/engine/index.py
+++ b/templates/types/streaming/fastapi/app/engine/index.py
@@ -1,15 +1,6 @@
-import os
-
-from llama_index import ServiceContext
 from llama_index.chat_engine import SimpleChatEngine
-from llama_index.llms import OpenAI
-
 
-def create_base_context():
-    model = os.getenv("MODEL", "gpt-3.5-turbo")
-    return ServiceContext.from_defaults(
-        llm=OpenAI(model=model),
-    )
+from app.context import create_base_context
 
 
 def get_chat_engine():
-- 
GitLab