First way:
Apply condition on dynamic incoming request
Apply null or empty condition on dynamic incoming request
Apply nested if condition on dynamic incoming request is equal to zero
Apply condition on dynamic incoming request
from django.shortcuts import render
def my_view(request):
if request.method == 'POST':
# Get the data from the request
input_data = request.POST.get('input_data')
# Perform the if-else condition
if input_data == 'condition1':
# Store the data based on condition 1
stored_data = 'Data stored for condition 1'
else:
# Store the data based on other conditions
stored_data = 'Data stored for other conditions'
# Render the template with the stored data
return render(request, 'my_template.html', {'stored_data': stored_data})
# Handle GET requests or other cases
return render(request, 'my_template.html')
Apply null or empty condition on dynamic incoming request
from django.shortcuts import render
def my_view(request):
if request.method == 'POST':
# Get the data from the request
input_data = request.POST.get('input_data')
# Check if input_data is null or empty
if input_data:
# Perform the if-else condition
if input_data == 'condition1':
# Store the data based on condition 1
stored_data = 'Data stored for condition 1'
else:
# Store the data based on other conditions
stored_data = 'Data stored for other conditions'
else:
# If input_data is null or empty, skip storing data
stored_data = None
# Render the template with the stored data
return render(request, 'my_template.html', {'stored_data': stored_data})
# Handle GET requests or other cases
return render(request, 'my_template.html')
Apply nested if condition condition on dynamic incoming request is equal to zero
from django.shortcuts import render
def my_view(request):
if request.method == 'POST':
# Get the data from the request
input_data = request.POST.get('input_data')
# Check if input_data is null, empty, or equal to 0
if input_data is not None and input_data != '' and input_data != '0':
# Perform the if-else condition
if input_data == '0':
# Skip storing data if input_data is 0
stored_data = None
else:
# Store the data based on other conditions
stored_data = 'Data stored for other conditions'
else:
# If input_data is null, empty, or 0, skip storing data
stored_data = None
# Render the template with the stored data
return render(request, 'my_template.html', {'stored_data': stored_data})
# Handle GET requests or other cases
return render(request, 'my_template.html')
In this updated code, the condition if input_data is not None and input_data != '' and input_data != '0': is used to check if input_data is not null, not empty, and not equal to the string '0'. If the condition is met, it proceeds with the if-else condition. If input_data is '0', it skips storing data by setting stored_data to None. Otherwise, it stores the data based on other
conditions.
dynamic_max_tokens = int(request.POST.get('max_tokens'))
dynamic_top_p = request.POST.get('top_p')
dynamic_temperature = request.POST.get('temperature')
dynamic_frequency_penalty = request.POST.get('frequency_penalty')
dynamic_presence_penalty = request.POST.get('presence_penalty')
temperature = float(dynamic_temperature) if dynamic_temperature is not None else 0.5
top_p = float(dynamic_top_p) if dynamic_top_p is not None else 1.0
frequency_penalty = float(dynamic_frequency_penalty) if dynamic_frequency_penalty is not None else 0.0
presence_penalty = float(dynamic_presence_penalty) if dynamic_presence_penalty is not None else 0.0
data = {
'user_input':form.cleaned_data['message'],
'temperature':request.POST.get('temperature'),
'max_tokens':request.POST.get('max_tokens'),
'top_p':top_p,
}
# Print all the request data
print('Request Data:', data)
# Make a request to ChatGPT using the OpenAI library
if dynamic_max_tokens == '0':
openai.api_key = settings.OPENAI_API_KEY
response = openai.Completion.create(
model="text-davinci-003",
prompt=user_input,
temperature=temperature,
max_tokens=100,
top_p=top_p,
frequency_penalty=frequency_penalty,
presence_penalty=presence_penalty,
logprobs=0,
)
if response.choices:
print('data 200 choices')
generated_message = response.choices[0].text.strip()
print(generated_message)
else:
print('data else choices')
generated_message = 'Failed to generate response'
else:
openai.api_key = settings.OPENAI_API_KEY
response = openai.Completion.create(
model="text-davinci-003",
prompt=user_input,
temperature=temperature,
max_tokens=dynamic_max_tokens,
top_p=top_p,
frequency_penalty=frequency_penalty,
presence_penalty=presence_penalty,
logprobs=0
)
if response.choices:
print('data 200 choices')
generated_message = response.choices[0].text.strip()
print(generated_message)
else:
print('data else choices')
generated_message = 'Failed to generate response'
# Save the user input and generated response to the database
chat_message = ChatMessage(message=user_input, generated_message=generated_message)
chat_message.save()
return render(request, 'chatgpt.html', {'form': form, 'generated_message': generated_message})
else:
form = ChatForm()
return render(request, 'chatgpt.html', {'form': form})
Top comments (0)