|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Modified version from https://github.com/R34LUS3R/GPT3-cli/blob/main/gpt.py |
| 3 | + |
| 4 | +import argparse |
| 5 | +import json |
| 6 | +import os |
| 7 | +import sys |
| 8 | +sys.path.append('/usr/lib/vmware/vsan/perfsvc/') |
| 9 | +import requests |
| 10 | +import xml.dom.minidom |
| 11 | + |
| 12 | +# Set default values for command-line arguments |
| 13 | +MODEL = "text-davinci-003" # model to use |
| 14 | +TOKEN_COUNT = 300 # number of tokens to generate |
| 15 | +TEMPERATURE = 0.4 # temperature |
| 16 | +TOP_P = 1 # top_p value |
| 17 | +FREQUENCY = 0.5 # frequency penalty |
| 18 | +PRESENCE = 0.5 # presence penalty |
| 19 | +ESXCLI_NS = "http://www.vmware.com/Products/ESX/5.0/esxcli/" |
| 20 | + |
| 21 | + |
| 22 | +def main(): |
| 23 | + """Main entry point.""" |
| 24 | + # Parse command-line arguments |
| 25 | + parser = argparse.ArgumentParser() |
| 26 | + parser.add_argument("prompt", nargs="+", default="", |
| 27 | + help="prompt to use as input for the GPT-3 model") |
| 28 | + args = parser.parse_args() |
| 29 | + |
| 30 | + # Get the OpenAI API key |
| 31 | + api_key = "FILL_ME_IN" |
| 32 | + |
| 33 | + # Set up the API request |
| 34 | + url = "https://api.openai.com/v1/completions" |
| 35 | + headers = { |
| 36 | + "Authorization": f"Bearer {api_key}", |
| 37 | + "Content-Type": "application/json", |
| 38 | + } |
| 39 | + data = { |
| 40 | + "model": MODEL, |
| 41 | + "prompt": ' '.join(args.prompt), |
| 42 | + "temperature": TEMPERATURE, |
| 43 | + "top_p": TOP_P, |
| 44 | + "frequency_penalty": FREQUENCY, |
| 45 | + "presence_penalty": PRESENCE, |
| 46 | + "max_tokens": TOKEN_COUNT, |
| 47 | + } |
| 48 | + |
| 49 | + # Send the API request |
| 50 | + response = requests.post(url, headers=headers, json=data) |
| 51 | + |
| 52 | + # Check for errors in the API response |
| 53 | + if response.status_code != 200: |
| 54 | + print("Error:", response.json()["error"]) |
| 55 | + return |
| 56 | + |
| 57 | + # Extract the text from the response |
| 58 | + text = response.json()["choices"][0]["text"] |
| 59 | + |
| 60 | + # Print the output |
| 61 | + # print(text) |
| 62 | + |
| 63 | + doc = xml.dom.minidom.Document() |
| 64 | + outputEl = doc.createElementNS(ESXCLI_NS, "output") |
| 65 | + outputEl.setAttribute("xmlns", ESXCLI_NS) |
| 66 | + doc.appendChild(outputEl) |
| 67 | + |
| 68 | + structEl = doc.createElementNS(ESXCLI_NS, "structure") |
| 69 | + structEl.setAttribute("typeName", "Result") |
| 70 | + |
| 71 | + fieldEl = doc.createElementNS(ESXCLI_NS, "field") |
| 72 | + fieldEl.setAttribute("name", "Answer") |
| 73 | + structEl.appendChild(fieldEl) |
| 74 | + |
| 75 | + boolEl = doc.createElementNS(ESXCLI_NS, "string") |
| 76 | + fieldEl.appendChild(boolEl) |
| 77 | + |
| 78 | + boolEl.appendChild(doc.createTextNode(text.strip())) |
| 79 | + |
| 80 | + outputEl.appendChild(structEl) |
| 81 | + |
| 82 | + print(doc.toxml()) |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == "__main__": |
| 86 | + main() |
0 commit comments