elastic / elasticsearch-labs

Notebooks & Example Apps for Search & AI Applications with Elasticsearch
https://www.elastic.co/search-labs
Apache License 2.0
593 stars 144 forks source link

"'NoneType' object is not iterable" in the Cohere notebook #277

Closed liu-xiao-guo closed 2 months ago

liu-xiao-guo commented 2 months ago

In the notebook at https://github.com/elastic/elasticsearch-labs/blob/main/notebooks/integrations/cohere/cohere-elasticsearch.ipynb, in the last part of the code:

response = co.chat(message=query, documents=ranked_documents, model="command-r-plus")

source_documents = []
for citation in response.citations:
   for document_id in citation.document_ids:
       if document_id not in source_documents:
           source_documents.append(document_id)

print(f"Query: {query}")
print(f"Response: {response.text}")

It returns the following error:


`---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[15], line 4
      1 response = co.chat(message=query, documents=ranked_documents, model="command-r-plus")
      3 source_documents = []
----> 4 for citation in response.citations:
      5    for document_id in citation.document_ids:
      6        if document_id not in source_documents:

TypeError: 'NoneType' object is not iterable`

According to the documentation at https://docs.cohere.com/docs/chat-api. There is no such a field "citations" in the Chat API.

image

image

liu-xiao-guo commented 2 months ago

This may impact the blog at https://www.elastic.co/search-labs/blog/elasticsearch-cohere-rerank. For my setup:

$ pip3 list | grep elasticsearch
elasticsearch               8.14.0
$ pip3 list | grep cohere
cohere                      5.5.8
szabosteve commented 2 months ago

If a trial account is used while walking through the tutorial, enough documents won't be uploaded to get relevant search results. For this reason, one of the tutorial requirements is using a production key. This fact was highlighted in the notebook but not on the docs page. This PR fixes this issue. It also references the Colab notebook and provides a link to RAG documentation on Cohere's side.