Doing logic programming based on OpenAI API results

Hi, I’m developing an app which needs to compare 3 different paragraphs of text (paragraph A, paragraph B, and paragraph C) to a larger reference text paragraph and tells me which of the A,B, or C paragraphs are more related to the reference paragraph and then do some logic programming based on the result. So the first part I think can be easily handled via a prompt to the OpenAI API, and it would tell which of the paragraphs are more related, but I want to create an if switch and do some logics based on the result of OpenAI API. But since the result of openai API is explanatory and won’t give me a reliable number or variable, I was wondering how such task would be done in the era of LLMs?
Thanks very much in advance.

This is a good case for using the Embedding API, not the completions engine.

Get an embedding for your large text (with whatever strategy is required), then for each of your test paragraphs and see which one has greater cosine similarity (with the large text’s vector)


Awesome, Thanks very much Robert, seems like exactly the thing I need!

1 Like

You could also ask the output to be in JSON format. And ask it not to generate any other result.

{“Paragraph”: 1}

… and it’s cheaper and faster by far! :slight_smile:

1 Like

Thanks very much, so you mean specifying it in the promo that “I need the result in json format like {“paragraph”:1}” ? And isn’t it risky as the nature of generative AI is creating new results each time?

Not really.

GenAI as you said is generative, so its taking an input (say a few shot examples that says it needs to generate in a specific format), and then makes a decision and given an output in that format.

Best to use this with the JSON format output using the JSON Mode, as described here:

1 Like