Can OpenAI chat completion do a keyword search

Hi I am trying to make use of Open AI as keyword search engine
like if I type a keyword the chat should return the sentences where the keyword has appeared

The lion is the king of the jungle. The nature is very kind. The king loves his horse
role: user
content: king (user gave the keyword king)

ideally I want AI to return -
role : assistant
The lion is the king of the jungle
The king loves his horse

But its just returning - The lion is the king of the jungle.
Sometimes it returns all the 3 sentences,

system message - Your task as an expert AI assistant is to return only the sentences where keywords given to you by the users are appeared.

I have kept the temperature = 0 and the input is small, the reply should fit easily in the maximum length set.

1 Like

I think it is working for me everytime with your examples. Does the screenshot show the result you expect? I can try with more examples, but if it does not work for you, can you double-check the parameters on your side?

These can help in understanding the issue better:

  • Which model are you using?
  • Do you use Playground to test, or did you code your own app?

How I would probably proceed after checking parameters:

The system prompt is good, but I think you can simplify it. My guess is that “as an expert AI assistant” is not necessary here and it might only confuse. The rest of the system probably can said easier. You can also add some example of output.

This next probably does not help with this issue but will be helpful when testing someting in playground and the moving to the app. So if you have already coded your app, you can double-check everything is exactly like in Playground. Maybe by accident the system prompt includes something else? Maybe there are empty line or \n\r instead or \n for line breaks. Or perhaps, the library you are using is not setting temperature correctly or is overriding it?

1 Like

What you’re asking for isn’t something the model is very good at. For long inputs, it will almost certainly forget some sentences, because it only has a limited amount of attention.

Then again, finding keywords is something a regular computer program (or database) is really good at, so trying to use an expensive large language model for that task doesn’t seem like the right tool, anyway.


@jwatte is correct. @RamN you could even ask ChatGPT tow write you a function in your preferred programming language to do what you want. It will be faster, cheaper and work every time after you have built it, does not even need Internet connection.


Thank you @torronen and @jwatte I appreciate your time.

It makes sense, I agree with @jwatte too, its not a good idea to make a costly search engine.

@torronen, thanks for checking the prompt. Also, I followed your guidelines on function, the function is giving me perfect answers at cheaper rate than adding an instruction in the system message and let Open AI do its work.
but definitely regular computer program will be almost free :smiley:

1 Like

I have one follow up question,
can this function be used in system message in the Playground so I can copy the code and make it API ready like, something like this:-

      "role": "system",
      "function": "search()"

Please excuse my question, newbie to functions, as got introduced through this post.

If this is a RAG implementation, add your keyword(s) to the embeddings search.

We asked this question repeatedly in all different kinds of ways with no answer:

Then, I simply added the keywords to the context call. And, voila!

Of course, if you add the keywords to the metadata, and there are no matches, then you get nothing back – so something like this you use with caution and only if you are 100% sure of the keywords.