-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrationale.py
61 lines (49 loc) · 1.54 KB
/
rationale.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from __future__ import annotations
import json
from dataclasses import asdict, dataclass
from typing import Literal
import openai
import sys
import openai
#from decouple import config
import os
from dotenv import load_dotenv
# Load environment variables from the .env file
load_dotenv()
openai.api_key = ""
OPENAI_MODEL = "gpt-3.5-turbo"
@dataclass
class Message:
role: Literal["system", "user", "assistant"]
content: str
@staticmethod
def from_response(response: dict) -> Message:
response["choices"][0]["message"]
return Message(**(response["choices"][0]["message"]))
def __str__(self) -> str:
return f"{self.role}: {self.content}"
class Transcript(list[Message]):
def raw(self) -> list[dict]:
return [asdict(m) for m in self]
def __str__(self) -> str:
return "\n".join([str(m) for m in self])
def chat_step(transcript_state: Transcript) -> Message:
chat_completion = openai.ChatCompletion.create(
model=OPENAI_MODEL, messages=transcript_state.raw()
)
if not isinstance(chat_completion, dict):
raise Exception(f"Unexpected response: {chat_completion}")
response_message = Message.from_response(chat_completion)
transcript_state.append(response_message)
return response_message
def openai_retinale(code: str) -> str:
prompt = f"""Explain the below moonscript code within 50 words:
{code}
"""
transcript = Transcript(
[
Message("user", prompt),
]
)
m = chat_step(transcript)
return m.content