最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Unable to pass self.instance_variable as default into a class method - Stack Overflow

programmeradmin1浏览0评论

I have a class and method that I am trying to pass a self.instance_variable as default but am unable to. Let me illustrate:

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.chatpletions.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")

You see, there is an error when trying to pass self.client into the chat_completion method. Where am I going wrong?

I have a class and method that I am trying to pass a self.instance_variable as default but am unable to. Let me illustrate:

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.chatpletions.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")

You see, there is an error when trying to pass self.client into the chat_completion method. Where am I going wrong?

Share Improve this question asked Nov 20, 2024 at 10:12 LegionLegion 4742 gold badges7 silver badges17 bronze badges 3
  • Please edit your question and show the complete error message. – OldBoy Commented Nov 20, 2024 at 10:22
  • Why at all do you need to pass self.client as function argument ? – Maurice Meyer Commented Nov 20, 2024 at 10:23
  • This question is similar to: Passing in "self" as a parameter in function call in a class (Python). If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – el_grezeq Commented Nov 20, 2024 at 10:27
Add a comment  | 

1 Answer 1

Reset to default 1

You simply cannot do it like this. Understand the "self" as an argument just like the others (it really is). You cannot access it directly from the function header

One "correct" way to do this is to define a default Value as None and check for it's content :

class ExampleClass:
    def chat_completion(self, prompt, context, client=None, model='gpt-4o'):
        client = client or self.client # this will ensure client is never None.
        # Process the prompt
发布评论

评论列表(0)

  1. 暂无评论