awacke1 commited on
Commit
d11baa8
·
1 Parent(s): 9a65397

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -0
app.py CHANGED
@@ -5,8 +5,75 @@ import os
5
  from dotenv import load_dotenv
6
  load_dotenv()
7
  import openai
 
8
 
9
  openai.api_key = os.getenv('OPENAI_API_KEY')
10
  # keys are here: https://platform.openai.com/auth/callback?code=ReZ4izEw0DwkUKrHR-Opxr5AMMgo9SojxC9pNHQUcjD6M&state=OGZFNDJmLlJGNlIwOUxlakpXZkVFfjNxNy02ZlFtLWN4eUcuOXJobXouSQ%3D%3D#
11
  # Read this, https://blog.futuresmart.ai/building-a-gpt-4-chatbot-using-chatgpt-api-and-streamlit-chat
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  from dotenv import load_dotenv
6
  load_dotenv()
7
  import openai
8
+ from openai import ChatCompletion
9
 
10
  openai.api_key = os.getenv('OPENAI_API_KEY')
11
  # keys are here: https://platform.openai.com/auth/callback?code=ReZ4izEw0DwkUKrHR-Opxr5AMMgo9SojxC9pNHQUcjD6M&state=OGZFNDJmLlJGNlIwOUxlakpXZkVFfjNxNy02ZlFtLWN4eUcuOXJobXouSQ%3D%3D#
12
  # Read this, https://blog.futuresmart.ai/building-a-gpt-4-chatbot-using-chatgpt-api-and-streamlit-chat
13
 
14
+ import streamlit as st
15
+
16
+ # Set the API key (replace "YOUR_API_KEY" with your actual OpenAI API key)
17
+ openai.api_key = 'YOUR_API_KEY'
18
+
19
+
20
+
21
+
22
+ # Define a function to chat with the model
23
+ def chat_with_model(prompts):
24
+ model = "gpt-3.5-turbo" # change this to the model you're using
25
+
26
+ conversation = [{'role': 'system', 'content': 'You are a helpful assistant.'}]
27
+ conversation.extend([{'role': 'user', 'content': prompt} for prompt in prompts])
28
+
29
+ response = openai.ChatCompletion.create(model=model, messages=conversation)
30
+ return response['choices'][0]['message']['content']
31
+
32
+
33
+ import openai
34
+ import streamlit as st
35
+ from streamlit_chat import message
36
+ import os
37
+ from dotenv import load_dotenv
38
+ load_dotenv('api_key.env')
39
+ openai.api_key = os.environ.get('API_KEY')
40
+
41
+
42
+
43
+
44
+ def generate_response(prompt):
45
+ completion=openai.Completion.create(
46
+ engine='text-davinci-003',
47
+ prompt=prompt,
48
+ max_tokens=1024,
49
+ n=1,
50
+ stop=None,
51
+ temperature=0.6,
52
+ )
53
+ message=completion.choices[0].text
54
+ return message
55
+
56
+
57
+
58
+
59
+ # Streamlit App
60
+ def main():
61
+ st.title("Chat with AI")
62
+
63
+ # Pre-defined prompts
64
+ prompts = ['How's the weather?', 'Tell me a joke.', 'What is the meaning of life?']
65
+
66
+ # User prompt input
67
+ user_prompt = st.text_input("Your question:", '')
68
+
69
+ if user_prompt:
70
+ prompts.append(user_prompt)
71
+
72
+ if st.button('Chat'):
73
+ st.write('Chatting with GPT-3...')
74
+ response = chat_with_model(prompts)
75
+ st.write('Response:')
76
+ st.write(response)
77
+
78
+ if __name__ == "__main__":
79
+ main()