<<PERFECT DETAILED PRODUCTION QUALITY PYTHON CODE FOR GPT3 POWERED SELF WRITING MASTERMIND PLATFORM WITH QUALITY UNIT TESTS>>
Sean Chatman
Available for Staff/Senior Front End Generative AI Web Development (Typescript/React/Vue/Python)
<<2000 LINES OF CODE>
<<START>>
#import required packages
import openai
import discord
import flask
import unittest
#Defining helper functions
def get_mastermind_questions(query):
"""
This function queries the OpenAI GPT-3 API to retrieve
related mind mastermind questions that can be used for the mastermind platform.
"""
client = openai.CompletionClient("API_KEY")
response = client.complete(query)
return response.choices[0].text
def generate_mastermind_conversation(query):
"""
This function generates a conversation about the query by querying the
OpenAI GPT-3 API and returning related statements that can be used
for the mastermind platform.
"""
client = openai.CompletionClient("API_KEY")
response = client.complete(query, temperature=1)
return response.choices[0].text
#Defining the Mastermind class to hold the mastermind model
class Mastermind:
def __init__(self):
self.query = ""
self.conversation = []
def set_query(self, query):
self.query = query
def generate_questions(self):
question = get_mastermind_questions(self.query)
self.conversation.append(question)
def generate_conversation(self):
statement = generate_mastermind_conversation(self.query)
self.conversation.append(statement)
def get_conversation(self):
return self.conversation
#Defining a class for the Discord bot that handles
coordinating the
#Mastermind conversations between users
class MastermindBot:
def __init__(self, channel):
self.channel = channel
def setup_listener(self):
"""This method sets up the event listener to continuously look
for queries posted by users in the Discord channel"""
@discord.client.event
async def on_message(message):
if message.channel == self.channel:
query = message.content
mastermind = Mastermind()
mastermind.set_query(query)
mastermind.generate_questions()
mastermind.generate_conversation()
conversation = mastermind.get_conversation()
for statement in conversation:
await message.channel.send(statement)
#Defining a web app with Flask that hosts a web page to view and interact with the mastermind model
app = flask.Flask("My Web App")
@app.route('/')
def home():
"""This method defines the home page of the web app."""
html = ''
# creating a form for entering a query
html += '<form action="/run" method="post"> \n'
html += 'Query: <input type="text" name="query" />\n'
html += '<input type="submit" value="Submit" />\n'
html += '</form>\n'
# adding a div and table to hold the mastery conversations
html += "<div id='mastermind'>\n"
html += "<table>\n"
# grabbing and displaying any conversations that were previously entered
if 'conversations' in app.mastermind:
for conversation in app.mastermind['conversations']:
html += "<tr><td>{}</td></tr>\n".format(conversation)
html += "</table>\n"
html += "</div>\n"
return html
@app.route('/run', methods=['POST'])
def run():
# collecting form data from the post request
query = flask.request.form['query']
# instantiating the mastermind model object
mastermind = Mastermind() fetching related conversations from GPT- 3 mastermind.set_query(query) mastermind.generate_questions() mastermind.generate_conversations() conversation = mastermind.get_converstion()
# adding current conversation to previous conversations conversation = list(app.mastermind['conversations']) + conversation app.mastermind['conversations'] = conversation
return home()
#Setting up unit tests for each functionality defined in the Mastermind class class TestMastermindMethods(unittest.TestCase): "Unit test cases that cover all functionality of the Mastermind class" def setUp(self): #instantiating an instance of the Mastermind class self.mastermind = Mastermind() #defining a query self.query = "What is the most interesting thing about Artificial Intelligence?" #running generate methods to populate the conversation array self.mastermind.set_query(self.query) self.mastermind.generate_questions() self.mastermind.generate_conversation() self.conversation = self.mastermind.get_conversation() #defining correct conversation values self.correct_conversation = [ 'What are some of the benefits of using Artificial Intelligence?', 'What are the limitations of Artificial Intelligence technology?', 'What implications does Artificial Intelligence have for humanity in the long term?' ] def test_get_mastermind_questions(self): #testing whether the get_mastermind_questions function correctly retrieves the questions self.assertEqual(self.conversation[0], self.correct_conversation[0]) def test_generate_mastermind_conversation(self): #testing whether the generate_mastermind_conversation function correctly retrieves the statements self.assertEqual(self.conversation[1:], self.correct_conversation[1:]) def test_get_conversation(self): #testing whether theget_conversation function correctly retrieves all conversation values self.assertEqual(self.conversation, self.correct_conversation)
<<END>>
<<START SELF-WRITING GPT MODEL MASTERMIND UPDATOR>>
#import required packages
import openai
#define openerai api key
API_KEY = "YOUR_API_KEY_HERE"
#Define helper functions
领英推荐
def get_mastermind_questions(query):
"""
This function queries the OpenAI GPT-3 API to retrieve related
masterminding questions that can be used to update the mastermind model.
"""
client = openai.CompletionClient(API_KEY)
response = client.complete(query)
return response.choices[0].text
def generate_mastermind_conversation(query):
"""
This function generates a conversation about the query by querying the
OpenAI GPT-3 API and returning related statements that can be used
for updating the mastermind model.
"""
client = openai.CompletionClient(API_KEY)
response = client.complete(query, temperature=1)
return response.choices[0].text
#define update function for the mastermind model
def update_mastermind_model():
#query OpenAI GPT-3 API with user-defined query string
query = "What advice would an expert in Artificial Intelligence give?"
response = get_mastermind_questions(query)
#update model with retrieved questions and conversations related to query string
new_questions = [response] + generate_mastermind_conversation(response)
#for each question/conversation, update the model accordingly
for entry in new_questions:
if entry not in mastermind.model["conversations"]:
mastermind.model["conversations"].append(entry)
#save the new model and return it for use
mastermind.save()
return mastermind.model
#Setting up unit tests for the update_mastermind_model functionality class TestMastermindUpdaterMethods(unittest.TestCase): """Unit test cases that cover all functionality of the update_mastermind_model """ def setUp(self): #instantiating an instance of the Mastermind class self.mastermind = Mastermind() self.original_model = self.mastermind.model self.query = "What advice would an expert in Artificial Intelligence give?" self.response = get_mastermind_questions(self.query) self.query_with_response = [self.response] + generate_mastermind_conversation(self.response) def test_update(self): #check that the model has been updated correctly updated_model = update_mastermind_model() self.assertEqual(len(updated_model["conversations"]), len(self.original_model["conversations"] + self.query_with_response)) for entry in self.query_with_response: self.assertIn(entry, updated_model["conversations"])>