microsoft / autogen

A programming framework for agentic AI. Discord: https://aka.ms/autogen-dc. Roadmap: https://aka.ms/autogen-roadmap
https://microsoft.github.io/autogen/
Creative Commons Attribution 4.0 International
29.22k stars 4.26k forks source link

Error occurs when I'm trying the demo "agentchat_RetrieveChat.ipynb". #370

Closed lcneyc closed 4 months ago

lcneyc commented 9 months ago
  1. Sentence_transformers already exists in my environment, autogen still print “The sentence_transformers python package is not installed. Please install it with pip install sentence_transformers“.
  2. After the above message, the code terminated because of the error below( I've already generated the "reference" folder by running pydoc-markdown) : ragproxyagent.initiate_chat(assistant, problem=code_problem, search_string="spark")
    
    File "/home/ecadi/.conda/envs/py39/lib/python3.9/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
    File "/home/ecadi/.conda/envs/py39/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
    File "/home/ecadi/.vscode-server/extensions/ms-python.python-2023.14.0/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher/../../debugpy/__main__.py", line 39, in <module>
    cli.main()
    File "/home/ecadi/.vscode-server/extensions/ms-python.python-2023.14.0/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 430, in main
    run()
    File "/home/ecadi/.vscode-server/extensions/ms-python.python-2023.14.0/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 284, in run_file
    runpy.run_path(target, run_name="__main__")
    File "/home/ecadi/.vscode-server/extensions/ms-python.python-2023.14.0/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 321, in run_path
    return _run_module_code(code, init_globals, run_name,
    File "/home/ecadi/.vscode-server/extensions/ms-python.python-2023.14.0/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 135, in _run_module_code
    _run_code(code, mod_globals, init_globals,
    File "/home/ecadi/.vscode-server/extensions/ms-python.python-2023.14.0/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 124, in _run_code
    exec(code, run_globals)
    File "/home/ecadi/lc/autogen/agentchat_RetrieveChat.py", line 171, in <module>
    ragproxyagent.initiate_chat(assistant, problem=code_problem, search_string="spark")  # search_string is used as an extra filter for the embeddings search, in this case, we only want to search documents that contain "spark".
    File "/home/ecadi/lc/autogen/venv/lib/python3.9/site-packages/autogen/agentchat/conversable_agent.py", line 531, in initiate_chat
    self.send(self.generate_init_message(**context), recipient, silent=silent)
    File "/home/ecadi/lc/autogen/venv/lib/python3.9/site-packages/autogen/agentchat/contrib/retrieve_user_proxy_agent.py", line 403, in generate_init_message
    self.retrieve_docs(problem, n_results, search_string)
    File "/home/ecadi/lc/autogen/venv/lib/python3.9/site-packages/autogen/agentchat/contrib/retrieve_user_proxy_agent.py", line 379, in retrieve_docs
    results = query_vector_db(
    File "/home/ecadi/lc/autogen/venv/lib/python3.9/site-packages/autogen/retrieve_utils.py", line 385, in query_vector_db
    collection = client.get_collection(collection_name)
    File "/home/ecadi/lc/autogen/venv/lib/python3.9/site-packages/chromadb/api/segment.py", line 199, in get_collection
    raise ValueError(f"Collection {name} does not exist.")
    ValueError: Collection autogen-docs does not exist.
sonichi commented 9 months ago

@thinkall for awareness

thinkall commented 9 months ago

Hi @lcneyc , I see you were running the code in debug mode with vscode. Did you set the default python in launch.json under .vscode? It's possible that the default python in debug is different from the one in conda.

lcneyc commented 9 months ago

Hi @thinkall , it's same running in the default mode. Thanks

thinkall commented 9 months ago

Hi @thinkall , it's same running in the default mode. Thanks

Could you please share the code? Thanks.

lcneyc commented 9 months ago

For some problems about proxy in jupyter notebook, I converted the code into ".py" file to run in the terminal. Code:

import autogen

config_list = [
    {
        'model': 'gpt-4',
        'api_key': 'sk-xxxx',
    },
]
assert len(config_list) > 0
print("models to use: ", [config_list[i]["model"] for i in range(len(config_list))])

# It first looks for environment variable "OAI_CONFIG_LIST" which needs to be a valid json string. If that variable is not found, it then looks for a json file named "OAI_CONFIG_LIST". It filters the configs by models (you can filter by other keys as well). Only the gpt-4 and gpt-3.5-turbo models are kept in the list based on the filter condition.
# 
# The config list looks like the following:
# ```python
# config_list = [
#     {
#         'model': 'gpt-4',
#         'api_key': '<your OpenAI API key here>',
#     },
#     {
#         'model': 'gpt-4',
#         'api_key': '<your Azure OpenAI API key here>',
#         'api_base': '<your Azure OpenAI API base here>',
#         'api_type': 'azure',
#         'api_version': '2023-06-01-preview',
#     },
#     {
#         'model': 'gpt-3.5-turbo',
#         'api_key': '<your Azure OpenAI API key here>',
#         'api_base': '<your Azure OpenAI API base here>',
#         'api_type': 'azure',
#         'api_version': '2023-06-01-preview',
#     },
# ]
# ```
# 
# If you open this notebook in colab, you can upload your files by clicking the file icon on the left panel and then choose "upload file" icon.
# 
# You can set the value of config_list in other ways you prefer, e.g., loading from a YAML file.

# ## Construct agents for RetrieveChat
# 
# We start by initialzing the `RetrieveAssistantAgent` and `RetrieveUserProxyAgent`. The system message needs to be set to "You are a helpful assistant." for RetrieveAssistantAgent. The detailed instructions are given in the user message. Later we will use the `RetrieveUserProxyAgent.generate_init_prompt` to combine the instructions and a retrieval augmented generation task for an initial prompt to be sent to the LLM assistant.

# In[13]:

# Accepted file formats for that can be stored in 
# a vector database instance
from autogen.retrieve_utils import TEXT_FORMATS

print("Accepted file formats for `docs_path`:")
print(TEXT_FORMATS)

# In[14]:

from autogen.agentchat.contrib.retrieve_assistant_agent import RetrieveAssistantAgent
from autogen.agentchat.contrib.retrieve_user_proxy_agent import RetrieveUserProxyAgent
import chromadb

autogen.ChatCompletion.start_logging()

# 1. create an RetrieveAssistantAgent instance named "assistant"
assistant = RetrieveAssistantAgent(
    name="assistant", 
    system_message="You are a helpful assistant.",
    llm_config={
        "request_timeout": 600,
        "seed": 62,
        "config_list": config_list,
    },
)

# 2. create the RetrieveUserProxyAgent instance named "ragproxyagent"
# By default, the human_input_mode is "ALWAYS", which means the agent will ask for human input at every step. We set it to "NEVER" here.
# `docs_path` is the path to the docs directory. By default, it is set to "./docs". Here we generated the documentations from FLAML's docstrings.
# Navigate to the website folder and run `pydoc-markdown` and it will generate folder `reference` under `website/docs`.
# `task` indicates the kind of task we're working on. In this example, it's a `code` task.
# `chunk_token_size` is the chunk token size for the retrieve chat. By default, it is set to `max_tokens * 0.6`, here we set it to 2000.
ragproxyagent = RetrieveUserProxyAgent(
    name="ragproxyagent",
    human_input_mode="NEVER",
    max_consecutive_auto_reply=10,
    retrieve_config={
        "task": "code",
        "docs_path": "./autogen/website/docs/reference",
        "chunk_token_size": 2000,
        "model": config_list[0]["model"],
        "client": chromadb.PersistentClient(path="/tmp/chromadb"),
        "embedding_model": "all-mpnet-base-v2",
    },
)

# <a id="example-1"></a>
# ### Example 1
# 
# [back to top](#toc)
# 
# Use RetrieveChat to help generate sample code and automatically run the code and fix errors if there is any.
# 
# Problem: Which API should I use if I want to use FLAML for a classification task and I want to train the model in 30 seconds. Use spark to parallel the training. Force cancel jobs if time limit is reached.

# In[6]:

# reset the assistant. Always reset the assistant before starting a new conversation.
assistant.reset()

# given a problem, we use the ragproxyagent to generate a prompt to be sent to the assistant as the initial message.
# the assistant receives the message and generates a response. The response will be sent back to the ragproxyagent for processing.
# The conversation continues until the termination condition is met, in RetrieveChat, the termination condition when no human-in-loop is no code block detected.
# With human-in-loop, the conversation will continue until the user says "exit".
code_problem = "How can I use FLAML to perform a classification task and use spark to do parallel training. Train 30 seconds and force cancel jobs if time limit is reached."
ragproxyagent.initiate_chat(assistant, problem=code_problem, search_string="spark")  # search_string is used as an extra filter for the embeddings search, in this case, we only want to search documents that contain "spark".

Thanks. @thinkall

Melo96 commented 9 months ago

Hi, I encountered the same issue today. Fix it by change line 385 in query_vector_db from: collection = client.get_collection(collection_name) To: collection = client.get_or_create_collection(collection_name) Hope it helps!

lcneyc commented 9 months ago

Hi, @Melo96 @thinkall Now it works well. Here is the solution summary: Q1:Sentence_transformers already exists in my environment, autogen still print “The sentence_transformers python package is not installed. Please install it with pip install sentence_transformers“. A1:It occurs when executing the code in embedding_fuction.py [Line 38]

try:
    from sentence_transformers import SentenceTransformer
except ImportError: 
    raise ValueError(
              "The sentence_transformers python package is not installed. Please install it with `pip install sentence_transformers`"
                )

The reason is somthing wrong with CUDA12.0 and pytorch2.1.0, and it occurs ImportError. Here's the solution:https://github.com/pytorch/pytorch/issues/111469#issuecomment-1773937884 Q2:ValueError: Collection autogen-docs does not exist. A2: @Melo96 's comment help a lot! Q3:No more context, will terminate. A3:rm -rf /tmp/chromadb for each retry.

thinkall commented 9 months ago

Hi @Melo96 , @lcneyc , I've updated the notebook which has more clear comments on how to generate the documentations, please check it out.

And for recreating the collection, you just need to set get_or_create to be True in the retrieve_config.

For no more context issue, make sure you have set the docs path correctly and updated the question accordingly. For instance, you may don't have any doc contains "spark", then search string should be unsetted.

ithllc commented 7 months ago

@thinkall BTW, this is still an issue and I was sure that the "get_or_create" argument was set to "True". I downloaded and used the Autogen from Github as of December 22nd, 2023, so at least from that date what @Melo96 states is still valid because when I made the change in the "retrieve_utils.py" file for the collection as per the suggestion listed here, I reran it and it worked fine.

Also, I was using LM Studio as my LLM config. I hope this helps.

AsafEitani commented 3 months ago

Hi, This still happens in main. I changed this line locally to use get_or_create_collection: https://github.com/microsoft/autogen/blob/main/autogen/retrieve_utils.py#L470C1-L471C1

Is there a plan to fix this? @thinkall

thinkall commented 3 months ago

Hi, This still happens in main. I changed this line locally to use get_or_create_collection: https://github.com/microsoft/autogen/blob/main/autogen/retrieve_utils.py#L470C1-L471C1

Is there a plan to fix this? @thinkall

Hi @AsafEitani , could you create a new issue and describe the steps for reproducing? Thanks.

thinkall commented 3 months ago

Also, please upgrade AutoGen to the latest version if possible.