run-llama / llama_index

LlamaIndex is a data framework for your LLM applications
https://docs.llamaindex.ai
MIT License
37.05k stars 5.32k forks source link

[Bug]: 2024-09-23 00:06:33,027 - WARNING - Retrying llama_index.llms.openai.base.OpenAI._achat in 0.6544275664914755 seconds as it raised APIConnectionError: Connection error.. 2024-09-23 00:06:54,743 - WARNING - Retrying llama_index.llms.openai.base.OpenAI._achat in 0.10014551963451379 seconds as it raised APIConnectionError: Connection error.. Observation: Error: Connection error. #16155

Open yxjun1984 opened 2 months ago

yxjun1984 commented 2 months ago

Bug Description

async def generate_reply_content_async(content, original_message): try:

检查缓存

    cache_key = hash(content)
    if cache_key in query_cache:
        logger.info("使用缓存的回复")
        return query_cache[cache_key]

    if not os.path.exists("F:\index_storage"):
        raise FileNotFoundError("索引文件夹不存在,请先运行 vectorization.py 脚本创建索引。")

    index = load_index()
    query_engine = index.as_query_engine(similarity_top_k=5)

    query_engine_tools = [
        QueryEngineTool(
            query_engine=query_engine,
            metadata=ToolMetadata(
                name="PR_Expert",
                description="Intended to offer advice and responses from senior B2B overseas sales experts.",
            ),
        ),
    ]

    llm = OpenAI(
        model="gpt-4o-mini",
        api_key=os.getenv("OPENAI_API_KEY"),
        api_base="https://api.100dog.com/v1",
        timeout=600
    )
    agent = ReActAgent.from_tools(query_engine_tools, llm=llm, verbose=True)

    prompt = f"""
    You are a public relation expert and expert in communication with an experience of more than 30 years in this field.
    I will give you some inputs and you will write professional reply to my client to be sent by email.

    Original email content:
    {content}

    Please craft a professional, respectful, and persuasive response, without including an email signature.
    """

    reply_content = await make_openai_request(agent, prompt)
    # reply_content = await llm.acomplete(prompt)

    full_reply = f"{reply_content}\n\n--- Original email content ---\n{original_message}\n\n"

    signature = """
-------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------------
    """
    full_reply += signature

    # 保存到缓存
    query_cache[cache_key] = full_reply

    return full_reply
except Exception as e:
    logger.error(f"生成回复内容时发生错误:{str(e)}")
    return None

Version

llama-index 0.11.11

Steps to Reproduce

i do not know

Relevant Logs/Tracbacks

2024-09-22 23:46:00,566 - INFO - 开始处理文件夹: P:\BaiduNetdiskDownload\emails_cleaneds_02\邮件来往处理后邮件地址替换3
2024-09-22 23:48:45,499 - INFO - 成功处理文档,获取到 2 条邮件数据
2024-09-22 23:48:45,500 - INFO - 准备发送邮件至: Muhammad Salamah <yxjun1984@gmail.com>
2024-09-22 23:48:45,500 - INFO - 正在尝试连接到 smtpout.secureserver.net:465
2024-09-22 23:48:46,040 - INFO - 成功连接到SMTP服务器
2024-09-22 23:48:46,392 - INFO - 成功登录
2024-09-22 23:48:47,654 - INFO - 邮件已成功发送到 Muhammad Salamah <yxjun1984@gmail.com>
2024-09-22 23:48:47,825 - INFO - 已发送邮件至: Muhammad Salamah <yxjun1984@gmail.com>
2024-09-22 23:48:47,830 - INFO - 准备发送邮件至: Hamza BENASSBA <yxjun1984@gmail.com>
2024-09-22 23:48:47,830 - INFO - 正在尝试连接到 smtpout.secureserver.net:465
2024-09-22 23:48:48,364 - INFO - 成功连接到SMTP服务器
2024-09-22 23:48:48,721 - INFO - 成功登录
2024-09-22 23:48:50,439 - INFO - 邮件已成功发送到 Hamza BENASSBA <yxjun1984@gmail.com>
2024-09-22 23:48:50,620 - INFO - 已发送邮件至: Hamza BENASSBA <yxjun1984@gmail.com>
2024-09-22 23:48:50,620 - INFO - 所有邮件处理完毕
2024-09-22 23:48:50,620 - INFO - 
        性能报告:
        总运行时间: 170.05 秒
        处理的邮件总数: 2
        成功发送的邮件数: 2
        发送失败的邮件数: 0
        成功率: 100.00%

2024-09-22 23:56:41,686 - INFO - 开始处理文件夹: P:\BaiduNetdiskDownload\emails_cleaneds_02\邮件来往处理后邮件地址替换3
2024-09-22 23:57:08,046 - INFO - 成功处理文档,获取到 2 条邮件数据
2024-09-22 23:57:08,047 - INFO - 准备发送邮件至: Muhammad Salamah <yxjun1984@gmail.com>
2024-09-22 23:57:08,047 - INFO - 正在尝试连接到 smtpout.secureserver.net:465
2024-09-22 23:57:08,593 - INFO - 成功连接到SMTP服务器
2024-09-22 23:57:08,954 - INFO - 成功登录
2024-09-22 23:57:10,238 - INFO - 邮件已成功发送到 Muhammad Salamah <yxjun1984@gmail.com>
2024-09-22 23:57:10,415 - INFO - 已发送邮件至: Muhammad Salamah <yxjun1984@gmail.com>
2024-09-22 23:57:10,415 - INFO - 准备发送邮件至: Hamza BENASSBA <yxjun1984@gmail.com>
2024-09-22 23:57:10,416 - INFO - 正在尝试连接到 smtpout.secureserver.net:465
2024-09-22 23:57:11,878 - INFO - 成功连接到SMTP服务器
2024-09-22 23:57:12,225 - INFO - 成功登录
2024-09-22 23:57:13,931 - INFO - 邮件已成功发送到 Hamza BENASSBA <yxjun1984@gmail.com>
2024-09-22 23:57:14,100 - INFO - 已发送邮件至: Hamza BENASSBA <yxjun1984@gmail.com>
2024-09-22 23:57:14,100 - INFO - 所有邮件处理完毕
2024-09-22 23:57:14,100 - INFO - 
        性能报告:
        总运行时间: 32.41 秒
        处理的邮件总数: 2
        成功发送的邮件数: 2
        发送失败的邮件数: 0
        成功率: 100.00%

2024-09-22 23:57:29,210 - INFO - 开始处理文件夹: P:\BaiduNetdiskDownload\emails_cleaneds_02\邮件来往处理后邮件地址替换3
2024-09-22 23:57:47,108 - INFO - 成功处理文档,获取到 2 条邮件数据
2024-09-22 23:57:47,109 - INFO - 准备发送邮件至: Muhammad Salamah <yxjun1984@gmail.com>
2024-09-22 23:57:47,109 - INFO - 正在尝试连接到 smtpout.secureserver.net:465
2024-09-22 23:57:47,866 - INFO - 成功连接到SMTP服务器
2024-09-22 23:57:48,220 - INFO - 成功登录
2024-09-22 23:57:49,496 - INFO - 邮件已成功发送到 Muhammad Salamah <yxjun1984@gmail.com>
2024-09-22 23:57:49,671 - INFO - 已发送邮件至: Muhammad Salamah <yxjun1984@gmail.com>
2024-09-22 23:57:49,672 - INFO - 准备发送邮件至: Hamza BENASSBA <yxjun1984@gmail.com>
2024-09-22 23:57:49,672 - INFO - 正在尝试连接到 smtpout.secureserver.net:465
2024-09-22 23:57:50,191 - INFO - 成功连接到SMTP服务器
2024-09-22 23:57:50,537 - INFO - 成功登录
2024-09-22 23:57:51,789 - INFO - 邮件已成功发送到 Hamza BENASSBA <yxjun1984@gmail.com>
2024-09-22 23:57:51,956 - INFO - 已发送邮件至: Hamza BENASSBA <yxjun1984@gmail.com>
2024-09-22 23:57:51,956 - INFO - 所有邮件处理完毕
2024-09-22 23:57:51,956 - INFO - 
        性能报告:
        总运行时间: 22.75 秒
        处理的邮件总数: 2
        成功发送的邮件数: 2
        发送失败的邮件数: 0
        成功率: 100.00%

2024-09-23 00:04:18,012 - INFO - 开始处理文件夹: P:\BaiduNetdiskDownload\emails_cleaneds_02\邮件来往处理后邮件地址替换3
2024-09-23 00:06:00,567 - INFO - 开始处理文件夹: P:\BaiduNetdiskDownload\emails_cleaneds_02\邮件来往处理后邮件地址替换3
2024-09-23 00:08:37,007 - INFO - 成功处理文档,获取到 2 条邮件数据
2024-09-23 00:08:37,008 - INFO - 准备发送邮件至: Muhammad Salamah <yxjun1984@gmail.com>
2024-09-23 00:08:37,009 - INFO - 正在尝试连接到 smtpout.secureserver.net:465
2024-09-23 00:08:37,557 - INFO - 成功连接到SMTP服务器
2024-09-23 00:08:37,919 - INFO - 成功登录
2024-09-23 00:08:39,197 - INFO - 邮件已成功发送到 Muhammad Salamah <yxjun1984@gmail.com>
2024-09-23 00:08:39,372 - INFO - 已发送邮件至: Muhammad Salamah <yxjun1984@gmail.com>
2024-09-23 00:08:39,373 - INFO - 准备发送邮件至: Hamza BENASSBA <yxjun1984@gmail.com>
2024-09-23 00:08:39,373 - INFO - 正在尝试连接到 smtpout.secureserver.net:465
2024-09-23 00:08:39,915 - INFO - 成功连接到SMTP服务器
2024-09-23 00:08:40,282 - INFO - 成功登录
2024-09-23 00:08:41,578 - INFO - 邮件已成功发送到 Hamza BENASSBA <yxjun1984@gmail.com>
2024-09-23 00:08:41,755 - INFO - 已发送邮件至: Hamza BENASSBA <yxjun1984@gmail.com>
2024-09-23 00:08:41,756 - INFO - 所有邮件处理完毕
2024-09-23 00:08:41,756 - INFO - 
        性能报告:
        总运行时间: 161.19 秒
        处理的邮件总数: 2
        成功发送的邮件数: 2
        发送失败的邮件数: 0
        成功率: 100.00%
dosubot[bot] commented 2 months ago

@yxjun1984 Hello! I'm here to help you with any bugs, questions, or contributions you have.

I found several similar issues related to APIConnectionError in the LlamaIndex repository. Here are some relevant discussions and their solutions:

  1. APIConnectionError on a custom agents implementation with AzureOpenAI (Closed):

    • Solution: Ensure that the LLM and embed model are correctly set up and passed to the necessary components. Increase the timeout and max retries for the OpenAI client [1].
  2. APIConnectionError: Connection error (Closed):

    • Solution: Set the correct environment variables for the Azure OpenAI API and ensure the model version and API version are compatible [2].

To resolve the APIConnectionError in llama_index.llms.openai.base.OpenAI._achat when generating reply content asynchronously, you can increase the timeout and max_retries parameters. These parameters are used to configure the OpenAI client and can help mitigate connection issues.

Here's how you can adjust these parameters:

  1. Increase the Timeout: Set a higher value for the timeout parameter to give the API more time to respond.
  2. Increase the Number of Retries: Set a higher value for the max_retries parameter to allow more attempts in case of connection failures.

You can do this by passing the desired values when initializing the OpenAI class:

from llama_index.llms.openai import OpenAI

# Initialize with increased timeout and max_retries
openai_llm = OpenAI(
    api_key="your_api_key",
    timeout=120.0,  # Increase timeout to 120 seconds
    max_retries=5,  # Increase max retries to 5
    reuse_client=False  # Optionally, set reuse_client to False for large volumes of async calls
)

Additionally, the llm_retry_decorator is used to handle retries with exponential backoff, which can further help in managing transient connection issues:

def llm_retry_decorator(f: Callable[[Any], Any]) -> Callable[[Any], Any]:
    @functools.wraps(f)
    def wrapper(self, *args: Any, **kwargs: Any) -> Any:
        max_retries = getattr(self, "max_retries", 0)
        if max_retries <= 0:
            return f(self, *args, **kwargs)

        retry = create_retry_decorator(
            max_retries=max_retries,
            random_exponential=True,
            stop_after_delay_seconds=60,
            min_seconds=1,
            max_seconds=20,
        )
        return retry(f)(self, *args, **kwargs)

    return wrapper

By increasing the timeout and max_retries, and utilizing the retry decorator, you can reduce the likelihood of encountering APIConnectionError during asynchronous operations [3][4][5][6].

To continue talking to Dosu, mention @dosu.