Skip to content

Commit

Permalink
Refine prompts, add basic presentation generation functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
HanzPo committed Mar 30, 2024
1 parent 7638f99 commit f122033
Showing 1 changed file with 37 additions and 5 deletions.
42 changes: 37 additions & 5 deletions backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
project="genai-genesis-418815"
location = 'northamerica-northeast1'
vertexai.init(project=project, location=location)
system = "You are an intelligent assistant who is an expert at a broad range of topics. Your job is to help humans learn about new topics. You only provide responses in a json format"
system = "You are an intelligent assistant who is an expert at a broad range of topics. Your job is to help humans learn about new topics."

app = Flask(__name__)

Expand All @@ -21,7 +21,7 @@ def create_subtopics():
request_data = request.get_json()
topic = request_data['topic']

human = "Create a list of 15 subtopics from the topic of {topic}. The name of the key should be 'subtopics' and the value should be a single list with 15 elements"
human = "Create a list of 15 subtopics that someone interested in learning about {topic} should learn. You must respond through a json format. The name of the only key in the json object should be 'subtopics' and the value should be a single list with 15 elements. Do not include markdown formatting in your response."
prompt = ChatPromptTemplate.from_messages([("system", system), ("human", human)])

llm = ChatVertexAI(model_name="gemini-pro", convert_system_message_to_human=True)
Expand All @@ -36,12 +36,44 @@ def create_subtopics():

if (results[:7] == "```json"):
results = results[7:-4]
elif (results[:3] == "```"):
results = results[3:-4]
elif (results[:7] == "```JSON"):
results = results[7:-4]

return json.loads(results)

@app.route("/generate_topics")
def generate_topics():
return "hello world"
@app.route("/create_presentation", methods = ['POST'])
def create_presentation():
request_data = request.get_json()
topic = request_data['topic']
education = request_data['education_level']
detail = request_data['detail']
subtopics = request_data['subtopics']

for subtopic in subtopics:
human = "Generate a paragraph explaining to someone at the {education} level in {detail} detail about {subtopic}. It is part of a presentation on {topic}. Respond in plain text. Do not include any markdown formatting."
prompt = ChatPromptTemplate.from_messages([("system", system), ("human", human)])

llm = ChatVertexAI(model_name="gemini-pro", convert_system_message_to_human=True)

output_parser = StrOutputParser()

chain = prompt | llm | output_parser

try:
results = chain.invoke({"topic" : topic, "subtopic": subtopic, "detail" : detail, "education": education})
except:
print("--------------------------------------------RATE LIMIT REACHED--------------------------------------------")
print("--------------------------------------------RATE LIMIT REACHED--------------------------------------------")
print("--------------------------------------------RATE LIMIT REACHED--------------------------------------------")

print(subtopic)
print(results)



return "<h1>Hello world</h1>"


if __name__ == "__main__":
Expand Down

0 comments on commit f122033

Please sign in to comment.