Skip to content

Commit 65e6605

Browse files
committed
refactor(chain): add some comments
1 parent cf86b39 commit 65e6605

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

server/llm_services/langchain_chain.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@
22
import re
33
import asyncio
44
import datetime
5-
65
from typing import Awaitable
76
from langchain import LLMChain
87
from langchain.schema import Document
98
from langchain.callbacks import AsyncIteratorCallbackHandler
109

1110
import db_services as _dbs_
12-
13-
from utils.ebbinghaus import handle_ebbinghaus_memory
1411
from .langchain_llm import LLM
12+
from utils.ebbinghaus import handle_ebbinghaus_memory
1513
from prompts import choose_prompt
1614

1715

16+
# Main Chain class, which encapsulates LLMChain configurations and various methods.
1817
class Chain:
1918
def __init__(
2019
self,
@@ -37,6 +36,7 @@ def __init__(
3736
self.streaming = streaming
3837
self.llm_callbacks = [AsyncIteratorCallbackHandler()]
3938

39+
# Asynchronous method to generate questions.
4040
async def agenerate_questions(
4141
self,
4242
docs: list[Document],
@@ -57,6 +57,7 @@ async def agenerate_questions(
5757
_dbs_.file.set_file_is_uploading_state(self.file_id)
5858
raise e
5959

60+
# Helper method for specific question generation.
6061
async def _agenerate_questions(
6162
self,
6263
llm_chain: LLMChain,
@@ -79,6 +80,7 @@ async def _agenerate_questions(
7980
question_type=question_type
8081
)
8182

83+
# Method to check answers.
8284
async def aexamine_answer(
8385
self,
8486
quesiton_id: int,
@@ -139,6 +141,7 @@ def _init_llm_chain(self, timeout: int, role: str, question_type: str):
139141
)
140142

141143

144+
# Helper function to wait for asynchronous tasks to complete.
142145
async def _wait_done(
143146
fn: Awaitable,
144147
event: asyncio.Event
@@ -152,6 +155,7 @@ async def _wait_done(
152155
event.set()
153156

154157

158+
# Adjust concurrency based on payment status.
155159
def _adjust_concurrency_by_payment_status():
156160
payment = os.environ.get("PAYMENT", "free")
157161
if (payment == "free"):
@@ -160,6 +164,7 @@ def _adjust_concurrency_by_payment_status():
160164
return 3
161165

162166

167+
# Adjust the number of retries based on payment status.
163168
def _adjust_retries_by_payment_status():
164169
payment = os.environ.get("PAYMENT", "free")
165170
if (payment == "free"):
@@ -168,6 +173,7 @@ def _adjust_retries_by_payment_status():
168173
return 6
169174

170175

176+
# Split the generated questions.
171177
def _spite_questions(
172178
content: str,
173179
type: str
@@ -180,6 +186,7 @@ def _spite_questions(
180186
return questions
181187

182188

189+
# Check if the structure of the question is valid.
183190
def _is_legal_question_structure(
184191
content: str,
185192
type: str
@@ -195,11 +202,13 @@ def _is_legal_question_structure(
195202
return True
196203

197204

205+
# Remove prefix numbers or dashes from a question.
198206
def _remove_prefix_numbers(text):
199207
cleaned_text = re.sub(r'^\s*(?:\d+\.|-)\s*', '', text)
200208
return cleaned_text.strip()
201209

202210

211+
# Extract score from the answer.
203212
def _extract_score(anwser: str):
204213
score = re.findall(r"\d+\.?\d*", anwser)
205214
if score:
@@ -208,6 +217,7 @@ def _extract_score(anwser: str):
208217
return 0
209218

210219

220+
# Get the push date based on the score.
211221
def _get_push_date(score: int):
212222
now = datetime.datetime.now()
213223
days = handle_ebbinghaus_memory(score)

0 commit comments

Comments
 (0)