前几天不是发布了flash 的api接入嘛? 但是这种方式造成不能够兼容的问题
;在我的模块下还有一位大佬说没有办法很好的兼容其他api ![]()
哈哈哈,喜欢一句话,没有条件可以创造条件。
(骗你的,其实是我自己想 做
)
官方文档压压惊
subcommand的自定义:
提问:
第一步请开启你的计划模式欸!!!
中间稍微迭代一下
哈哈哈,那就类似于大模型开发的api调用原理:
当然此处的内容会更加的全面。
但是有一个想法,那就是利用openai兼容的api的python脚本进行调用大模型
为了能够更加合适的方式进行脚本上传,增加一个.env文件进行储存api和url;
subcommand
哈哈哈,subcommand已经在上传了,官方审核中;
以下是python代码
‘’'python
“”"
.env文件解析和LLM API调用脚本
支持从.env文件中读取API配置并调用OpenAI兼容的LLM API
“”"
import os
import json
import requests
from pathlib import Path
def parse_env_file(env_path):
“”"
解析.env文件,返回配置字典
Args:
env_path: .env文件的路径
Returns:
dict: 包含所有配置的字典
"""
config = {}
if not os.path.exists(env_path):
print("错误:.env文件不存在")
return config
with open(env_path, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip()
# 跳过注释和空行
if not line or line.startswith('#'):
continue
# 解析键值对
if '=' in line:
key, value = line.split('=', 1)
key = key.strip()
value = value.strip()
# 去除引号
if value.startswith('"') and value.endswith('"'):
value = value[1:-1]
elif value.startswith("'") and value.endswith("'"):
value = value[1:-1]
config[key] = value
return config
def call_llm_api(api_url, api_key, model, user_message):
“”"
调用LLM API(OpenAI兼容格式)
Args:
api_url: API端点URL
api_key: API密钥
model: 模型名称
user_message: 用户输入的消息
Returns:
str: API的响应内容
"""
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
payload = {
"model": model,
"messages": [
{"role": "user", "content": user_message}
]
}
try:
response = requests.post(api_url, headers=headers, json=payload, timeout=60)
response.raise_for_status()
result = response.json()
# 尝试提取回复内容
if 'choices' in result and len(result['choices']) > 0:
return result['choices'][0]['message']['content']
else:
return f"API返回格式异常:{json.dumps(result, ensure_ascii=False, indent=2)}"
except requests.exceptions.RequestException as e:
return f"API调用失败:{str(e)}"
except json.JSONDecodeError as e:
return f"响应解析失败:{str(e)}"
except Exception as e:
return f"发生错误:{str(e)}"
def main():
“”“主程序”“”
# 获取.env文件路径
env_path = Path(file).parent.parent / ‘.env’
# 解析.env文件
config = parse_env_file(env_path)
# 检查必需的配置
if 'API_URL' not in config:
print("错误:.env文件中未找到API_URL配置")
exit(1)
if 'api_key' not in config:
print("错误:.env文件中未找到api_key配置")
exit(1)
if 'model' not in config:
print("错误:.env文件中未找到model配置")
exit(1)
api_url = config['API_URL']
api_key = config['api_key']
model = config['model']
# 从命令行参数或环境变量获取用户输入
user_message = os.getenv('USER_MESSAGE', '')
if not user_message:
print("错误:未提供用户输入")
exit(1)
# 调用API
response = call_llm_api(api_url, api_key, model, user_message)
print(response)
if name == “main”:
main()
‘’’
env.文件格式:
嘿嘿,晚安!!!
![]()


