You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

类方法中无法将self.client设为默认参数的问题求助

类方法中无法将self.client设为默认参数的问题求助

我有一个类和方法,尝试把self.instance_variable作为默认参数传递,但一直做不到。我举个例子说明:

from openai import OpenAI

class Example_class:
    def __init__(self) -> None:
        self.client = OpenAI(api_key='xyz')
        self.client2 = OpenAI(api_key='abc')
    
    def chat_completion(self, prompt, context, client=self.client, model='gpt-4o'):
        # Process the prompt
        messages = [{"role": "system", "content": context}, {"role": "user", "content": prompt}]
        response = client.chat.completions.create(
            model=model,
            messages=messages,
            temperature=0.35, # this is the degree of randomness of the model's output
        )
        return response.choices[0].message.content
    
    def do_something(self):
        self.chat_completion(prompt="blah blah blah", context="fgasa")

你看,在尝试把self.client传入chat_completion方法时出现了错误,我哪里出错了?

备注:内容来源于stack exchange,提问作者Legion

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.15 10:49:50