class_docstr: Enables text generation using OpenAI's large language models (LLMs). It supports gpt-4 and gpt-3.5-turbo
  family of models.
  
  Users can pass any text generation parameters valid for the `openai.ChatCompletion.create` method
  directly to this component via the `**generation_kwargs` parameter in __init__ or the `**generation_kwargs`
  parameter in `run` method.
  
  For more details on the parameters supported by the OpenAI API, refer to the OpenAI
  [documentation](https://platform.openai.com/docs/api-reference/chat).
  
  ```python
  from haystack.components.generators import OpenAIGenerator
  client = OpenAIGenerator()
  response = client.run("What's Natural Language Processing? Be brief.")
  print(response)
  
  >> {'replies': ['Natural Language Processing (NLP) is a branch of artificial intelligence that focuses on
  >> the interaction between computers and human language. It involves enabling computers to understand, interpret,
  >> and respond to natural human language in a way that is both meaningful and useful.'], 'meta': [{'model':
  >> 'gpt-3.5-turbo-0613', 'index': 0, 'finish_reason': 'stop', 'usage': {'prompt_tokens': 16,
  >> 'completion_tokens': 49, 'total_tokens': 65}}]}
  ```
  
   Key Features and Compatibility:
       - **Primary Compatibility**: Designed to work seamlessly with gpt-4, gpt-3.5-turbo family of models.
       - **Streaming Support**: Supports streaming responses from the OpenAI API.
       - **Customizability**: Supports all parameters supported by the OpenAI API.
  
   Input and Output Format:
       - **String Format**: This component uses the strings for both input and output.
docstr: Creates an instance of OpenAIGenerator. Unless specified otherwise in the `model`, this is for OpenAI's
  GPT-3.5 model.
  
  :param api_key: The OpenAI API key. It can be explicitly provided or automatically read from the
      environment variable OPENAI_API_KEY (recommended).
  :param model: The name of the model to use.
  :param streaming_callback: A callback function that is called when a new token is received from the stream.
      The callback function accepts StreamingChunk as an argument.
  :param api_base_url: An optional base URL.
  :param organization: The Organization ID, defaults to `None`. See
  [production best practices](https://platform.openai.com/docs/guides/production-best-practices/setting-up-your-organization).
  :param system_prompt: The system prompt to use for text generation. If not provided, the system prompt is
  omitted, and the default system prompt of the model is used.
  :param generation_kwargs: Other parameters to use for the model. These parameters are all sent directly to
      the OpenAI endpoint. See OpenAI [documentation](https://platform.openai.com/docs/api-reference/chat) for
      more details.
      Some of the supported parameters:
      - `max_tokens`: The maximum number of tokens the output text can have.
      - `temperature`: What sampling temperature to use. Higher values mean the model will take more risks.
          Try 0.9 for more creative applications and 0 (argmax sampling) for ones with a well-defined answer.
      - `top_p`: An alternative to sampling with temperature, called nucleus sampling, where the model
          considers the results of the tokens with top_p probability mass. So, 0.1 means only the tokens
          comprising the top 10% probability mass are considered.
      - `n`: How many completions to generate for each prompt. For example, if the LLM gets 3 prompts and n is 2,
          it will generate two completions for each of the three prompts, ending up with 6 completions in total.
      - `stop`: One or more sequences after which the LLM should stop generating tokens.
      - `presence_penalty`: What penalty to apply if a token is already present at all. Bigger values mean
          the model will be less likely to repeat the same token in the text.
      - `frequency_penalty`: What penalty to apply if a token has already been generated in the text.
          Bigger values mean the model will be less likely to repeat the same token in the text.
      - `logit_bias`: Add a logit bias to specific tokens. The keys of the dictionary are tokens, and the
          values are the bias to add to that token.
docstr_tok: ["Creates","an","instance","of","OpenAIGenerator",".","Unless","specified","otherwise","in","the","`","model","`",",","this","is","for","OpenAI's","GPT-3.5","model",".",":","param","api_key",":","The","OpenAI","API","key",".","It","can","be","explicitly","provided","or","automatically","read","from","the","environment","variable","OPENAI_API_KEY","(","recommended",")",".",":","param","model",":","The","name","of","the","model","to","use",".",":","param","streaming_callback",":","A","callback","function","that","is","called","when","a","new","token","is","received","from","the","stream",".","The","callback","function","accepts","StreamingChunk","as","an","argument",".",":","param","api_base_url",":","An","optional","base","URL",".",":","param","organization",":","The","Organization","ID",",","defaults","to","`","None","`",".","See","[","production","best","practices","]","(","https",":","\/\/platform.openai.com\/docs\/guides\/production-best-practices\/setting-up-your-organization",")",".",":","param","system_prompt",":","The","system","prompt","to","use","for","text","generation",".","If","not","provided",",","the","system","prompt","is","omitted",",","and","the","default","system","prompt","of","the","model","is","used",".",":","param","generation_kwargs",":","Other","parameters","to","use","for","the","model",".","These","parameters","are","all","sent","directly","to","the","OpenAI","endpoint",".","See","OpenAI","[","documentation","]","(","https",":","\/\/platform.openai.com\/docs\/api-reference\/chat",")","for","more","details",".","Some","of","the","supported","parameters",":","-","`","max_tokens","`",":","The","maximum","number","of","tokens","the","output","text","can","have",".","-","`","temperature","`",":","What","sampling","temperature","to","use",".","Higher","values","mean","the","model","will","take","more","risks",".","Try","0.9","for","more","creative","applications","and","0","(","argmax","sampling",")","for","ones","with","a","well-defined","answer",".","-","`","top_p","`",":","An","alternative","to","sampling","with","temperature",",","called","nucleus","sampling",",","where","the","model","considers","the","results","of","the","tokens","with","top_p","probability","mass",".","So",",","0.1","means","only","the","tokens","comprising","the","top","10","%","probability","mass","are","considered",".","-","`","n","`",":","How","many","completions","to","generate","for","each","prompt",".","For","example",",","if","the","LLM","gets","3","prompts","and","n","is","2",",","it","will","generate","two","completions","for","each","of","the","three","prompts",",","ending","up","with","6","completions","in","total",".","-","`","stop","`",":","One","or","more","sequences","after","which","the","LLM","should","stop","generating","tokens",".","-","`","presence_penalty","`",":","What","penalty","to","apply","if","a","token","is","already","present","at","all",".","Bigger","values","mean","the","model","will","be","less","likely","to","repeat","the","same","token","in","the","text",".","-","`","frequency_penalty","`",":","What","penalty","to","apply","if","a","token","has","already","been","generated","in","the","text",".","Bigger","values","mean","the","model","will","be","less","likely","to","repeat","the","same","token","in","the","text",".","-","`","logit_bias","`",":","Add","a","logit","bias","to","specific","tokens",".","The","keys","of","the","dictionary","are","tokens",",","and","the","values","are","the","bias","to","add","to","that","token","."]
code: def __init__(
      self,
      api_key: Optional[str] = None,
      model: str = "gpt-3.5-turbo",
      streaming_callback: Optional[
          Callable[[StreamingChunk], None]
      ] = None,
      api_base_url: Optional[str] = None,
      organization: Optional[str] = None,
      system_prompt: Optional[str] = None,
      generation_kwargs: Optional[Dict[str, Any]] = None,
  ):
      """
      Creates an instance of OpenAIGenerator. Unless specified otherwise in the `model`, this is for OpenAI's
      GPT-3.5 model.
  
      :param api_key: The OpenAI API key. It can be explicitly provided or automatically read from the
          environment variable OPENAI_API_KEY (recommended).
      :param model: The name of the model to use.
      :param streaming_callback: A callback function that is called when a new token is received from the stream.
          The callback function accepts StreamingChunk as an argument.
      :param api_base_url: An optional base URL.
      :param organization: The Organization ID, defaults to `None`. See
      [production best practices](https://platform.openai.com/docs/guides/production-best-practices/setting-up-your-organization).
      :param system_prompt: The system prompt to use for text generation. If not provided, the system prompt is
      omitted, and the default system prompt of the model is used.
      :param generation_kwargs: Other parameters to use for the model. These parameters are all sent directly to
          the OpenAI endpoint. See OpenAI [documentation](https://platform.openai.com/docs/api-reference/chat) for
          more details.
          Some of the supported parameters:
          - `max_tokens`: The maximum number of tokens the output text can have.
          - `temperature`: What sampling temperature to use. Higher values mean the model will take more risks.
              Try 0.9 for more creative applications and 0 (argmax sampling) for ones with a well-defined answer.
          - `top_p`: An alternative to sampling with temperature, called nucleus sampling, where the model
              considers the results of the tokens with top_p probability mass. So, 0.1 means only the tokens
              comprising the top 10% probability mass are considered.
          - `n`: How many completions to generate for each prompt. For example, if the LLM gets 3 prompts and n is 2,
              it will generate two completions for each of the three prompts, ending up with 6 completions in total.
          - `stop`: One or more sequences after which the LLM should stop generating tokens.
          - `presence_penalty`: What penalty to apply if a token is already present at all. Bigger values mean
              the model will be less likely to repeat the same token in the text.
          - `frequency_penalty`: What penalty to apply if a token has already been generated in the text.
              Bigger values mean the model will be less likely to repeat the same token in the text.
          - `logit_bias`: Add a logit bias to specific tokens. The keys of the dictionary are tokens, and the
              values are the bias to add to that token.
      """
      self.model = model
      self.generation_kwargs = generation_kwargs or {}
      self.system_prompt = system_prompt
      self.streaming_callback = streaming_callback
  
      self.api_base_url = api_base_url
      self.organization = organization
      self.client = OpenAI(
          api_key=api_key,
          organization=organization,
          base_url=api_base_url,
      )
  
imports: import dataclasses
  import logging
  import warnings
  from typing import Optional, List, Callable, Dict, Any, Union
  from openai import OpenAI, Stream
  from openai.types.chat import ChatCompletionChunk, ChatCompletion
  from haystack import component, default_from_dict, default_to_dict
  from haystack.components.generators.utils import serialize_callback_handler, deserialize_callback_handler
  from haystack.dataclasses import StreamingChunk, ChatMessage
label_desc: Use image node_id 1 to create a new OpenAIGenerator object  with example: obj = OpenAIGenerator(api_key, model, streaming_callback, api_base_url, organization, system_prompt, generation_kwargs)