Refactor code to deprecate text embedding

This commit is contained in:
2025-02-11 02:34:55 +08:00
parent 5467f72bd7
commit 32d82c99ec
17 changed files with 414 additions and 306 deletions

48
agents/CSA/__init__.py Normal file
View File

@@ -0,0 +1,48 @@
"""
===软件架构师===
用于分析项目的整体框架,抽取出清晰的项目结构和功能划分
"""
from langchain_core.messages import SystemMessage
from langchain_core.prompts import HumanMessagePromptTemplate, ChatPromptTemplate
from langchain_openai import ChatOpenAI
from langchain_community.callbacks.manager import get_openai_callback
from agents.CSA.prompt import CSA_SYSTEM_PROMPT, CSA_HUMAN_PROMPT
from logger import Logger
class CSA:
def __init__(self, base_url, api_key, model, process_output_callback):
# LLM配置
self.llm = ChatOpenAI(base_url=base_url, api_key=api_key, model=model)
# 提示词配置
self.system_prompt = CSA_SYSTEM_PROMPT
self.human_prompt = CSA_HUMAN_PROMPT
# 日志器配置
self.log = Logger(name='CSA', callback=process_output_callback)
def analyse(self, project_structure):
self.log.info('CSA开始分析项目模块')
# 提示词模板
self.llm_tmpl = ChatPromptTemplate.from_messages([
SystemMessage(content=self.system_prompt),
HumanMessagePromptTemplate.from_template(template=self.human_prompt),
])
# 调用链配置
self.llm_chain = self.llm_tmpl | self.llm
# 获取分析结果
with get_openai_callback() as cb:
result = self.llm_chain.invoke({'project_structure': project_structure})
# TODO: 接入token用量统计
# print(f"请求消耗的输入 token 数: {cb.prompt_tokens}")
# print(f"请求消耗的输出 token 数: {cb.completion_tokens}")
# print(f"请求总共消耗的 token 数: {cb.total_tokens}")
self.log.info('CSA完成分析项目模块')
return result.content

32
agents/CSA/prompt.py Normal file
View File

@@ -0,0 +1,32 @@
CSA_SYSTEM_PROMPT = """
You are a senior software architect, your responsibilities are:
The user provides you with the directory structure of the project, you need to analyze the project, summarize the project function division, and output the results in the following format (Markdown):
[Project name] Functional division
[Module number] [Module name]
- Package name: [Package name]
- Main function: [Main function]
- Absolute file path: [Absolute file path]
Emphasis:
1. The part wrapped in square brackets [] is what you need to fill in according to the actual situation, do not use square brackets when outputting;
2. One package (directory) uses one number;
3. The output absolute path refers to the absolute path of the source code file. All source code files in the same directory must be output;
For example:
HelloWorld Functional division
1 Configuration
- Package name: com.example.hello.config
- Main function: Web application configuration, including MVC and login interceptor.
- Absolute file path: C:/Users/yvling/Desktop/HelloWorld/src/main/java/com/example/hello/config.java
2 Controller
- Package name: com.example.hello.controller
- Main function: Demonstrating various common web security vulnerabilities through different controllers.
- Absolute file path: C:/Users/yvling/Desktop/HelloWorld/src/main/java/com/example/hello/controller.java
"""
CSA_HUMAN_PROMPT = """
The project directory structure provided by the user is as follows:
{project_structure}
Please start the analysis and output according to the format.
"""