Skip to content

Commit

Permalink
Merge pull request #26 from mayaCostantini/openai-wrapper
Browse files Browse the repository at this point in the history
Add example OpenAI wrapper analytic for ranking suspicious processes
  • Loading branch information
subbyte authored May 28, 2024
2 parents f740562 + 41b267a commit 78236ab
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
10 changes: 10 additions & 0 deletions analytics/openai-suspicious-processes/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM python:3.9

RUN pip install --upgrade pip && \
pip install --no-cache-dir pandas openai

WORKDIR /opt/analytics

ADD analytics.py /opt/analytics/

CMD ["python3", "analytics.py"]
20 changes: 20 additions & 0 deletions analytics/openai-suspicious-processes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# OpenAI suspicious processes ranking

## Goal

Analyze a list of potentially suspicious processes by prompting an OpenAI model.

## Usage

The example `PROMPT` in this analytic asks ChatGPT (`gpt-3.5-turbo` version) to rank the processes provided in the input dataframe and
to give explanations for the top 10 suspicious processes.

## Example

Assume you have a variable `procs` with `process` entities. You can prompt the model to rank the processes with the following command:

```
APPLY python://openai-suspicious-processes ON procs
```

The `OPENAI_API_KEY` variable must be provided in the huntbook environment.
46 changes: 46 additions & 0 deletions analytics/openai-suspicious-processes/analytics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env python3

import os
import pandas as pd

from openai import OpenAI


OPENAI_MODEL = "gpt-3.5-turbo"

PROMPT = """
The following dataframe contains information about different processes running on a system: {}.
Rank those processes by suspicioussness and give an explanation for the top 10.
Focus on the `name` and `command_line` attributes of the processes.
"""

client = OpenAI(
api_key=os.environ.get("OPENAI_API_KEY"),
)


def analytics(df):
"""
Given a prompt and process information in the dataframe,
rank the processes by suspiciousness and give an explanation
for the top 10 suspicious processes.
"""

complete_prompt = PROMPT.format(df.to_json())

chat_completion = client.chat.completions.create(
messages=[
{
"role": "user",
"content": complete_prompt,
}
],
model=OPENAI_MODEL,
)

display = (
f"<p><b>Prompt:</b> {PROMPT.format(df)} </p>"
f"<p><b>Answer:</b> {chat_completion.choices[0].message.content}</p>"
)

return df, display
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 78236ab

Please sign in to comment.