hello

国内GPT中转配置方案

官网主页https://api.bianxie.ai/

软件/代码/插件接入使用便携AI聚合API需要两个数据:

  1. BASE_URL:有的软件里也叫API URL或者OPENAI_API_BASE,都是一个意思,就是说你从这个链接调用服务;
  2. API Key:就是令牌,以sk-开头的一长串字符。

客户端工具https://github.com/Dooy/chatgpt-web-midjourney-proxy

配置BASE_URL和API_KEY即可直接使用

GPT API使用参考模版

以一个调用LLM读取报文协议生成模糊测试初始化种子的demo为例

文件目录树:

1
2
3
4
5
6
7
8
❯ tree                          
.
├── config
│ ├── config.yaml
│ └── prompts
│ ├── generate_seeds.txt
│ └── packet
└── llm_generate_seeds.py

模版代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import openai
from typing import Dict
import yaml
import os
import logging
import traceback


class SeedsGenerator:
"""种子生成器,用于生成相关服务协议的初始化种子"""

def __init__(self, config_path: str):
"""
初始化种子生成器

Args:
config_path: 配置文件路径
"""

# 加载配置
with open(config_path, 'r', encoding='utf-8') as f:
self.config = yaml.safe_load(f)

# 设置OpenAI API
self.client = openai.OpenAI(
api_key=self.config['deepseek']['api_key'],
base_url=self.config['deepseek']['base_url']
)

# 加载提示词模板
prompt_dir = os.path.join(os.path.dirname(config_path), 'prompts')
with open(os.path.join(prompt_dir, 'packet'), 'rb') as f:
self.packet_prompt = f.read()

# 加载提示词模板
with open(os.path.join(prompt_dir, 'generate_seeds.txt'), 'r', encoding='utf-8') as f:
self.analyze_prompt = f.read()


# 设置日志记录器
self.logger = logging.getLogger('SeedsGenerator')
if not self.logger.handlers:
handler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
self.logger.addHandler(handler)
self.logger.setLevel(logging.INFO)

def generate_seeds(self) -> Dict:
"""
生成初始化种子

Returns:
初始化种子,保存在seeds目录下
"""
try:
# 构建提示词
prompt = self.analyze_prompt.format(
packet = self.packet_prompt
)

# 调用OpenAI API

self.logger.info("生成初始化种子...")
response = self.client.chat.completions.create(
model=self.config['deepseek']['model'],
messages=[
{"role": "system", "content": "你是一个专业的模糊测试专家。请严格按照指定格式输出结果。"},
{"role": "user", "content": prompt}
],
temperature=0.3
)

# 解析响应
result = response.choices[0].message.content

# 解析分析结果
if not os.path.exists("seeds"):
os.makedirs("seeds")

with open(f"seeds/seed_llm", 'wb') as f:
f.write(result.encode('utf-8'))
self.logger.info("初始化种子生成成功,保存在seeds/seed_llm")

except Exception as e:
self.logger.error(f"分析报文时发生错误: {str(e)}")
self.logger.error(traceback.format_exc())
return None

if __name__ == "__main__":
# 初始化种子生成器
generator = SeedsGenerator('./config/config.yaml')

# 生成初始化种子
seeds = generator.generate_seeds()

使用的话直接在config/config.yaml里添加各类参数,config/prompts添加提示词即可。

1
2
3
4
deepseek:
api_key: "sk-XXXXXXXXXXXXXXXXXXXXXXXX"
base_url: "https://api.deepseek.com"
model: "deepseek-chat"