-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathnam.py
90 lines (79 loc) · 2.46 KB
/
nam.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from openai import OpenAI
import os
from dotenv import load_dotenv
load_dotenv('.env')
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
# Set up OpenAI API credentials
client = OpenAI(
api_key=OPENAI_API_KEY
) # Replace with your actual OpenAI API key
# Define the input code
code_input = """
{
"flowchart": {
"start": {
"next": "check_condition"
},
"check_condition": {
"condition": "result == -1",
"true": "print_message_not_found",
"false": "print_message_found"
},
"print_message_not_found": {
"message": "Element not found",
"next": "end"
},
"print_message_found": {
"message": "Element found at index: result",
"next": "end"
},
"end": {}
}
}
"""
# Define the template code
temp_code = """
import React, { useCallback, useState } from "react";
import Flowchart from "flowchart-react";
import { ConnectionData, NodeData } from "flowchart-react/schema";
const App = () => {
// ... (Your existing template code here)
return (
<Flowchart
showToolbar={["start-end", "operation", "decision"]}
onChange={(nodes, connections) => {
setNodes(nodes);
setConns(connections);
}}
onDoubleClick={handleCreateNode}
style={{ width: 800, height: 600 }}
nodes={nodes}
connections={conns}
/>
);
};
export default App;
"""
# Define the prompt
prompt = f"""
This is the json format data for the flowchart to be created: {code_input}
This is the template code file that needs to be created as output: {temp_code}
Using the provided JSON data, please fill in the template code file with the appropriate code by replacing only the values of the flowchart to be generated.
Finally, print the code with the updated data.
"""
# Make the API call
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "system",
"content": "You are a powerful flowchart generator. You can replace the data and provide me the code for flowchart generation. You have to provide me, I believe in you!",
},
{"role": "user", "content": f"This is the data:\n{code_input}\n{temp_code}"},
],
# prompt=prompt,
# max_tokens=500 # Adjust as needed
)
# Check if the output is empty, if so, print the original temp_code
print(response.choices[0].message.content)
# if response['choices'] else temp_code)