How to define another another flask endpoint in a ore defined function like below?def…
- User
How to define another another flask endpoint in a ore defined function like below?def gen_questions():
data = request.json
assistant_id = data.get("assistant_id", "")
goals = data.get("goals", "")
ques_num = data.get("ques_num", "")
ques_type = data.get("ques_type", "")
ques_focus = data.get("ques_focus", "")
difficulty_level = data.get("difficulty_level", "")
locale = data.get("locale", "")
prompt = data.get("prompt", "").get("prompt", "")
prompt2 = data.get("prompt", "").get("prompt", "")
placeholders = ['%s', '%s', '%s', '%s', '%s', '%s']
values = [goals, ques_num, ques_type, ques_focus, difficulty_level, locale]
for placeholder, value in zip(placeholders, values):
prompt = prompt.replace(placeholder, value, 1)
thread = client.beta.threads.create()
client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content=prompt
)
run = client.beta.threads.runs.create(
thread_id= thread.id,
assistant_id=assistant_id
)
Check if the Run requires action
while True:
run_status = client.beta.threads.runs.retrieve(
thread_id= thread.id, run_id=run.id
)
print(f"Run status: {run_status.status}")
if run_status.status == "completed":
break
sleep(1)
messages = client.beta.threads.messages.list(thread_id=thread.id)
response = messages.data[0].content[0].text.value
output_tokens = num_tokens_from_string(f"{response}", "cl100k_base")
input_tokens = num_tokens_from_string(f"{prompt}", "cl100k_base")
tokens = output_tokens + input_tokens
print(f"Assistant response: {response} | Tokens used: {tokens}")
return jsonify({"response": response}, {"tokens": tokens})
Sure, here's the modified gen_questions() function that uses the prompt received in the request and applies different prompts based on the ques_type value:
python
- Assistant
It looks like you are asking how to define another Flask endpoint within an already defined function, which actually is contrary to Flask's design. Flask uses decorators to route URLs to different view functions, and each view function is mapped to a different endpoint. However, you can have conditional logic within a single view function that changes its behavior based on the received data, such as the
ques_typein the given example.I'll first show you how to create a Flask endpoint using this function and then show you how you can conditionally handle different
ques_typevalues within this function.Here's how you set up a basic Flask app with this endpoint:
from flask import Flask, request, jsonify import time # Import the time module for sleep from somepackage import client, num_tokens_from_string # Replace 'somepackage' with the actual package name. app = Flask(__name__) @app.route('/gen_questions', methods=['POST']) def gen_questions(): data = request.json # The rest of your code here... # Instead of using `print`, it's better to use logging in production code # print(f"Assistant response: {response} | Tokens used: {tokens}") return jsonify({"response": response, "tokens": tokens}) if __name__ == "__main__": app.run(debug=True)Please ensure you replace
'somepackage'with the actual name of the package whereclientandnum_tokens_from_stringare defined.Also, there are a few potential issues and opportunities for improvement in your initial function:
- You're accessing
.get("prompt", "")twice; likely, you intended to access different keys. - The sleep() function should come from the
timemodule, so you should havefrom time import sleep. - Instead of an indefinite loop checking for the run status, you may want to set a maximum number of retries to avoid an infinite loop in case of an error.
Remember that this function is defined within a Flask application, and as such, you can't define new Flask endpoints within the function itself. Instead, you create new endpoints in the main application body using the
@app.routedecorator, and each endpoint function (likegen_questions()) handles different routes or types of requests. - You're accessing
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.