API: openAI API (pip 安装openai 包)
AI模型:通义千问(需要开通百炼服务)
利用大模型来阅读和分析log
from pathlib import Path
import os
from openai import OpenAI
def clean_file():
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
)
files = client.files.list()
print(files)
for file in files.data:
client.files.delete(file.id)
def get_response():
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
)
# data.pdf 是一个示例文件
file_object_main = client.files.create(file=Path("log1.txt"), purpose="file-extract")
file_object_phy = client.files.create(file=Path("log2.txt"), purpose="file-extract")
completion = client.chat.completions.create(
model="qwen-long",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "system", "content": f"fileid://{file_object_main.id}"},
{"role": "system", "content": f"fileid://{file_object_phy.id}"},
{"role": "user", "content": "请结合2份log上的时间戳,分析出错的原因。"},
],
)
client.files.delete(file_object_phy.id)
client.files.delete(file_object_main.id)
return completion.choices[0].message.content.strip()
if __name__ == '__main__':
clean_file()
response = get_response()
print(response)