agent.py
from typing import List
import networkx as nx
import ollama
from config import C
from models import SourceFile, CodeUnit
from prompt import PROMPT_AGENT_1, PROMPT_AGENT_2
from utils import parse_code_uint, gen_graph_by_codeunits
from loguru import logger
client = ollama.AsyncClient(host=C.ollama.base_url)
async def chat_completion_text(text:str, prompt:str):
messages=[]
messages.append({
"role":"system",
"content":prompt
})
messages.append({
"role":"user",
"content":text
})
res=await chat_completion_messages(messages)
return res
async def chat_completion_messages(messages: list[dict]) -> str:
try:
response = await client.chat(
model=C.ollama.model,
messages=messages
)
return response.message.content
except Exception as e:
raise Exception(f"Error from Ollama: {str(e)}")
#负责解析项目依赖,生成项目依赖图谱
async def agent_1(sourceFile:SourceFile)->List[nx.Graph]:
"""
使用语言模型解析代码中的依赖关系。
:param sourceFile:
:param code: 输入的 Python 代码字符串
:return: 解析后的依赖关系描述字符串
"""
response = await chat_completion_text(sourceFile.source_code,PROMPT_AGENT_1)
logger.debug(f"response==input_text:{response}")
res=parse_code_uint(code=sourceFile.source_code,path=sourceFile.path,name=sourceFile.name,input_text=response)
return res
async def agent_2(text:str)->str:
response = await chat_completion_text(text, PROMPT_AGENT_2)
return response
Ollama tried many models(qwen2.5:72b, qwen2.5:32b, qwen2.5-coder:32b, deepseek-r1:32b), but all of them reported this error
AiCodeAudit-main\utils\__init__.py", line 104, in parse_code_uint
raise ValueError("未找到 <输出单元> 标签或标签格式不正确",input_text)
error log:
2025-04-28T17:12:06.938067+0800 | DEBUG | input_text:The provided code is a set of C functions that deal with IP addresses and their operations. Here's a brief explanation of each function:
1. `addr_pton`: Converts an ASCII representation of an IP address to its binary form.
2. `addr_ntop`: Converts the binary form of an IP address to its ASCII representation.
3. `addr_pton_cidr`: Parses a CIDR notation (like 'x.x.x.x/y' or 'xxxx:yyyy::/z') and converts it into binary form along with mask length.
4. `addr_netmatch`: Checks if two IP addresses belong to the same subnet based on given mask length.
5. `addr_unicast_masklen`: Returns default unicast mask length for a specific address family.
6. `masklen_valid`: Validates if the given mask length is valid for the specified address family.
7. `addr_host_is_all0s`: Checks whether the host part of an IP address is all zeros according to the given mask length.
8. `_SA` and `_SSA` are macros used internally to cast between different socket address types (IPv4 and IPv6).
9. The remaining functions (`addr_netmask`, `addr_and`, `addr_cmp`, etc.) seem to be performing bitwise operations on IP addresses, comparing them, or creating masks.
These functions appear to form a library for handling various aspects of network programming involving IP addresses in both IPv4 and IPv6 formats. They ensure that IP addresses are correctly converted between their string representations and binary forms, manipulated (like performing bitwise AND), compared, and validated against certain criteria (like subnet membership or mask length validity).
Caused by:
|
match = re.search(r'<输出单元>\n(.*?)\n<输出单元>', input_text, re.DOTALL) |
|
if not match: |
|
raise ValueError("未找到 <输出单元> 标签或标签格式不正确",input_text) |
agent.py
Ollama tried many models(
qwen2.5:72b,qwen2.5:32b,qwen2.5-coder:32b,deepseek-r1:32b), but all of them reported this errorerror log:
Caused by:
AiCodeAudit/utils/__init__.py
Lines 99 to 101 in 4b3cf94