Skip to content

fix: rename self._client to self.client in MyLLM class#588

Open
edmundjj626-arch wants to merge 1 commit into
datawhalechina:mainfrom
edmundjj626-arch:fix/my-llm-client-attribute
Open

fix: rename self._client to self.client in MyLLM class#588
edmundjj626-arch wants to merge 1 commit into
datawhalechina:mainfrom
edmundjj626-arch:fix/my-llm-client-attribute

Conversation

@edmundjj626-arch
Copy link
Copy Markdown

问题描述

第七章代码 code/chapter7/my_llm.py 中存在一个变量命名错误,如果按照教程使用4.1.3创建的HelloAgentsLLM并继承会导致当使用 provider="modelscope" 时,调用 think() 方法会抛出 AttributeError

父类 HelloAgentsLLM(hello_agents.py)

class HelloAgentsLLM:
    def __init__(self, ...):
        self.client = OpenAI(api_key=apiKey, base_url=baseUrl, timeout=timeout)

    def think(self, messages, temperature=0):
        response = self.client.chat.completions.create(...)  # 访问 self.client

子类 MyLLM(my_llm.py)

class MyLLM(HelloAgentsLLM):
    def __init__(self, ...):
        if provider == "modelscope":
            # ⚠️ Bug: 使用 self._client 而非 self.client
            self._client = OpenAI(api_key=self.api_key, base_url=self.base_url, timeout=self.timeout)
        else:
            super().__init__(...)  # 只有这个分支调用父类

Fixes: code/chapter7/my_llm.py, docs/chapter7/*.md

The MyLLM subclass uses self._client when provider="modelscope", but the
parent class HelloAgentsLLM uses self.client. This causes AttributeError
when calling think() because self.client is never initialized.

Fixes: code/chapter7/my_llm.py, docs/chapter7/*.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant