hello
国内GPT中转配置方案
官网主页:https://api.bianxie.ai/
软件/代码/插件接入使用便携AI聚合API需要两个数据:
- BASE_URL:有的软件里也叫API URL或者OPENAI_API_BASE,都是一个意思,就是说你从这个链接调用服务;
- 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) 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 ) 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"
|