1、修改api文件位置

2、意图识别继承langfuse
This commit is contained in:
2025-08-27 11:22:54 +08:00
parent 53ac47f4a5
commit c9c7f13060
12 changed files with 1385 additions and 1321 deletions
+76 -25
View File
@@ -19,6 +19,8 @@ import jieba
import time
import threading
from langchain_openai import ChatOpenAI
from .PromptTemplates import (classification_prompt, query_rewrite_prompt_pro,
extract_nouns_prompt, classification_info,
slot_filling_prompt, step_back_prompt,
@@ -32,7 +34,10 @@ from .DataModels import (
StepBackPrompt, HypotheticalDocument
)
from .ProfessionalNounVector import ProfessionalNounRetriever, AsyncProfessionalNounRetriever
from rag2_0.tool.ModelTool import XinferenceReRankerModel, OpenAiLLM
from rag2_0.tool.APIKeyManager import APIKeyManager
TEMPERATURE = 0.4
TOP_P = 0.7
class AsyncIntentRecognizer:
SOFT_WIKI_PATH = "data/wiki_data"
@@ -59,21 +64,6 @@ class AsyncIntentRecognizer:
model_name: 要使用的模型名称
vector_index_dir: 向量索引目录,如果为None则使用默认目录
"""
api_key = os.getenv("OPENAI_API_KEY")
base_url = os.getenv("OPENAI_API_BASE")
model_name = os.getenv("MODEL_NAME", "gpt-3.5-turbo")
# 初始化LLM
llm_params = {
"temperature": 0.4, # 降低随机性,使结果更确定
"top_p": 0.7,
"model": model_name,
"api_key": api_key,
"base_url": base_url
}
self._llm = OpenAiLLM(**llm_params)
llm_params["model"] = os.getenv("MINI_MODEL_NAME", "gpt-3.5-turbo")
self._llm_mini = OpenAiLLM(**llm_params)
# 加载suffix关键词
self._suffix_keywords = self._load_suffix_keywords()
@@ -199,7 +189,15 @@ class AsyncIntentRecognizer:
# 解析输出
try:
# 异步调用LLM
response = await self._llm.invoke_async(formatted_prompt, False)
llm = ChatOpenAI(
api_key=APIKeyManager.get_api_key(),
openai_api_base=os.getenv("OPENAI_API_BASE"),
model_name=os.getenv("MODEL_NAME"),
temperature=TEMPERATURE,
top_p=TOP_P
)
llm.with_structured_output(Classification)
response = await llm.ainvoke(formatted_prompt)
# 尝试直接解析JSON响应
response.content = response.content.strip()
@@ -266,8 +264,17 @@ class AsyncIntentRecognizer:
terms_list_parser = PydanticOutputParser(pydantic_object=TermList)
formatted_prompt = formatted_prompt.replace("{output_format}", terms_list_parser.get_format_instructions())
llm = ChatOpenAI(
api_key=APIKeyManager.get_api_key(),
openai_api_base=os.getenv("OPENAI_API_BASE"),
model_name=os.getenv("MODEL_NAME"),
temperature=TEMPERATURE,
top_p=TOP_P
)
llm.with_structured_output(TermList)
# 异步调用LLM
response = await self._llm.invoke_async(formatted_prompt, False)
response = await llm.ainvoke(formatted_prompt)
# 尝试使用Pydantic解析器解析TermList
response.content = response.content.strip()
@@ -349,7 +356,16 @@ class AsyncIntentRecognizer:
"""
try:
response = await self._llm.invoke_async(prompt, False, response_format={"type": "json_object"})
llm = ChatOpenAI(
api_key=APIKeyManager.get_api_key(),
openai_api_base=os.getenv("OPENAI_API_BASE"),
model_name=os.getenv("MODEL_NAME"),
temperature=TEMPERATURE,
top_p=TOP_P
)
response = await llm.ainvoke(prompt, response_format={"type": "json_object"})
response.content = response.content.strip()
clean_output = re.sub(r'<think>.*?</think>', '', response.content, flags=re.DOTALL)
parsed_output = JsonOutputParser().parse(clean_output)
@@ -389,8 +405,17 @@ class AsyncIntentRecognizer:
context=context)
# 解析输出
try:
llm = ChatOpenAI(
api_key=APIKeyManager.get_api_key(),
openai_api_base=os.getenv("OPENAI_API_BASE"),
model_name=os.getenv("MODEL_NAME"),
temperature=TEMPERATURE,
top_p=TOP_P
)
llm.with_structured_output(QueryRewrite)
# 异步调用LLM
response = await self._llm.invoke_async(formatted_prompt, False)
response = await llm.ainvoke(formatted_prompt)
response.content = response.content.strip()
clean_output = re.sub(r'<think>.*?</think>', '', response.content, flags=re.DOTALL)
parsed_output = query_rewrite_parser.parse(clean_output)
@@ -634,8 +659,18 @@ class AsyncIntentRecognizer:
previous_slots=json.dumps(previous_slots,ensure_ascii=False),
)
try:
llm = ChatOpenAI(
api_key=APIKeyManager.get_api_key(),
openai_api_base=os.getenv("OPENAI_API_BASE"),
model_name=os.getenv("MODEL_NAME"),
temperature=TEMPERATURE,
top_p=TOP_P
)
llm.with_structured_output(slot_model_class)
# 异步调用LLM
response = await self._llm.invoke_async(formatted_prompt, False)
response = await llm.ainvoke(formatted_prompt)
response.content = response.content.strip()
clean_output = re.sub(r'<think>.*?</think>', '', response.content, flags=re.DOTALL)
# 尝试解析LLM响应
@@ -669,10 +704,17 @@ class AsyncIntentRecognizer:
)
try:
llm = ChatOpenAI(
api_key=APIKeyManager.get_api_key(),
openai_api_base=os.getenv("OPENAI_API_BASE"),
model_name=os.getenv("MODEL_NAME"),
temperature=TEMPERATURE,
top_p=TOP_P
)
llm.with_structured_output(StepBackPrompt)
# 异步调用LLM
response = await self._llm.invoke_async(formatted_prompt, False)
# 解析输出
response = await llm.ainvoke(formatted_prompt)
response.content = response.content.strip()
clean_output = re.sub(r'<think>.*?</think>', '', response.content, flags=re.DOTALL)
parsed_output = step_back_parser.parse(clean_output)
@@ -728,9 +770,18 @@ class AsyncIntentRecognizer:
"""
try:
llm = ChatOpenAI(
api_key=APIKeyManager.get_api_key(),
openai_api_base=os.getenv("OPENAI_API_BASE"),
model_name=os.getenv("MODEL_NAME"),
temperature=TEMPERATURE,
top_p=TOP_P,
)
# 异步调用LLM
start_time = time.time()
response = await self._llm.invoke_async(prompt, False, response_format={"type": "json_object"})
response = await llm.ainvoke(prompt, response_format={"type": "json_object"})
end_time = time.time()
# 解析JSON响应