GithubHelp home page GithubHelp logo

langchain-cohere's People

Contributors

abdalrohman avatar abdullahkady avatar anirudh31415926535 avatar baskaryan avatar billytrend-cohere avatar ccurme avatar charl3sj avatar dependabot[bot] avatar efriis avatar ganggreentempertatum avatar grazzouk avatar harry-cohere avatar kaijietti avatar kakao-bart-lee avatar keitap avatar mahjongmen avatar marcoszh avatar minjie-cohere avatar pat-cohere avatar wei-fh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

langchain-cohere's Issues

'requested_tool_name': 'directly_answer', 'available_tool_names': ['google_search_tool']

Hello. I need help.

I need google_search_tool only as in the context of the message there are launches like "search", "find". How to call agent_executor with such questions, everything works fine, but how to ask a normal question without search, then I get the error directly_answer is not a valid tool, try one of [google_search_tool]., latencies are higher than 6 times, but the model gave a normal response.

class Chat:
 
    def __init__(self):
        model = ChatCohere()
        agent = create_cohere_react_agent(llm=model, tools=[], prompt=prompt)
        self.agent_executor = AgentExecutor(agent=agent, tools=[google_search_tool], verbose=True)
        self.chain = prompt | model
 
    def prediction(self, question, history):
        return self.agent_executor.astream({
            "question": question,
            "chat_history": history
        }})
 
    def invoke(self, question):
        return self.chain.invoke({"question": question})
 
 
cohere = Chat()
from langchain_google_community import GoogleSearchAPIWrapper
from langchain_core.tools import Tool

from config import GOOGLE_API_KEY

search = GoogleSearchAPIWrapper(google_api_key=GOOGLE_API_KEY)


google_search_tool = Tool(
    name="google_search_tool",
    description="Search Google for recent results.",
    func=search.run
)

image

ChatCohere always returns an AIMessage with empty content when bound to a tool

Below the example to reproduce it. Happens the same with command-r or command-r-plus. Also it happens regardless of the structure of the tool.

from langchain_cohere import ChatCohere
from langchain_core.messages import HumanMessage, AIMessage, AnyMessage, SystemMessage
from pydantic.v1 import BaseModel


class SelectIceCream(BaseModel):
    """Use this function only when the user wants to buy ice cream"""

    ice_cream_type: str


cohere_api_key = "<...>"
llm = ChatCohere(model="command-r", temperature=0.6, cohere_api_key=cohere_api_key)
llm_with_tools = llm.bind_tools([SelectIceCream])


chat = [
    SystemMessage(content="You are a helpful assistant"),
    HumanMessage(content="Where is the city of Paris? Be concise."),
]


def main():
    response = llm.invoke(chat)
    print(f"Without tools:\t{response=} \n\n---\n\n")

    response = llm_with_tools.invoke(chat)
    print(f"With tools:\t{response=}")


main()

Outputs:

Without tools:  response=AIMessage(content='The city of Paris is in the north of France, on the River Seine.', additional_kwargs={'documents': None, 'citations': None, 'search_results': None, 'search_queries': None, 'is_search_required': None, 'generation_id': '86f2f8b4-a99d-4246-8d72-6b7843ff55d0'}, response_metadata={'documents': None, 'citations': None, 'search_results': None, 'search_queries': None, 'is_search_required': None, 'generation_id': '86f2f8b4-a99d-4246-8d72-6b7843ff55d0'}, id='run-ede8f9b2-9e9a-4086-af6a-0274c02c315e-0') 

---


With tools:     response=AIMessage(content='', additional_kwargs={'documents': None, 'citations': None, 'search_results': None, 'search_queries': None, 'is_search_required': None, 'generation_id': 'f4ed5db6-7ea7-4495-a06a-f364cd53b417'}, response_metadata={'documents': None, 'citations': None, 'search_results': None, 'search_queries': None, 'is_search_required': None, 'generation_id': 'f4ed5db6-7ea7-4495-a06a-f364cd53b417'}, id='run-ef87eae8-7408-4f36-9f07-778ebd49f612-0')

So without tools gives a normal reply but the response content of the llm bound to tools is always an empty string.

I'm using langchain-core==0.1.16 and langchain-cohere==0.1.3

The usage shown in the source code and docs is invalid and the completion_with_retry have argument bug in llms.py (tested 0.14)

According to the Document in LangChain - Cohere
The usage for LLM is

from langchain_cohere import Cohere

It is same as stated in the source code's comment.
However, if use accordingly, the error will be

# ImportError: cannot import name 'Cohere' from 'langchain_cohere' (/Users/user-cloaked/miniforge3/envs/langchain/lib/python3.11/site-packages/langchain_cohere/__init__.py)

According to the structure of the source code, the actual working method seems to be:

from langchain_cohere.llms import Cohere

Assume that this usage is the actual way.
There is a bug inside the llms.py

If calling the Cohere according to manual, should be something like this

model = Cohere(model="command", max_tokens=256, temperature=0.75)

However, it will generate error like this

# TypeError: langchain_cohere.llms.completion_with_retry() got multiple values for keyword argument 'model'

Because when running completion_with_retry(), self.model is provided instead of model
i.e. it didn't allow to specify any model in other words
I am not sure if it is intended to do so or just a copy and paste error, the async also have to same problem by reading the source code without running.

So the final working writing at the moment is

from langchain_cohere.llms import Cohere
model = Cohere(max_tokens=256, temperature=0.75)
# ...

Pydantic basemodel with enum field not supported with_structured_output

llm.with_structured_output method does not support Pydantic models with enum fields. Getting following error.

    return master_prompts | self.get_default_model().with_structured_output(MessageRouter)
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>.venv/lib/python3.11/site-packages/langchain_core/_api/beta_decorator.py", line 109, in warning_emitting_wrapper
    return wrapped(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>.venv/lib/python3.11/site-packages/langchain_cohere/chat_models.py", line 210, in with_structured_output
    llm = self.bind_tools([schema], **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>.venv/lib/python3.11/site-packages/langchain_cohere/chat_models.py", line 189, in bind_tools
    formatted_tools = _format_to_cohere_tools(tools)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>.venv/lib/python3.11/site-packages/langchain_cohere/cohere_agent.py", line 61, in _format_to_cohere_tools
    return [_convert_to_cohere_tool(tool) for tool in tools]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>.venv/lib/python3.11/site-packages/langchain_cohere/cohere_agent.py", line 61, in <listcomp>
    return [_convert_to_cohere_tool(tool) for tool in tools]
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>.venv/lib/python3.11/site-packages/langchain_cohere/cohere_agent.py", line 155, in _convert_to_cohere_tool
    parameter_definitions={
                          ^
  File "<path>.venv/lib/python3.11/site-packages/langchain_cohere/cohere_agent.py", line 156, in <dictcomp>
    param_name: ToolParameterDefinitionsValue(
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>.venv/lib/python3.11/site-packages/pydantic/v1/main.py", line 341, in __init__
    raise validation_error
pydantic.v1.error_wrappers.ValidationError: 1 validation error for ToolParameterDefinitionsValue
type
  none is not an allowed value (type=type_error.none.not_allowed)

CohereEmbeddings doesn't work

Tried this code from the documentation example

from langchain_cohere.embeddings import CohereEmbeddings

cohere_embeddings = CohereEmbeddings(model="embed-english-light-v3.0")
text = "This is a test document."

query_result = cohere_embeddings.embed_query(text)
print(query_result)

doc_result = cohere_embeddings.embed_documents([text])
print(doc_result)

And got the following error

Traceback (most recent call last):
File "", line 6, in
File "/home/kolyan288/anaconda3/envs/Global/lib/python3.10/site-packages/langchain_cohere/embeddings.py", line 173, in embed_query
return self.embed([text], input_type="search_query")[0]
File "/home/kolyan288/anaconda3/envs/Global/lib/python3.10/site-packages/langchain_cohere/embeddings.py", line 118, in embed
embeddings = self.embed_with_retry(
File "/home/kolyan288/anaconda3/envs/Global/lib/python3.10/site-packages/langchain_cohere/embeddings.py", line 100, in embed_with_retry
return _embed_with_retry(**kwargs)
File "/home/kolyan288/anaconda3/envs/Global/lib/python3.10/site-packages/tenacity/init.py", line 330, in wrapped_f
return self(f, *args, **kw)
File "/home/kolyan288/anaconda3/envs/Global/lib/python3.10/site-packages/tenacity/init.py", line 467, in call
do = self.iter(retry_state=retry_state)
File "/home/kolyan288/anaconda3/envs/Global/lib/python3.10/site-packages/tenacity/init.py", line 368, in iter
result = action(retry_state)
File "/home/kolyan288/anaconda3/envs/Global/lib/python3.10/site-packages/tenacity/init.py", line 410, in exc_check
raise retry_exc.reraise()
File "/home/kolyan288/anaconda3/envs/Global/lib/python3.10/site-packages/tenacity/init.py", line 183, in reraise
raise self.last_attempt.result()
File "/home/kolyan288/anaconda3/envs/Global/lib/python3.10/concurrent/futures/_base.py", line 451, in result
return self.__get_result()
File "/home/kolyan288/anaconda3/envs/Global/lib/python3.10/concurrent/futures/_base.py", line 403, in __get_result
raise self._exception
File "/home/kolyan288/anaconda3/envs/Global/lib/python3.10/site-packages/tenacity/init.py", line 470, in call
result = fn(*args, **kwargs)
File "/home/kolyan288/anaconda3/envs/Global/lib/python3.10/site-packages/langchain_cohere/embeddings.py", line 98, in _embed_with_retry
return self.client.embed(**kwargs)
File "/home/kolyan288/anaconda3/envs/Global/lib/python3.10/site-packages/cohere/client.py", line 137, in embed
responses = [
File "/home/kolyan288/anaconda3/envs/Global/lib/python3.10/site-packages/cohere/client.py", line 137, in
responses = [
File "/home/kolyan288/anaconda3/envs/Global/lib/python3.10/concurrent/futures/_base.py", line 621, in result_iterator
yield _result_or_cancel(fs.pop())
File "/home/kolyan288/anaconda3/envs/Global/lib/python3.10/concurrent/futures/_base.py", line 319, in _result_or_cancel
return fut.result(timeout)
File "/home/kolyan288/anaconda3/envs/Global/lib/python3.10/concurrent/futures/_base.py", line 458, in result
return self.__get_result()
File "/home/kolyan288/anaconda3/envs/Global/lib/python3.10/concurrent/futures/_base.py", line 403, in _get_result
raise self.exception
File "/home/kolyan288/anaconda3/envs/Global/lib/python3.10/concurrent/futures/thread.py", line 58, in run
result = self.fn(*self.args, **self.kwargs)
File "/home/kolyan288/anaconda3/envs/Global/lib/python3.10/site-packages/cohere/client.py", line 140, in
lambda text_batch: BaseCohere.embed(
File "/home/kolyan288/anaconda3/envs/Global/lib/python3.10/site-packages/cohere/base_client.py", line 1446, in embed
typing.cast(typing.Any, construct_type(type
=typing.Any, object
=_response.json())) # type: ignore
File "/home/kolyan288/anaconda3/envs/Global/lib/python3.10/site-packages/httpx/_models.py", line 764, in json
return jsonlib.loads(self.content, **kwargs)
File "/home/kolyan288/anaconda3/envs/Global/lib/python3.10/json/init.py", line 346, in loads
return _default_decoder.decode(s)
File "/home/kolyan288/anaconda3/envs/Global/lib/python3.10/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/home/kolyan288/anaconda3/envs/Global/lib/python3.10/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

My dependencies

langchain==0.2.1
langchain-chroma==0.1.1
langchain-cli==0.0.24
langchain-cohere==0.1.8
langchain-community==0.2.1
langchain-core==0.2.3
langchain-experimental==0.0.59
langchain-huggingface==0.0.1
langchain-openai==0.1.8
langchain-text-splitters==0.2.0

platform: linux mint 21.1

python version: 3.10.14

Cohere client.tokenize() now requires model argument

Using a LangChain ConversationalRetrievalChain, we fail on a call to tokenize because of a missing model parameter:

> Entering new ConversationalRetrievalChain chain...
INFO:httpx:HTTP Request: POST https://api.cohere.ai/v1/embed "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: POST http://localhost:6333/collections/my.collection/points/search "HTTP/1.1 200 OK"
ERROR:root:Exception raised by task = <Task finished name='Task-522' coro=<Chain.ainvoke() done, defined at /home/zachary/.cache/pypoetry/virtualenvs/MY_VENV/lib/python3.10/site-packages/langchain/chains/base.py:170> exception=TypeError("Client.tokenize() missing 1 required keyword-only argument: 'model'")>
Traceback (most recent call last):
  File "/home/zachary/PycharmProjects/my_project/somewhere/in/my/code/utils.py", line 108, in _handle_task_result
    task.result()
  File "/home/zachary/.cache/pypoetry/virtualenvs/MY_VENV/lib/python3.10/site-packages/langchain/chains/base.py", line 212, in ainvoke
    raise e
  File "/home/zachary/.cache/pypoetry/virtualenvs/MY_VENV/lib/python3.10/site-packages/langchain/chains/base.py", line 203, in ainvoke
    await self._acall(inputs, run_manager=run_manager)
  File "/home/zachary/.cache/pypoetry/virtualenvs/MY_VENV/lib/python3.10/site-packages/langchain/chains/conversational_retrieval/base.py", line 207, in _acall
    docs = await self._aget_docs(new_question, inputs, run_manager=_run_manager)
  File "/home/zachary/.cache/pypoetry/virtualenvs/MY_VENV/lib/python3.10/site-packages/langchain/chains/conversational_retrieval/base.py", line 333, in _aget_docs
    return self._reduce_tokens_below_limit(docs)
  File "/home/zachary/.cache/pypoetry/virtualenvs/MY_VENV/lib/python3.10/site-packages/langchain/chains/conversational_retrieval/base.py", line 298, in _reduce_tokens_below_limit
    tokens = [
  File "/home/zachary/.cache/pypoetry/virtualenvs/MY_VENV/lib/python3.10/site-packages/langchain/chains/conversational_retrieval/base.py", line 299, in <listcomp>
    self.combine_docs_chain.llm_chain._get_num_tokens(doc.page_content)
  File "/home/zachary/.cache/pypoetry/virtualenvs/MY_VENV/lib/python3.10/site-packages/langchain/chains/llm.py", line 384, in _get_num_tokens
    return _get_language_model(self.llm).get_num_tokens(text)
  File "/home/zachary/.cache/pypoetry/virtualenvs/MY_VENV/lib/python3.10/site-packages/langchain_cohere/chat_models.py", line 366, in get_num_tokens
    return len(self.client.tokenize(text=text).tokens)
TypeError: Client.tokenize() missing 1 required keyword-only argument: 'model'

Background

This PR made the model: str parameter to tokenize() mandatory, and is included starting in cohere-python v5.2.0.

I was using cohere-python v5.2.5, langchain-core v0.1.42, langchain-community v0.0.32.

langchain-cohere is calling the function without the argument (link), which then throws an error:

    def get_num_tokens(self, text: str) -> int:
        """Calculate number of tokens."""
        return len(self.client.tokenize(text=text).tokens)

Suggested Fix

The Cohere documentation still says that this argument is optional, even though it isn't. If it is indeed mandatory, then the fix is probably to choose a reasonable default while allowing the user to override.

Thanks, I'm happy to provide more info as needed.

Cannot run example code.

I was trying to run an example code, on Cohere's website at https://docs.cohere.com/docs/cohere-and-langchain

##This is the code

from pprint import pprint

from langchain_cohere import ChatCohere, CohereRagRetriever
from langchain_core.documents import Document

user_query = "Does LangChain support Cohere RAG?"
llm = ChatCohere()
rag = CohereRagRetriever(llm=llm, connectors=[])
docs = rag.get_relevant_documents(
user_query,
documents=[
Document(page_content="LangChain supports Cohere RAG!"),
Document(page_content="The sky is blue!"),
],
)
answer = docs.pop()

pprint("Relevant documents:")
pprint(docs)

pprint(f"Question: {user_query}")
pprint("Answer:")
pprint(answer.page_content)
pprint(answer.metadata["citations"])

##And this is the error I am getting

Traceback (most recent call last):
File "c:\Users\samar\Desktop\Assistant\COHERETRYOUTSS.py", line 57, in
docs = rag.get_relevant_documents(
File "C:\Users\samar\Desktop\Assistant\RA\lib\site-packages\langchain_core\retrievers.py", line 321, in get_relevant_documents
raise e
File "C:\Users\samar\Desktop\Assistant\RA\lib\site-packages\langchain_core\retrievers.py", line 314, in get_relevant_documents
result = self._get_relevant_documents(
File "C:\Users\samar\Desktop\Assistant\RA\lib\site-packages\langchain_cohere\rag_retrievers.py", line 82, in _get_relevant_documents
return _get_docs(res)
File "C:\Users\samar\Desktop\Assistant\RA\lib\site-packages\langchain_cohere\rag_retrievers.py", line 38, in _get_docs
"token_count": response.generation_info["token_count"],
KeyError: 'token_count'
PS C:\Users\samar\Desktop\Assistant>

Please help. Thank you

import error

ImportError Traceback (most recent call last)
in <cell line: 1>()
----> 1 from langchain_cohere import ChatCohere
2 from langchain_core.messages import HumanMessage
3 from langgraph.graph import END, MessageGraph
4
5 model = ChatCohere(temperature=0)

2 frames
/usr/local/lib/python3.10/dist-packages/langchain_cohere/cohere_agent.py in
2 from typing import Any, Callable, Dict, List, Sequence, Tuple, Type, Union
3
----> 4 from cohere.types import (
5 ChatRequestToolResultsItem,
6 Tool,

ImportError: cannot import name 'ChatRequestToolResultsItem' from 'cohere.types' (/usr/local/lib/python3.10/dist-packages/cohere/types/init.py)


NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.

Simple script no longer works in 0.2.0 (ValueError: could not convert string to float: 'float_')

from langchain_cohere import CohereEmbeddings
embeddings = CohereEmbeddings(model="embed-multilingual-v3.0")
emb = embeddings.embed_query("Hello, world!")

This script works in versions before 0.2.0. In 0.2.0 it produces the error:

ValueError: could not convert string to float: 'float_'
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[4], [line 4](vscode-notebook-cell:?execution_count=4&line=4)
      [1](vscode-notebook-cell:?execution_count=4&line=1) from langchain_cohere import CohereEmbeddings
      [2](vscode-notebook-cell:?execution_count=4&line=2) embeddings = CohereEmbeddings(model="embed-multilingual-v3.0")
----> [4](vscode-notebook-cell:?execution_count=4&line=4) emb = embeddings.embed_query("Hello, world!")

File ~/.local/lib/python3.10/site-packages/langchain_cohere/embeddings.py:193, in CohereEmbeddings.embed_query(self, text)
    [184](https://vscode-remote+ssh-002dremote-002bthyme.vscode-resource.vscode-cdn.net/home/gavin/jupyter/~/.local/lib/python3.10/site-packages/langchain_cohere/embeddings.py:184) def embed_query(self, text: str) -> List[float]:
    [185](https://vscode-remote+ssh-002dremote-002bthyme.vscode-resource.vscode-cdn.net/home/gavin/jupyter/~/.local/lib/python3.10/site-packages/langchain_cohere/embeddings.py:185)     """Call out to Cohere's embedding endpoint.
    [186](https://vscode-remote+ssh-002dremote-002bthyme.vscode-resource.vscode-cdn.net/home/gavin/jupyter/~/.local/lib/python3.10/site-packages/langchain_cohere/embeddings.py:186) 
    [187](https://vscode-remote+ssh-002dremote-002bthyme.vscode-resource.vscode-cdn.net/home/gavin/jupyter/~/.local/lib/python3.10/site-packages/langchain_cohere/embeddings.py:187)     Args:
   (...)
    [191](https://vscode-remote+ssh-002dremote-002bthyme.vscode-resource.vscode-cdn.net/home/gavin/jupyter/~/.local/lib/python3.10/site-packages/langchain_cohere/embeddings.py:191)         Embeddings for the text.
    [192](https://vscode-remote+ssh-002dremote-002bthyme.vscode-resource.vscode-cdn.net/home/gavin/jupyter/~/.local/lib/python3.10/site-packages/langchain_cohere/embeddings.py:192)     """
--> [193](https://vscode-remote+ssh-002dremote-002bthyme.vscode-resource.vscode-cdn.net/home/gavin/jupyter/~/.local/lib/python3.10/site-packages/langchain_cohere/embeddings.py:193)     return self.embed([text], input_type="search_query")[0]

File ~/.local/lib/python3.10/site-packages/langchain_cohere/embeddings.py:143, in CohereEmbeddings.embed(self, texts, input_type)
    [130](https://vscode-remote+ssh-002dremote-002bthyme.vscode-resource.vscode-cdn.net/home/gavin/jupyter/~/.local/lib/python3.10/site-packages/langchain_cohere/embeddings.py:130) def embed(
    [131](https://vscode-remote+ssh-002dremote-002bthyme.vscode-resource.vscode-cdn.net/home/gavin/jupyter/~/.local/lib/python3.10/site-packages/langchain_cohere/embeddings.py:131)     self,
    [132](https://vscode-remote+ssh-002dremote-002bthyme.vscode-resource.vscode-cdn.net/home/gavin/jupyter/~/.local/lib/python3.10/site-packages/langchain_cohere/embeddings.py:132)     texts: List[str],
    [133](https://vscode-remote+ssh-002dremote-002bthyme.vscode-resource.vscode-cdn.net/home/gavin/jupyter/~/.local/lib/python3.10/site-packages/langchain_cohere/embeddings.py:133)     *,
    [134](https://vscode-remote+ssh-002dremote-002bthyme.vscode-resource.vscode-cdn.net/home/gavin/jupyter/~/.local/lib/python3.10/site-packages/langchain_cohere/embeddings.py:134)     input_type: typing.Optional[cohere.EmbedInputType] = None,
    [135](https://vscode-remote+ssh-002dremote-002bthyme.vscode-resource.vscode-cdn.net/home/gavin/jupyter/~/.local/lib/python3.10/site-packages/langchain_cohere/embeddings.py:135) ) -> List[List[float]]:
    [136](https://vscode-remote+ssh-002dremote-002bthyme.vscode-resource.vscode-cdn.net/home/gavin/jupyter/~/.local/lib/python3.10/site-packages/langchain_cohere/embeddings.py:136)     embeddings = self.embed_with_retry(
    [137](https://vscode-remote+ssh-002dremote-002bthyme.vscode-resource.vscode-cdn.net/home/gavin/jupyter/~/.local/lib/python3.10/site-packages/langchain_cohere/embeddings.py:137)         model=self.model,
    [138](https://vscode-remote+ssh-002dremote-002bthyme.vscode-resource.vscode-cdn.net/home/gavin/jupyter/~/.local/lib/python3.10/site-packages/langchain_cohere/embeddings.py:138)         texts=texts,
   (...)
    [141](https://vscode-remote+ssh-002dremote-002bthyme.vscode-resource.vscode-cdn.net/home/gavin/jupyter/~/.local/lib/python3.10/site-packages/langchain_cohere/embeddings.py:141)         embedding_types=self.embedding_types,
    [142](https://vscode-remote+ssh-002dremote-002bthyme.vscode-resource.vscode-cdn.net/home/gavin/jupyter/~/.local/lib/python3.10/site-packages/langchain_cohere/embeddings.py:142)     ).embeddings
--> [143](https://vscode-remote+ssh-002dremote-002bthyme.vscode-resource.vscode-cdn.net/home/gavin/jupyter/~/.local/lib/python3.10/site-packages/langchain_cohere/embeddings.py:143)     return [list(map(float, e)) for e in embeddings]

File ~/.local/lib/python3.10/site-packages/langchain_cohere/embeddings.py:143, in <listcomp>(.0)
    [130](https://vscode-remote+ssh-002dremote-002bthyme.vscode-resource.vscode-cdn.net/home/gavin/jupyter/~/.local/lib/python3.10/site-packages/langchain_cohere/embeddings.py:130) def embed(
    [131](https://vscode-remote+ssh-002dremote-002bthyme.vscode-resource.vscode-cdn.net/home/gavin/jupyter/~/.local/lib/python3.10/site-packages/langchain_cohere/embeddings.py:131)     self,
    [132](https://vscode-remote+ssh-002dremote-002bthyme.vscode-resource.vscode-cdn.net/home/gavin/jupyter/~/.local/lib/python3.10/site-packages/langchain_cohere/embeddings.py:132)     texts: List[str],
    [133](https://vscode-remote+ssh-002dremote-002bthyme.vscode-resource.vscode-cdn.net/home/gavin/jupyter/~/.local/lib/python3.10/site-packages/langchain_cohere/embeddings.py:133)     *,
    [134](https://vscode-remote+ssh-002dremote-002bthyme.vscode-resource.vscode-cdn.net/home/gavin/jupyter/~/.local/lib/python3.10/site-packages/langchain_cohere/embeddings.py:134)     input_type: typing.Optional[cohere.EmbedInputType] = None,
    [135](https://vscode-remote+ssh-002dremote-002bthyme.vscode-resource.vscode-cdn.net/home/gavin/jupyter/~/.local/lib/python3.10/site-packages/langchain_cohere/embeddings.py:135) ) -> List[List[float]]:
    [136](https://vscode-remote+ssh-002dremote-002bthyme.vscode-resource.vscode-cdn.net/home/gavin/jupyter/~/.local/lib/python3.10/site-packages/langchain_cohere/embeddings.py:136)     embeddings = self.embed_with_retry(
    [137](https://vscode-remote+ssh-002dremote-002bthyme.vscode-resource.vscode-cdn.net/home/gavin/jupyter/~/.local/lib/python3.10/site-packages/langchain_cohere/embeddings.py:137)         model=self.model,
    [138](https://vscode-remote+ssh-002dremote-002bthyme.vscode-resource.vscode-cdn.net/home/gavin/jupyter/~/.local/lib/python3.10/site-packages/langchain_cohere/embeddings.py:138)         texts=texts,
   (...)
    [141](https://vscode-remote+ssh-002dremote-002bthyme.vscode-resource.vscode-cdn.net/home/gavin/jupyter/~/.local/lib/python3.10/site-packages/langchain_cohere/embeddings.py:141)         embedding_types=self.embedding_types,
    [142](https://vscode-remote+ssh-002dremote-002bthyme.vscode-resource.vscode-cdn.net/home/gavin/jupyter/~/.local/lib/python3.10/site-packages/langchain_cohere/embeddings.py:142)     ).embeddings
--> [143](https://vscode-remote+ssh-002dremote-002bthyme.vscode-resource.vscode-cdn.net/home/gavin/jupyter/~/.local/lib/python3.10/site-packages/langchain_cohere/embeddings.py:143)     return [list(map(float, e)) for e in embeddings]

ValueError: could not convert string to float: 'float_'

CohereRagRetriever.get_relevant_documents (and its async version) crash when used

I was trying to use this library to implement Cohere RAG, but have encountered two errors.

Error when no relevant documents are found

One occurs when no relevant documents are found:

rag = CohereRagRetriever(llm=ChatCohere(model='command-r-plus'))
docs = rag.get_relevant_documents(
    'Give me a list of leap years that have not yet happened.',
    documents=[Document('These documents'), Document('are not relevant')],
)

Gives the error:

  File ".../langchain_core/retrievers.py", line 321, in get_relevant_documents
    raise e
  File ".../langchain_core/retrievers.py", line 314, in get_relevant_documents
    result = self._get_relevant_documents(
  File ".../langchain_cohere/rag_retrievers.py", line 82, in _get_relevant_documents
    return _get_docs(res)
  File ".../langchain_cohere/rag_retrievers.py", line 23, in _get_docs
    and len(response.generation_info["documents"]) > 0
TypeError: object of type 'NoneType' has no len()

This line fails because documents is defaulted to None, not an empty list, and therefore doesn't support the len() method.

Changing to a simple boolean check of

    if (
        "documents" in response.generation_info
        and response.generation_info["documents"]
    ):

could be a way to fix this.

Error when documents are found

Running with relevant documents lets it find something and skip the above error:

rag = CohereRagRetriever(llm=ChatCohere(model='command-r-plus'))
docs = rag.get_relevant_documents(
    'Give me a list of leap years that have not yet happened.',
    documents=[Document('Past Leap Years: 2016, 2020, 2024'), Document('Upcoming Leap Years: 2028, 2032, 2036')],
)

but it gives a later error:

  File ".../langchain_core/retrievers.py", line 384, in aget_relevant_documents
    raise e
  File ".../langchain_core/retrievers.py", line 377, in aget_relevant_documents
    result = await self._aget_relevant_documents(
  File ".../langchain_cohere/rag_retrievers.py", line 102, in _aget_relevant_documents
    return _get_docs(res)
  File ".../langchain_cohere/rag_retrievers.py", line 38, in _get_docs
    "token_count": response.generation_info["token_count"],

This seems to be because the response returned by the ChatModel now stores token count in response.meta rather than in response.token_count so it's not set here and then crashes in rag_retrievers at the line specified above.

This was tested all tested with langchain-cohere 0.1.3 (latest) and langchain-core versions 0.1.43 and 0.1.44.

ChatCohere does not work with asynchronous operations in 0.1.8

In the current released version, your asynchronous operation _agenerate() is a blocking call.

However, someone has fixed this already - 038cded

But you have yet to release a new package with the fix.

When will the new package be released so that ChatCohere can be used asynchronously?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.