File size: 1,658 Bytes
02063af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import openai
import os

from streamlit_chat import message
from dotenv import load_dotenv
from openai import ChatCompletion

load_dotenv()

openai.api_key = os.getenv('OPENAI_KEY')
# keys are here: https://platform.openai.com/auth/callback?code=ReZ4izEw0DwkUKrHR-Opxr5AMMgo9SojxC9pNHQUcjD6M&state=OGZFNDJmLlJGNlIwOUxlakpXZkVFfjNxNy02ZlFtLWN4eUcuOXJobXouSQ%3D%3D#

# Define a function to chat with the model
def chat_with_model(prompts):
    model = "gpt-3.5-turbo"  # change this to the model you're using

    conversation = [{'role': 'system', 'content': 'You are a helpful assistant.'}]
    conversation.extend([{'role': 'user', 'content': prompt} for prompt in prompts])

    response = openai.ChatCompletion.create(model=model, messages=conversation)
    return response['choices'][0]['message']['content']

load_dotenv('api_key.env')

def generate_response(prompt):
    completion=openai.Completion.create(
        engine='text-davinci-003',
        prompt=prompt,
        max_tokens=1024,
        n=1,
        stop=None,
        temperature=0.6,
    )
    message=completion.choices[0].text
    return message

# Streamlit App
def main():
    st.title("Chat with AI")

    # Pre-defined prompts
    prompts = ['Hows the weather?', 'Tell me a joke.', 'What is the meaning of life?']

    # User prompt input
    user_prompt = st.text_input("Your question:", '')

    if user_prompt:
        prompts.append(user_prompt)

    if st.button('Chat'):
        st.write('Chatting with GPT-3...')
        response = chat_with_model(prompts)
        st.write('Response:')
        st.write(response)

if __name__ == "__main__":
    main()