> ## Documentation Index
> Fetch the complete documentation index at: https://docs.gp.scale.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Create Completion and Chat Completions

> Use this recipe to use the Scale GenAI Platform SDK to perform completion and chat completions

<AccordionGroup>
  <Accordion title="1. Instantiate Client">
    Follow the instructions in the [Quickstart Guide](/docs/getting-started) to setup the SGP Client

    ```py theme={null}
    from scale_gp import SGPClient

    client = SGPClient(api_key=api_key)
    ```
  </Accordion>

  <Accordion title="2. Perform a completion">
    Define model name and parameters to perform a completion.

    ```py theme={null}

    model_parameters = ModelParameters(max_tokens=200, temperature=0.5, top_k=1, top_p=1)

    completion = client.completions.create(
        model="gpt-4",
        prompt="Why is the sky blue?",
        account_id=account_id,
        model_parameters=model_parameters,
    )
    ```
  </Accordion>

  <Accordion title="3. Perform a chat completion">
    Call a chat model to perform a completion using a message and the parameters defined above. Chat completion allows for a series of messages to be sent to the model,
    serving as a conversation between the user and the model

    ```py theme={null}

    chat_model_parameters = ChatModelParameters(max_tokens=200, temperature=0.5, top_k=1, top_p=1)
    message = MessageEgpAPIBackendServerAPIModelsEgpModelsUserMessage(role="user", content="What is the capital of Canada?")

    chat_completion = client.chat_completions.create(
        model="gpt-4",
        messages=[
            message
        ],  # messages is a list of historical messages in the conversation, with roles usually alternating between user, assistant and system
        account_id=account_id,
        model_parameters=model_parameters,
        instructions="Answer the question like an elementary school teacher.",
    )

    ```
  </Accordion>
</AccordionGroup>

<RequestExample>
  ```python Python theme={null}
  import os

  from scale_gp import SGPClient
  from scale_gp.types.completion_create_params import ModelParameters
  from scale_gp.types.chat_completion_create_params import (
      ModelParameters as ChatModelParameters,
      MessageEgpAPIBackendServerAPIModelsEgpModelsUserMessage,
  )

  client = SGPClient(api_key=api_key)

  model_parameters = ModelParameters(max_tokens=200, temperature=0.5, top_k=1, top_p=1)

  completion = client.completions.create(
      model="gpt-4",
      prompt="Why is the sky blue?",
      account_id=account_id,
      model_parameters=model_parameters,
  )

  print(completion)

  chat_model_parameters = ChatModelParameters(max_tokens=200, temperature=0.5, top_k=1, top_p=1)
  message = MessageEgpAPIBackendServerAPIModelsEgpModelsUserMessage(role="user", content="What is the capital of Canada?")

  chat_completion = client.chat_completions.create(
      model="gpt-4",
      messages=[
          message
      ],  # messages is a list of historical messages in the conversation, with roles usually alternating between user, assistant and system
      account_id=account_id,
      model_parameters=model_parameters,
      instructions="Answer the question like an elementary school teacher.",
  )

  print(chat_completion)
  ```
</RequestExample>

<ResponseExample>
  ```python Completion theme={null}
  CompletionsResponse(
      completion=Completion(text="The sky appears blue because of the way Earth's atmosphere scatters sunlight in all directions and blue light is scattered more than other colors because it travels in smaller, shorter waves. This is known as Rayleigh scattering.", finish_reason='stop', response_metadata=None),
      token_usage=TokenUsage(total=56, completion=43, prompt=13)
  )
  ```

  ```python Chat Completion theme={null}
  ChatCompletionsResponse(
      chat_completion=ChatCompletion(message=ChatCompletionMessageEgpAPIBackendServerAPIModelsEgpModelsAssistantMessage(content="The capital of Canada is Ottawa. It's a beautiful city where the Prime Minister lives and where laws are made for the country.", role='assistant'), finish_reason='stop'),
      token_usage=TokenUsage(total=53, completion=26, prompt=27)
  )
  ```
</ResponseExample>
