oliveirabruno01 commited on
Commit
5e56e98
·
1 Parent(s): f336208

Fix request body and add api_key field

Browse files
Files changed (2) hide show
  1. .env.example +0 -1
  2. app.py +16 -17
.env.example DELETED
@@ -1 +0,0 @@
1
- OPENAI_API_KEY=
 
 
app.py CHANGED
@@ -1,7 +1,6 @@
1
  import gradio as gr
2
  import time
3
  from openai import OpenAI
4
- from dotenv import load_dotenv
5
  from tenacity import (
6
  retry,
7
  stop_after_attempt,
@@ -9,25 +8,13 @@ from tenacity import (
9
  )
10
  import prompts
11
 
12
- load_dotenv()
13
-
14
- # Initialize OpenAI client
15
- client = OpenAI(
16
- base_url="https://openai-proxy.replicate.com/v1"
17
- )
18
-
19
 
20
  previous_thought = ""
21
  previous_answer = ""
22
  is_clearing = False
23
 
24
 
25
- @retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6))
26
- def completion_with_backoff(**kwargs):
27
- return client.chat.completions.create(**kwargs)
28
-
29
-
30
- def ai_response(input_text, shared_text, temperature):
31
  global previous_thought
32
 
33
  in_context_learning = [*prompts.continue_skill_example, *prompts.boilerplate_example, *prompts.continue_complete_skill_example,
@@ -40,19 +27,29 @@ def ai_response(input_text, shared_text, temperature):
40
  "content": str({"shared_context": shared_text, "previous_thought": previous_thought})},
41
  {"role": "user", "content": input_text}
42
  ]
43
- messages = [*prompts.system_prompt, *in_context_learning, *context]
44
  print(messages)
45
 
46
  shared_text = ""
47
 
48
  print(messages)
49
 
 
 
 
 
 
 
 
 
 
 
50
  stream = completion_with_backoff(
51
  model='meta/llama-2-70b-chat',
52
  temperature=temperature,
53
  messages=messages,
54
  response_format={"type": "json_object"},
55
- stream=True
56
  )
57
 
58
  thought = ""
@@ -79,13 +76,15 @@ def ai_response(input_text, shared_text, temperature):
79
 
80
 
81
  with gr.Blocks() as demo:
 
 
82
  user_input = gr.Textbox(lines=2, label="User Input")
83
  cot_textbox = gr.Textbox(label="CoT etc.")
84
  shared_textbox = gr.Textbox(label="Shared Textbox", interactive=True)
85
  temperature = gr.Slider(label="Temperature", minimum=0, maximum=2, step=0.01, value=0.01)
86
  # n_shots = gr.Slider(label="N-shots (~150 tokens each. It should not work 0-shot)", minimum=0, maximum=5, step=1, value=1)
87
  ai_btn = gr.Button("Generate AI Response")
88
- generation = ai_btn.click(fn=ai_response, inputs=[user_input, shared_textbox, temperature],
89
  outputs=[shared_textbox, cot_textbox])
90
 
91
 
 
1
  import gradio as gr
2
  import time
3
  from openai import OpenAI
 
4
  from tenacity import (
5
  retry,
6
  stop_after_attempt,
 
8
  )
9
  import prompts
10
 
 
 
 
 
 
 
 
11
 
12
  previous_thought = ""
13
  previous_answer = ""
14
  is_clearing = False
15
 
16
 
17
+ def ai_response(api_key, input_text, shared_text, temperature):
 
 
 
 
 
18
  global previous_thought
19
 
20
  in_context_learning = [*prompts.continue_skill_example, *prompts.boilerplate_example, *prompts.continue_complete_skill_example,
 
27
  "content": str({"shared_context": shared_text, "previous_thought": previous_thought})},
28
  {"role": "user", "content": input_text}
29
  ]
30
+ messages = [prompts.system_prompt, *in_context_learning, *context]
31
  print(messages)
32
 
33
  shared_text = ""
34
 
35
  print(messages)
36
 
37
+ # Initialize OpenAI client
38
+ client = OpenAI(
39
+ base_url="https://openai-proxy.replicate.com/v1",
40
+ api_key=api_key
41
+ )
42
+
43
+ @retry(wait=wait_random_exponential(min=1, max=120), stop=stop_after_attempt(6))
44
+ def completion_with_backoff(**kwargs):
45
+ return client.chat.completions.create(**kwargs)
46
+
47
  stream = completion_with_backoff(
48
  model='meta/llama-2-70b-chat',
49
  temperature=temperature,
50
  messages=messages,
51
  response_format={"type": "json_object"},
52
+ stream=True,
53
  )
54
 
55
  thought = ""
 
76
 
77
 
78
  with gr.Blocks() as demo:
79
+ api_input = gr.Textbox(label="Your OpenAI API key")
80
+
81
  user_input = gr.Textbox(lines=2, label="User Input")
82
  cot_textbox = gr.Textbox(label="CoT etc.")
83
  shared_textbox = gr.Textbox(label="Shared Textbox", interactive=True)
84
  temperature = gr.Slider(label="Temperature", minimum=0, maximum=2, step=0.01, value=0.01)
85
  # n_shots = gr.Slider(label="N-shots (~150 tokens each. It should not work 0-shot)", minimum=0, maximum=5, step=1, value=1)
86
  ai_btn = gr.Button("Generate AI Response")
87
+ generation = ai_btn.click(fn=ai_response, inputs=[api_input, user_input, shared_textbox, temperature],
88
  outputs=[shared_textbox, cot_textbox])
89
 
90