Debug School

rakesh kumar
rakesh kumar

Posted on

How to convert to json format after retriving data from open ai api and renders in template file django

step 1: get data using openai api modal and convert into json format in view.py

  nutrition = response_nutrition.choices[0].text

  parsed_message_nutrition = nutrition .split("\n") 
Enter fullscreen mode Exit fullscreen mode
@csrf_exempt
def paneer_form(request):

 nutrition_value = "Generate nutrition values for a paneer"

 response_nutrition = openai.Completion.create(
                engine='text-davinci-003',
                prompt=nutrition_value,
                max_tokens=120,
                temperature=0.3,
                top_p=1.0,
                frequency_penalty=0.0,
                presence_penalty=0.0
            )

 if response_nutrition.choices:
  generated_message_nutrition = response_nutrition.choices[0].text
parsed_message_nutrition = generated_message_nutrition.split("\n")

context = {'paneer': parsed_message,'paneer_about': generated_message_about,'paneer_nutrition': parsed_message_nutrition}

 print(parsed_message_nutrition)

  return render(request, 'openai/dashboard.html', context)
Enter fullscreen mode Exit fullscreen mode

In template file

<table>
                <thead>
                  <tr>
                    <h4>Nutrition Value:</h4>
                  </tr>
                </thead>
                <tbody>
                  {% for step in paneer_nutrition %}
                  <tr>
                    <td>{{ step }}</td>
                  </tr>
                  {% endfor %}
                </tbody>
              </table>
Enter fullscreen mode Exit fullscreen mode

output

Image description

Image description

Top comments (0)