logdetective 0.2.5__tar.gz → 0.2.7__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {logdetective-0.2.5 → logdetective-0.2.7}/PKG-INFO +19 -3
- {logdetective-0.2.5 → logdetective-0.2.7}/README.md +17 -2
- {logdetective-0.2.5 → logdetective-0.2.7}/logdetective/constants.py +14 -1
- {logdetective-0.2.5 → logdetective-0.2.7}/logdetective/logdetective.py +30 -6
- logdetective-0.2.7/logdetective/server.py +192 -0
- {logdetective-0.2.5 → logdetective-0.2.7}/logdetective/utils.py +53 -8
- {logdetective-0.2.5 → logdetective-0.2.7}/pyproject.toml +2 -1
- logdetective-0.2.5/logdetective/server.py +0 -56
- {logdetective-0.2.5 → logdetective-0.2.7}/LICENSE +0 -0
- {logdetective-0.2.5 → logdetective-0.2.7}/logdetective/__init__.py +0 -0
- {logdetective-0.2.5 → logdetective-0.2.7}/logdetective/drain3.ini +0 -0
- {logdetective-0.2.5 → logdetective-0.2.7}/logdetective/extractors.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: logdetective
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.7
|
|
4
4
|
Summary: Log using LLM AI to search for build/test failures and provide ideas for fixing these.
|
|
5
5
|
License: Apache-2.0
|
|
6
6
|
Author: Jiri Podivin
|
|
@@ -21,6 +21,7 @@ Provides-Extra: server
|
|
|
21
21
|
Requires-Dist: drain3 (>=0.9.11,<0.10.0)
|
|
22
22
|
Requires-Dist: huggingface-hub (>0.23.2)
|
|
23
23
|
Requires-Dist: llama-cpp-python (>=0.2.56,<0.3.0,!=0.2.86)
|
|
24
|
+
Requires-Dist: numpy (>=1.26.0,<2.0.0)
|
|
24
25
|
Requires-Dist: requests (>=2.31.0,<3.0.0)
|
|
25
26
|
Project-URL: homepage, https://github.com/fedora-copr/logdetective
|
|
26
27
|
Project-URL: issues, https://github.com/fedora-copr/logdetective/issues
|
|
@@ -38,7 +39,7 @@ A Python tool to analyze logs using a Language Model (LLM) and Drain template mi
|
|
|
38
39
|
Installation
|
|
39
40
|
------------
|
|
40
41
|
|
|
41
|
-
**
|
|
42
|
+
**Fedora 40+**
|
|
42
43
|
|
|
43
44
|
dnf install logdetective
|
|
44
45
|
|
|
@@ -83,6 +84,10 @@ Example you want to use a different model:
|
|
|
83
84
|
logdetective https://example.com/logs.txt --model https://huggingface.co/QuantFactory/Meta-Llama-3-8B-Instruct-GGUF/resolve/main/Meta-Llama-3-8B-Instruct.Q5_K_S.gguf?download=true
|
|
84
85
|
logdetective https://example.com/logs.txt --model QuantFactory/Meta-Llama-3-8B-Instruct-GGUF
|
|
85
86
|
|
|
87
|
+
Note that streaming with some models (notably Meta-Llama-3 is broken) is broken and can be workarounded by `no-stream` option:
|
|
88
|
+
|
|
89
|
+
logdetective https://example.com/logs.txt --model QuantFactory/Meta-Llama-3-8B-Instruct-GGUF --no-stream
|
|
90
|
+
|
|
86
91
|
|
|
87
92
|
Real Example
|
|
88
93
|
------------
|
|
@@ -181,7 +186,7 @@ or
|
|
|
181
186
|
Server
|
|
182
187
|
------
|
|
183
188
|
|
|
184
|
-
FastApi based server is implemented in `logdetective/server.py`. In order to run in a development mode,
|
|
189
|
+
FastApi based server is implemented in `logdetective/server.py`. In order to run it in a development mode,
|
|
185
190
|
simply start llama-cpp-python server with your chosen model as described in llama-cpp-python [docs](https://llama-cpp-python.readthedocs.io/en/latest/server/#running-the-server).
|
|
186
191
|
|
|
187
192
|
Afterwards, start the logdetective server with `fastapi dev logdetective/server.py --port 8080`.
|
|
@@ -189,6 +194,17 @@ Requests can then be made with post requests, for example:
|
|
|
189
194
|
|
|
190
195
|
curl --header "Content-Type: application/json" --request POST --data '{"url":"<YOUR_URL_HERE>"}' http://localhost:8080/analyze
|
|
191
196
|
|
|
197
|
+
We also have a Containerfile and composefile to run the logdetective server and llama server in containers.
|
|
198
|
+
|
|
199
|
+
Before doing `podman-compose up`, make sure to set `MODELS_PATH` environment variable and point to a directory with your local model files:
|
|
200
|
+
```
|
|
201
|
+
$ export MODELS_PATH=/path/to/models/
|
|
202
|
+
$ ll $MODELS_PATH
|
|
203
|
+
-rw-r--r--. 1 tt tt 3.9G apr 10 17:18 mistral-7b-instruct-v0.2.Q4_K_S.gguf
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
If the variable is not set, `./models` is mounted inside by default.
|
|
207
|
+
|
|
192
208
|
|
|
193
209
|
License
|
|
194
210
|
-------
|
|
@@ -10,7 +10,7 @@ A Python tool to analyze logs using a Language Model (LLM) and Drain template mi
|
|
|
10
10
|
Installation
|
|
11
11
|
------------
|
|
12
12
|
|
|
13
|
-
**
|
|
13
|
+
**Fedora 40+**
|
|
14
14
|
|
|
15
15
|
dnf install logdetective
|
|
16
16
|
|
|
@@ -55,6 +55,10 @@ Example you want to use a different model:
|
|
|
55
55
|
logdetective https://example.com/logs.txt --model https://huggingface.co/QuantFactory/Meta-Llama-3-8B-Instruct-GGUF/resolve/main/Meta-Llama-3-8B-Instruct.Q5_K_S.gguf?download=true
|
|
56
56
|
logdetective https://example.com/logs.txt --model QuantFactory/Meta-Llama-3-8B-Instruct-GGUF
|
|
57
57
|
|
|
58
|
+
Note that streaming with some models (notably Meta-Llama-3 is broken) is broken and can be workarounded by `no-stream` option:
|
|
59
|
+
|
|
60
|
+
logdetective https://example.com/logs.txt --model QuantFactory/Meta-Llama-3-8B-Instruct-GGUF --no-stream
|
|
61
|
+
|
|
58
62
|
|
|
59
63
|
Real Example
|
|
60
64
|
------------
|
|
@@ -153,7 +157,7 @@ or
|
|
|
153
157
|
Server
|
|
154
158
|
------
|
|
155
159
|
|
|
156
|
-
FastApi based server is implemented in `logdetective/server.py`. In order to run in a development mode,
|
|
160
|
+
FastApi based server is implemented in `logdetective/server.py`. In order to run it in a development mode,
|
|
157
161
|
simply start llama-cpp-python server with your chosen model as described in llama-cpp-python [docs](https://llama-cpp-python.readthedocs.io/en/latest/server/#running-the-server).
|
|
158
162
|
|
|
159
163
|
Afterwards, start the logdetective server with `fastapi dev logdetective/server.py --port 8080`.
|
|
@@ -161,6 +165,17 @@ Requests can then be made with post requests, for example:
|
|
|
161
165
|
|
|
162
166
|
curl --header "Content-Type: application/json" --request POST --data '{"url":"<YOUR_URL_HERE>"}' http://localhost:8080/analyze
|
|
163
167
|
|
|
168
|
+
We also have a Containerfile and composefile to run the logdetective server and llama server in containers.
|
|
169
|
+
|
|
170
|
+
Before doing `podman-compose up`, make sure to set `MODELS_PATH` environment variable and point to a directory with your local model files:
|
|
171
|
+
```
|
|
172
|
+
$ export MODELS_PATH=/path/to/models/
|
|
173
|
+
$ ll $MODELS_PATH
|
|
174
|
+
-rw-r--r--. 1 tt tt 3.9G apr 10 17:18 mistral-7b-instruct-v0.2.Q4_K_S.gguf
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
If the variable is not set, `./models` is mounted inside by default.
|
|
178
|
+
|
|
164
179
|
|
|
165
180
|
License
|
|
166
181
|
-------
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
|
|
2
2
|
# pylint: disable=line-too-long
|
|
3
|
-
DEFAULT_ADVISOR = "
|
|
3
|
+
DEFAULT_ADVISOR = "fedora-copr/Mistral-7B-Instruct-v0.2-GGUF"
|
|
4
4
|
|
|
5
5
|
PROMPT_TEMPLATE = """
|
|
6
6
|
Given following log snippets, and nothing else, explain what failure, if any, occured during build of this package.
|
|
@@ -30,3 +30,16 @@ Log:
|
|
|
30
30
|
Answer:
|
|
31
31
|
|
|
32
32
|
"""
|
|
33
|
+
|
|
34
|
+
SNIPPET_PROMPT_TEMPLATE = """
|
|
35
|
+
Analyse following RPM build log snippet.
|
|
36
|
+
Analysis of the snippets must be in a format of [X] : [Y], where [X] is a log snippet, and [Y] is the explanation.
|
|
37
|
+
Snippets themselves must not be altered in any way whatsoever.
|
|
38
|
+
|
|
39
|
+
Snippet:
|
|
40
|
+
|
|
41
|
+
{}
|
|
42
|
+
|
|
43
|
+
Analysis:
|
|
44
|
+
|
|
45
|
+
"""
|
|
@@ -3,14 +3,14 @@ import logging
|
|
|
3
3
|
import sys
|
|
4
4
|
|
|
5
5
|
from logdetective.constants import DEFAULT_ADVISOR
|
|
6
|
-
from logdetective.utils import
|
|
6
|
+
from logdetective.utils import (
|
|
7
|
+
process_log, initialize_model, retrieve_log_content, format_snippets, compute_certainty)
|
|
7
8
|
from logdetective.extractors import LLMExtractor, DrainExtractor
|
|
8
9
|
|
|
9
10
|
LOG = logging.getLogger("logdetective")
|
|
10
11
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
"""Main execution function."""
|
|
12
|
+
def setup_args():
|
|
13
|
+
""" Setup argument parser and return arguments. """
|
|
14
14
|
parser = argparse.ArgumentParser("logdetective")
|
|
15
15
|
parser.add_argument("file", type=str,
|
|
16
16
|
default="", help="The URL or path to the log file to be analyzed.")
|
|
@@ -21,6 +21,7 @@ def main():
|
|
|
21
21
|
help="Suffix of the model file name to be retrieved from Hugging Face.\
|
|
22
22
|
Makes sense only if the model is specified with Hugging Face name.",
|
|
23
23
|
default="Q4_K_S.gguf")
|
|
24
|
+
parser.add_argument("-n", "--no-stream", action='store_true')
|
|
24
25
|
parser.add_argument("-S", "--summarizer", type=str, default="drain",
|
|
25
26
|
help="Choose between LLM and Drain template miner as the log summarizer.\
|
|
26
27
|
LLM must be specified as path to a model, URL or local file.")
|
|
@@ -32,7 +33,12 @@ def main():
|
|
|
32
33
|
This only makes sense when you are summarizing with Drain")
|
|
33
34
|
parser.add_argument("-v", "--verbose", action='count', default=0)
|
|
34
35
|
parser.add_argument("-q", "--quiet", action='store_true')
|
|
35
|
-
|
|
36
|
+
return parser.parse_args()
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def main():
|
|
40
|
+
"""Main execution function."""
|
|
41
|
+
args = setup_args()
|
|
36
42
|
|
|
37
43
|
if args.verbose and args.quiet:
|
|
38
44
|
sys.stderr.write("Error: --quiet and --verbose is mutually exclusive.\n")
|
|
@@ -83,7 +89,25 @@ def main():
|
|
|
83
89
|
log_summary = format_snippets(log_summary)
|
|
84
90
|
LOG.info("Log summary: \n %s", log_summary)
|
|
85
91
|
|
|
86
|
-
|
|
92
|
+
stream = True
|
|
93
|
+
if args.no_stream:
|
|
94
|
+
stream = False
|
|
95
|
+
response = process_log(log_summary, model, stream)
|
|
96
|
+
probs = []
|
|
97
|
+
print("Explanation:")
|
|
98
|
+
if args.no_stream:
|
|
99
|
+
print(response["choices"][0]["text"])
|
|
100
|
+
probs = response["choices"][0]["logprobs"]["top_logprobs"]
|
|
101
|
+
else:
|
|
102
|
+
# Stream the output
|
|
103
|
+
for chunk in response:
|
|
104
|
+
if isinstance(chunk["choices"][0]["logprobs"], dict):
|
|
105
|
+
probs.extend(chunk["choices"][0]["logprobs"]["top_logprobs"])
|
|
106
|
+
delta = chunk['choices'][0]['text']
|
|
107
|
+
print(delta, end='', flush=True)
|
|
108
|
+
certainty = compute_certainty(probs)
|
|
109
|
+
|
|
110
|
+
print(f"\nResponse certainty: {certainty:.2f}%\n")
|
|
87
111
|
|
|
88
112
|
|
|
89
113
|
if __name__ == "__main__":
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import logging
|
|
3
|
+
import os
|
|
4
|
+
from typing import List
|
|
5
|
+
|
|
6
|
+
from llama_cpp import CreateCompletionResponse
|
|
7
|
+
from fastapi import FastAPI, HTTPException
|
|
8
|
+
from pydantic import BaseModel
|
|
9
|
+
|
|
10
|
+
import requests
|
|
11
|
+
|
|
12
|
+
from logdetective.constants import PROMPT_TEMPLATE, SNIPPET_PROMPT_TEMPLATE
|
|
13
|
+
from logdetective.extractors import DrainExtractor
|
|
14
|
+
from logdetective.utils import validate_url, compute_certainty
|
|
15
|
+
|
|
16
|
+
class BuildLog(BaseModel):
|
|
17
|
+
"""Model of data submitted to API.
|
|
18
|
+
"""
|
|
19
|
+
url: str
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class Response(BaseModel):
|
|
23
|
+
"""Model of data returned by Log Detective API
|
|
24
|
+
|
|
25
|
+
explanation: CreateCompletionResponse
|
|
26
|
+
https://llama-cpp-python.readthedocs.io/en/latest/api-reference/#llama_cpp.llama_types.CreateCompletionResponse
|
|
27
|
+
response_certainty: float
|
|
28
|
+
"""
|
|
29
|
+
explanation: CreateCompletionResponse
|
|
30
|
+
response_certainty: float
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class StagedResponse(Response):
|
|
34
|
+
"""Model of data returned by Log Detective API when called when staged response
|
|
35
|
+
is requested. Contains list of reponses to prompts for individual snippets.
|
|
36
|
+
|
|
37
|
+
explanation: CreateCompletionResponse
|
|
38
|
+
https://llama-cpp-python.readthedocs.io/en/latest/api-reference/#llama_cpp.llama_types.CreateCompletionResponse
|
|
39
|
+
response_certainty: float
|
|
40
|
+
snippets: list of CreateCompletionResponse
|
|
41
|
+
"""
|
|
42
|
+
snippets: List[CreateCompletionResponse]
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
LOG = logging.getLogger("logdetective")
|
|
46
|
+
|
|
47
|
+
app = FastAPI()
|
|
48
|
+
|
|
49
|
+
LLM_CPP_HOST = os.environ.get("LLAMA_CPP_HOST", "localhost")
|
|
50
|
+
LLM_CPP_SERVER_ADDRESS = f"http://{LLM_CPP_HOST}"
|
|
51
|
+
LLM_CPP_SERVER_PORT = os.environ.get("LLAMA_CPP_SERVER_PORT", 8000)
|
|
52
|
+
LLM_CPP_SERVER_TIMEOUT = os.environ.get("LLAMA_CPP_SERVER_TIMEOUT", 600)
|
|
53
|
+
LOG_SOURCE_REQUEST_TIMEOUT = os.environ.get("LOG_SOURCE_REQUEST_TIMEOUT", 60)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def process_url(url: str) -> str:
|
|
57
|
+
"""Validate log URL and return log text.
|
|
58
|
+
"""
|
|
59
|
+
if validate_url(url=url):
|
|
60
|
+
try:
|
|
61
|
+
log_request = requests.get(url, timeout=int(LOG_SOURCE_REQUEST_TIMEOUT))
|
|
62
|
+
except requests.RequestException as ex:
|
|
63
|
+
raise HTTPException(
|
|
64
|
+
status_code=400,
|
|
65
|
+
detail=f"We couldn't obtain the logs: {ex}") from ex
|
|
66
|
+
|
|
67
|
+
if not log_request.ok:
|
|
68
|
+
raise HTTPException(status_code=400,
|
|
69
|
+
detail="Something went wrong while getting the logs: "
|
|
70
|
+
f"[{log_request.status_code}] {log_request.text}")
|
|
71
|
+
else:
|
|
72
|
+
LOG.error("Invalid URL received ")
|
|
73
|
+
raise HTTPException(status_code=400,
|
|
74
|
+
detail=f"Invalid log URL: {url}")
|
|
75
|
+
|
|
76
|
+
return log_request.text
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def mine_logs(log: str) -> List[str]:
|
|
80
|
+
"""Extract snippets from log text
|
|
81
|
+
"""
|
|
82
|
+
extractor = DrainExtractor(verbose=True, context=True, max_clusters=8)
|
|
83
|
+
|
|
84
|
+
LOG.info("Getting summary")
|
|
85
|
+
log_summary = extractor(log)
|
|
86
|
+
|
|
87
|
+
ratio = len(log_summary) / len(log.split('\n'))
|
|
88
|
+
LOG.debug("Log summary: \n %s", log_summary)
|
|
89
|
+
LOG.info("Compression ratio: %s", ratio)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
return log_summary
|
|
93
|
+
|
|
94
|
+
def submit_text(text: str, max_tokens: int = 0, log_probs: int = 1):
|
|
95
|
+
"""Submit prompt to LLM.
|
|
96
|
+
max_tokens: number of tokens to be produces, 0 indicates run until encountering EOS
|
|
97
|
+
log_probs: number of token choices to produce log probs for
|
|
98
|
+
"""
|
|
99
|
+
LOG.info("Analyzing the text")
|
|
100
|
+
data = {
|
|
101
|
+
"prompt": text,
|
|
102
|
+
"max_tokens": str(max_tokens),
|
|
103
|
+
"logprobs": str(log_probs)}
|
|
104
|
+
|
|
105
|
+
try:
|
|
106
|
+
# Expects llama-cpp server to run on LLM_CPP_SERVER_ADDRESS:LLM_CPP_SERVER_PORT
|
|
107
|
+
response = requests.post(
|
|
108
|
+
f"{LLM_CPP_SERVER_ADDRESS}:{LLM_CPP_SERVER_PORT}/v1/completions",
|
|
109
|
+
headers={"Content-Type":"application/json"},
|
|
110
|
+
data=json.dumps(data),
|
|
111
|
+
timeout=int(LLM_CPP_SERVER_TIMEOUT))
|
|
112
|
+
except requests.RequestException as ex:
|
|
113
|
+
raise HTTPException(
|
|
114
|
+
status_code=400,
|
|
115
|
+
detail=f"Llama-cpp query failed: {ex}") from ex
|
|
116
|
+
|
|
117
|
+
if not response.ok:
|
|
118
|
+
raise HTTPException(
|
|
119
|
+
status_code=400,
|
|
120
|
+
detail="Something went wrong while getting a response from the llama server: "
|
|
121
|
+
f"[{response.status_code}] {response.text}")
|
|
122
|
+
try:
|
|
123
|
+
response = json.loads(response.text)
|
|
124
|
+
except UnicodeDecodeError as ex:
|
|
125
|
+
LOG.error("Error encountered while parsing llama server response: %s", ex)
|
|
126
|
+
raise HTTPException(
|
|
127
|
+
status_code=400,
|
|
128
|
+
detail=f"Couldn't parse the response.\nError: {ex}\nData: {response.text}") from ex
|
|
129
|
+
|
|
130
|
+
return CreateCompletionResponse(response)
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
@app.post("/analyze", response_model=Response)
|
|
134
|
+
async def analyze_log(build_log: BuildLog):
|
|
135
|
+
"""Provide endpoint for log file submission and analysis.
|
|
136
|
+
Request must be in form {"url":"<YOUR_URL_HERE>"}.
|
|
137
|
+
URL must be valid for the request to be passed to the LLM server.
|
|
138
|
+
Meaning that it must contain appropriate scheme, path and netloc,
|
|
139
|
+
while lacking result, params or query fields.
|
|
140
|
+
"""
|
|
141
|
+
log_text = process_url(build_log.url)
|
|
142
|
+
log_summary = mine_logs(log_text)
|
|
143
|
+
response = submit_text(PROMPT_TEMPLATE.format(log_summary))
|
|
144
|
+
|
|
145
|
+
if "logprobs" in response["choices"][0]:
|
|
146
|
+
try:
|
|
147
|
+
certainty = compute_certainty(
|
|
148
|
+
response["choices"][0]["logprobs"]["top_logprobs"])
|
|
149
|
+
except ValueError as ex:
|
|
150
|
+
LOG.error("Error encountered while computing certainty: %s", ex)
|
|
151
|
+
raise HTTPException(
|
|
152
|
+
status_code=400,
|
|
153
|
+
detail=f"Couldn't compute certainty with data:\n"
|
|
154
|
+
f"{response["choices"][0]["logprobs"]["top_logprobs"]}") from ex
|
|
155
|
+
|
|
156
|
+
return Response(explanation=response, response_certainty=certainty)
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
@app.post("/analyze/staged", response_model=StagedResponse)
|
|
160
|
+
async def analyze_log_staged(build_log: BuildLog):
|
|
161
|
+
"""Provide endpoint for log file submission and analysis.
|
|
162
|
+
Request must be in form {"url":"<YOUR_URL_HERE>"}.
|
|
163
|
+
URL must be valid for the request to be passed to the LLM server.
|
|
164
|
+
Meaning that it must contain appropriate scheme, path and netloc,
|
|
165
|
+
while lacking result, params or query fields.
|
|
166
|
+
"""
|
|
167
|
+
log_text = process_url(build_log.url)
|
|
168
|
+
log_summary = mine_logs(log_text)
|
|
169
|
+
|
|
170
|
+
analyzed_snippets = []
|
|
171
|
+
|
|
172
|
+
for snippet in log_summary:
|
|
173
|
+
response = submit_text(SNIPPET_PROMPT_TEMPLATE.format(snippet))
|
|
174
|
+
analyzed_snippets.append(response)
|
|
175
|
+
|
|
176
|
+
final_analysis = submit_text(
|
|
177
|
+
PROMPT_TEMPLATE.format([e["choices"][0]["text"] for e in analyzed_snippets]))
|
|
178
|
+
|
|
179
|
+
certainty = 0
|
|
180
|
+
if "logprobs" in final_analysis["choices"][0]:
|
|
181
|
+
try:
|
|
182
|
+
certainty = compute_certainty(
|
|
183
|
+
final_analysis["choices"][0]["logprobs"]["top_logprobs"])
|
|
184
|
+
except ValueError as ex:
|
|
185
|
+
LOG.error("Error encountered while computing certainty: %s", ex)
|
|
186
|
+
raise HTTPException(
|
|
187
|
+
status_code=400,
|
|
188
|
+
detail=f"Couldn't compute certainty with data:\n"
|
|
189
|
+
f"{final_analysis["choices"][0]["logprobs"]["top_logprobs"]}") from ex
|
|
190
|
+
|
|
191
|
+
return StagedResponse(
|
|
192
|
+
explanation=final_analysis, snippets=analyzed_snippets, response_certainty=certainty)
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import logging
|
|
2
2
|
import os
|
|
3
|
+
from typing import Iterator, List, Dict
|
|
3
4
|
from urllib.parse import urlparse
|
|
4
|
-
|
|
5
|
+
import numpy as np
|
|
5
6
|
import requests
|
|
6
7
|
|
|
7
|
-
from llama_cpp import Llama
|
|
8
|
+
from llama_cpp import Llama, CreateCompletionResponse, CreateCompletionStreamResponse
|
|
8
9
|
from logdetective.constants import PROMPT_TEMPLATE
|
|
9
10
|
|
|
10
11
|
|
|
@@ -53,24 +54,45 @@ def initialize_model(model_pth: str, filename_suffix: str = ".gguf", verbose: bo
|
|
|
53
54
|
filename_suffix (str): suffix of the model file name to be pulled from Hugging Face
|
|
54
55
|
verbose (bool): level of verbosity for llamacpp
|
|
55
56
|
"""
|
|
57
|
+
|
|
58
|
+
LOG.info("Loading model from %s", model_pth)
|
|
59
|
+
|
|
56
60
|
if os.path.isfile(model_pth):
|
|
57
61
|
model = Llama(
|
|
58
62
|
model_path=model_pth,
|
|
59
63
|
n_ctx=0, # Maximum context for the model
|
|
60
|
-
verbose=verbose
|
|
64
|
+
verbose=verbose,
|
|
65
|
+
logits_all=True)
|
|
61
66
|
else:
|
|
62
67
|
model = Llama.from_pretrained(
|
|
63
68
|
model_pth,
|
|
64
69
|
f"*{filename_suffix}",
|
|
65
70
|
n_ctx=0, # Maximum context for the model
|
|
66
|
-
verbose=verbose
|
|
71
|
+
verbose=verbose,
|
|
72
|
+
logits_all=True)
|
|
67
73
|
|
|
68
74
|
return model
|
|
69
75
|
|
|
70
76
|
|
|
71
|
-
def
|
|
77
|
+
def compute_certainty(probs: List[Dict[str, float] | None]) -> float:
|
|
78
|
+
"""Compute certainty of repsponse based on average logit probability.
|
|
79
|
+
Log probability is log(p), isn't really readable for most people, especially in compound.
|
|
80
|
+
In this case it's just a matter of applying inverse operation exp.
|
|
81
|
+
Of course that leaves you with a value in range <0, 1> so it needs to be multiplied by 100.
|
|
82
|
+
Simply put, this is the most straightforward way to get the numbers out.
|
|
72
83
|
"""
|
|
73
|
-
|
|
84
|
+
|
|
85
|
+
top_logprobs = [
|
|
86
|
+
np.exp(x) * 100 for e in probs if isinstance(e, dict) for x in e.values()]
|
|
87
|
+
certainty = np.median(top_logprobs, axis=0)
|
|
88
|
+
if np.isnan(certainty):
|
|
89
|
+
raise ValueError("NaN certainty of answer")
|
|
90
|
+
return certainty
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def process_log(log: str, model: Llama, stream: bool) -> (
|
|
94
|
+
CreateCompletionResponse | Iterator[CreateCompletionStreamResponse]):
|
|
95
|
+
"""Processes a given log using the provided language model and returns its summary.
|
|
74
96
|
|
|
75
97
|
Args:
|
|
76
98
|
log (str): The input log to be processed.
|
|
@@ -79,11 +101,19 @@ def process_log(log: str, model: Llama) -> str:
|
|
|
79
101
|
Returns:
|
|
80
102
|
str: The summary of the given log generated by the language model.
|
|
81
103
|
"""
|
|
82
|
-
|
|
104
|
+
response = model(
|
|
105
|
+
prompt=PROMPT_TEMPLATE.format(log),
|
|
106
|
+
stream=stream,
|
|
107
|
+
max_tokens=0,
|
|
108
|
+
logprobs=1)
|
|
109
|
+
|
|
110
|
+
return response
|
|
83
111
|
|
|
84
112
|
|
|
85
113
|
def retrieve_log_content(log_path: str) -> str:
|
|
86
|
-
"""Get content of the file on the log_path path.
|
|
114
|
+
"""Get content of the file on the log_path path.
|
|
115
|
+
Path is assumed to be valid URL if it has a scheme.
|
|
116
|
+
Otherwise it attempts to pull it from local filesystem."""
|
|
87
117
|
parsed_url = urlparse(log_path)
|
|
88
118
|
log = ""
|
|
89
119
|
|
|
@@ -113,3 +143,18 @@ def format_snippets(snippets: list[str]) -> str:
|
|
|
113
143
|
================
|
|
114
144
|
"""
|
|
115
145
|
return summary
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def validate_url(url: str) -> bool:
|
|
149
|
+
"""Validate incoming URL to be at least somewhat sensible for log files
|
|
150
|
+
Only http and https protocols permitted. No result, params or query fields allowed.
|
|
151
|
+
Either netloc or path must have non-zero length.
|
|
152
|
+
"""
|
|
153
|
+
result = urlparse(url)
|
|
154
|
+
if result.scheme not in ['http', 'https']:
|
|
155
|
+
return False
|
|
156
|
+
if any([result.params, result.query, result.fragment]):
|
|
157
|
+
return False
|
|
158
|
+
if not (result.path or result.netloc):
|
|
159
|
+
return False
|
|
160
|
+
return True
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[tool.poetry]
|
|
2
2
|
name = "logdetective"
|
|
3
|
-
version = "0.2.
|
|
3
|
+
version = "0.2.7"
|
|
4
4
|
description = "Log using LLM AI to search for build/test failures and provide ideas for fixing these."
|
|
5
5
|
authors = ["Jiri Podivin <jpodivin@gmail.com>"]
|
|
6
6
|
license = "Apache-2.0"
|
|
@@ -31,6 +31,7 @@ requests = "^2.31.0"
|
|
|
31
31
|
llama-cpp-python = "^0.2.56,!=0.2.86"
|
|
32
32
|
drain3 = "^0.9.11"
|
|
33
33
|
huggingface-hub = ">0.23.2"
|
|
34
|
+
numpy = "^1.26.0"
|
|
34
35
|
|
|
35
36
|
[build-system]
|
|
36
37
|
requires = ["poetry-core"]
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import logging
|
|
2
|
-
import os
|
|
3
|
-
import json
|
|
4
|
-
|
|
5
|
-
from fastapi import FastAPI
|
|
6
|
-
from pydantic import BaseModel
|
|
7
|
-
|
|
8
|
-
import requests
|
|
9
|
-
|
|
10
|
-
from logdetective.constants import PROMPT_TEMPLATE
|
|
11
|
-
from logdetective.extractors import DrainExtractor
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
class BuildLog(BaseModel):
|
|
15
|
-
"""Model of data submitted to API.
|
|
16
|
-
"""
|
|
17
|
-
url: str
|
|
18
|
-
|
|
19
|
-
LOG = logging.getLogger("logdetective")
|
|
20
|
-
|
|
21
|
-
app = FastAPI()
|
|
22
|
-
|
|
23
|
-
LLM_CPP_SERVER_ADDRESS = os.environ.get("LLAMA_CPP_SERVER", " http://localhost")
|
|
24
|
-
LLM_CPP_SERVER_PORT = os.environ.get("LLAMA_CPP_SERVER_PORT", 8000)
|
|
25
|
-
LLM_CPP_SERVER_TIMEOUT = os.environ.get("LLAMA_CPP_SERVER_TIMEOUT", 600)
|
|
26
|
-
LOG_SOURCE_REQUEST_TIMEOUT = os.environ.get("LOG_SOURCE_REQUEST_TIMEOUT", 60)
|
|
27
|
-
|
|
28
|
-
@app.post("/analyze", )
|
|
29
|
-
async def analyze_log(build_log: BuildLog):
|
|
30
|
-
"""Provide endpoint for log file submission and analysis.
|
|
31
|
-
Request must be in form {"url":"<YOUR_URL_HERE>"}.
|
|
32
|
-
"""
|
|
33
|
-
extractor = DrainExtractor(verbose=True, context=True, max_clusters=8)
|
|
34
|
-
|
|
35
|
-
LOG.info("Getting summary")
|
|
36
|
-
|
|
37
|
-
log = requests.get(build_log.url, timeout=int(LOG_SOURCE_REQUEST_TIMEOUT)).text
|
|
38
|
-
log_summary = extractor(log)
|
|
39
|
-
|
|
40
|
-
ratio = len(log_summary) / len(log.split('\n'))
|
|
41
|
-
LOG.debug("Log summary: \n %s", log_summary)
|
|
42
|
-
LOG.info("Compression ratio: %s", ratio)
|
|
43
|
-
|
|
44
|
-
LOG.info("Analyzing the text")
|
|
45
|
-
data = {
|
|
46
|
-
"prompt": PROMPT_TEMPLATE.format(log_summary),
|
|
47
|
-
"max_tokens": "0"}
|
|
48
|
-
|
|
49
|
-
# Expects llama-cpp server to run on LLM_CPP_SERVER_ADDRESS:LLM_CPP_SERVER_PORT
|
|
50
|
-
response = requests.post(
|
|
51
|
-
f"{LLM_CPP_SERVER_ADDRESS}:{LLM_CPP_SERVER_PORT}/v1/completions",
|
|
52
|
-
headers={"Content-Type":"application/json"},
|
|
53
|
-
data=json.dumps(data),
|
|
54
|
-
timeout=int(LLM_CPP_SERVER_TIMEOUT))
|
|
55
|
-
|
|
56
|
-
return response.text
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|