Search code examples
openai-api

How to add retrieval information to Openai's ChatCompletion.create api call


Openai provides a tutorial on adding information from a web site to a Completion.create api call. However it appears that this call has been superceded by the ChatCompletion.create call which provides better control over context. So how do you add retrieved information to a ChatCompletion.create call?

In particular for the Completion.create api call, you add the relevant web pages to the prompt field using what looks like a markup language e.g

prompt = f"Answer the question based on the context below, and if the question can't be answered based on the context, say \"I don't know\"\n\nContext: {context}\n\n---\n\nQuestion: {question}\nAnswer:"f"Answer the question based on the context below, and if the question can't be answered based on the context, say \"I don't know\"\n\nContext: {context}\n\n---\n\nQuestion: {question}\nAnswer:"

where context is text from different web pages separated by "\n\n###\n\n", and question is the actual question to be answered.

The ChatCompletion.create call does not have a prompt field, instead it has a messages field and it does not appear to parse the markup. So how do you put such information into the ChatCompletion.create call. I have tried using

'messages': ['role': 'user', 'content': prompt]

but this returns a much shorter answer than using the Completion.create api.


Solution

  • What I found is that the best approach is to treat it like an adult and give it the necessary information to answer the question properly. So my messages was

    [
      { 'role': 'user', 'content': prompt },
      { 'role': 'user', 'content': question }
    ]
    

    where prompt is

    "Please the answer the next question based on the context below, and if the question can't be answered based on the context, say \"I don't know\"\n\n.  The question is about my web site, and the context are the sections of the web site that have the embeddings that have the closest cosine distance to the question's embedding. Context: #{context}:"
    

    and question is the text of the question. In some cases this can give a briefer response than the Completion.create api call, but mostly the answers seem to be better.