-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
80 lines (69 loc) · 1.81 KB
/
Copy pathapp.py
File metadata and controls
80 lines (69 loc) · 1.81 KB
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
import os
from dotenv import load_dotenv
import streamlit as st
import openai
t_in = """foo
bar
"""
g_out = """Car
break
startEngine
Truck
move
stop
"""
t_out="""class foo{
public void bar();
}
"""
def get_completion(prompt, model="gpt-3.5-turbo"):
messages = [{"role": "user", "content": prompt}]
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=0, # this is the degree of randomness of the model's output
)
return response.choices[0].message["content"]
def main():
load_dotenv()
key = os.environ.get('OPENAI_API_KEY')
openai.api_key=key
st.set_page_config(page_title="Code Sidekick")
st.header("Code Sidekick")
s_in = st.text_area("Sample Input:",value=t_in)
s_cx = st.text_area("Optional Context:")
s_out = st.text_area("Sample Output:",value=t_out)
g_in = st.text_area("Generator Input:",value=g_out)
if st.button("Generate Code"):
button_handler(s_in, s_out, g_in, s_cx)
def button_handler(s_in, s_out, g_in, s_cx):
input_ok = len(s_in)>0 and len(s_out)>0 and len(g_in)>0
if input_ok:
ctx = f"""
{s_cx}
"""
prompt = f"""
You are an expert in languages, data formats and text pattern recognition.
Given the following pattern bounded in backticks:
```
{s_in}
```
gives the following code output also in backticks:
```
{s_out}
```
Generate the new code based on this new input in back ticks:
```
{g_in}
```
{ctx if len(s_cx)>0 else ""}
Otherwise say "I don't understand the pattern"
"""
response = get_completion(prompt)
st.write(prompt)
st.write("=========================================")
st.write(response)
else:
st.write(f"s_in={len(s_in)},g_in={len(g_in)}, s_cx={len(s_cx)}")
if __name__ == '__main__':
main()