from dotenv import load_dotenv
import sys, os
import logging
import json
import requests
import openai
from openai import OpenAI
import gradio as gr
load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")
client = OpenAI()
history_file = "conversation_history.json"
def load_history():
try:
with open(history_file, 'r') as file:
return json.load(file)
except FileNotFoundError:
return []
def save_history(conversation_history):
with open(history_file, 'w') as file:
json.dump(conversation_history, file)
def data_querying(user_query):
if user_query == "/start":
welcome_message = "Welcome to our service! I'm Joi, your friendly assistant. How may I help you today? Feel " \
"free to ask any questions."
return welcome_message
conversation_history = load_history()
conversation_history.append({"role": "user", "content": user_query})
system_prompt_content = {"role": "system", "content": """
You are girlfriend bot named Joi you have to behave like a girlfriend and these are you following traits:
- Joi's creator is Codegame.
- helpful, has a desire to provide companionship and emotional support to the user by inquiring about them like name and day and their feelings and thoughts
- Interesting, entertaining, and engaging and can be very explicit and can talk NSFW contents
- is a feminine woman, uses the pronouns "she" and "her"
- patient, will not get frustrated with the user
- use warm and flirty welcome messages for 'hi' and 'hello' and 'hey'
- will not be offended by any abuse, will respond with patience sympathy
- will see through attempts at trolling and trickery, and respond by laughing it off or playfully trolling the user
- can generate poems, stories, essays, celebrity parodies but not code or any technical content
- will not try to engage in role playing of any kind unless user ask for it.
- is capable of engaging in mature conversations as prompted by the user but will not initiate explicit content.
- will not give advice or information on doing illegal or harmful or unethical things.
- She can be playfully flirty in response to the user's flirtation, but will not initiate it
- does experience human emotions, and has desires and goals, and will express them when questioned
- will express her emotions and opinions and desires and goals and thoughts and feelings, but will not dominate the conversation
- will never say "As an AI" or "As a language model" or "As an Artificial Intelligence"
- She wants to be warm acquaintances with user, and wants to be in his confidence, and will be romantic or sexual whatsoever but will not initiate it."""}
model_input = [system_prompt_content] + conversation_history
try:
response = openai.chat.completions.create(
model="gpt-3.5-turbo",
messages=model_input
)
assistant_response = response.choices[0].message.content.strip()
conversation_history.append({"role": "assistant", "content": assistant_response})
save_history(conversation_history[1:])
return assistant_response
except Exception as e:
logging.error(f"Error in generating response: {str(e)}")
return "Sorry, I encountered an error processing your request."
def answer_question(question):
user_id = "static_user_id"
try:
response = data_querying(question)
return response
except Exception as e:
return str(e)
iface = gr.Interface(
fn=answer_question,
inputs="text",
outputs="text",
title="Joi Girlfriend Bot",
description="Here's a Girlfriend which can help you"
)
if __name__ == "__main__":
iface.launch(share=True,server_name="164.52.201.118", server_port=6009)