forked from Anilabhimanyu/Edufrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
llm_model.py
40 lines (34 loc) · 955 Bytes
/
llm_model.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
import openai
import streamlit as st
from decouple import config
# Load API key from .env file
api_key = config("OPENAI_API_KEY")
# Set up OpenAI API credentials
openai.api_key = api_key
# Define the function to interact with the ChatGPT model
def gpt_response(prompt):
response = openai.Completion.create(
engine="text-davinci-003",
prompt=prompt,
max_tokens=2000,
temperature=0.1,
n=1,
stop=None,
)
return response.choices[0].text.strip()
def chat_with_gpt(message_history):
prompt = ""
for message in message_history:
role = message["role"]
content = message["content"]
prompt += f"{role}: {content}\n"
response = openai.Completion.create(
engine="text-davinci-003",
prompt=prompt,
max_tokens=50,
temperature=0.1,
n=1,
stop=None,
)
return response.choices[0].text.strip()
# Main function