Skip to content

Latest commit

 

History

History
54 lines (42 loc) · 1.57 KB

example-06-explain-code.md

File metadata and controls

54 lines (42 loc) · 1.57 KB

Explain code

Explain a complicated piece of code.

Example Usage

curl --location --request PUT 'https://api.openai.lawrencemcdaniel.com/examples/default-explain-code' \
--header 'x-api-key: YOUR-API-GATEWAY-KEY' \
--header 'Content-Type: application/json' \
--data '{"class Log:
    def __init__(self, path):
        dirname = os.path.dirname(path)
        os.makedirs(dirname, exist_ok=True)
        f = open(path, "a+")

        # Check that the file is newline-terminated
        size = os.path.getsize(path)
        if size > 0:
            f.seek(size - 1)
            end = f.read(1)
            if end != "\n":
                f.write("\n")
        self.f = f
        self.path = path

    def log(self, event):
        event["_event_id"] = str(uuid.uuid4())
        json.dump(event, self.f)
        self.f.write("\n")

    def state(self):
        state = {"complete": set(), "last": None}
        for line in open(self.path):
            event = json.loads(line)
            if event["type"] == "submit" and event["success"]:
                state["complete"].add(event["id"])
                state["last"] = event
        return state
"}'

Example results

Official Documentation

OpenAI Playground