Debug School

rakesh kumar
rakesh kumar

Posted on

How to apply ternary operator on dynamic request value in django

To apply a ternary operator on a dynamic request value while storing data in the view function, you can use the conditional expression syntax. Here's an example of how you can do it:

views.py

from django.shortcuts import render

def my_view(request):
    # Get the dynamic request value
    dynamic_value = request.POST.get('dynamic_value')

    # Apply the ternary operator to set the stored value
    stored_value = dynamic_value if dynamic_value is not None else 'default'

    # Store the data or perform any other operations
    # ...

    return render(request, 'my_template.html', {'stored_value': stored_value})
Enter fullscreen mode Exit fullscreen mode

In this example, the dynamic_value is obtained from the request object using request.POST.get('dynamic_value'). Then, the ternary operator is applied to set the stored_value. If the dynamic_value is not None, it is assigned to stored_value, otherwise, the default value 'default' is used.

Another Real Example

openai.api_key = settings.OPENAI_API_KEY # Replace with your OpenAI API key

@csrf_exempt
def chat_with_gpt(request):
    if request.method == 'POST':
        form = ChatForm(request.POST)
        if form.is_valid():
            user_input = form.cleaned_data['message']
            dynamic_max_tokens =  request.POST.get('max_tokens')  
            dynamic_top_p = request.POST.get('top_p') 
            dynamic_temperature = request.POST.get('temperature') 
            dynamic_modal = request.POST.get('modal')
            dynamic_frequency_penalty = request.POST.get('frequency_penalty') 
            dynamic_presence_penalty = request.POST.get('presence_penalty') 

            max_tokens = int(dynamic_max_tokens) if dynamic_max_tokens and dynamic_max_tokens != '0' else 100
            modal = dynamic_modal if dynamic_modal and dynamic_modal != '0' else "text-davinci-003"
            temperature = float(dynamic_temperature) if dynamic_temperature and dynamic_temperature != '0' else 0.5
            top_p = float(dynamic_top_p) if dynamic_top_p and dynamic_top_p != '0' else 0.5
            frequency_penalty = float(dynamic_frequency_penalty) if dynamic_frequency_penalty and dynamic_frequency_penalty != '0' else 0.5
            presence_penalty = float(dynamic_presence_penalty) if dynamic_presence_penalty and dynamic_presence_penalty != '0' else 0
            data = {
                'user_input':form.cleaned_data['message'],
                'temperature':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

            openai.api_key = settings.OPENAI_API_KEY
            response = openai.Completion.create(
                model=modal,
                prompt=user_input,
                temperature=temperature,
                max_tokens=max_tokens,
                top_p=top_p,
                frequency_penalty=frequency_penalty,
                presence_penalty=presence_penalty,
                logprobs=0,
                echo=True
            )

            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})
Enter fullscreen mode Exit fullscreen mode

Top comments (0)