Skip to content

Text-to-Text

Chat Completion

Introduction

OpenAI's Chat Completion is a powerful feature that allows developers to build applications that can carry out dynamic and interactive conversations. It uses the same underlying model as the text completion feature, but it's designed to handle multi-turn conversations.

In a chat-based model, you send a list of messages instead of a single string as a prompt. Each message in the list has two properties: 'role' and 'content'. The 'role' can be 'system', 'user', or 'assistant', and 'content' contains the text of the message from the role.

The system role is used to set the behavior of the assistant, the user role providzes instructions to the assistant, and the assistant role contains the assistant's responses. The model generates a response for the assistant based on the conversation history.

This feature opens up new possibilities for creating interactive and dynamic applications, such as virtual assistants, chatbots, and more.

Best Practices

The chat completion feature works similarly to the text completion feature, but it takes a list of messages as input instead of a single prompt. Here's an example of how you can use OpenAI chat completion:

curl -X POST 'https://genaiapi.cloudsway.net/v1/ai/sVUARttwSjilLOZY/chat/completions' -H 'Authorization: Bearer YOU_API_KEY' -H 'Content-Type: application/json' --data '{
    "model": "gpt-4",
    "messages": [
        {
            "role": "user",
            "content": "hello"
        }
    ]
}'

In this example, we are sending a POST request to the chat completion endpoint with the following parameters:

  • model: The name of the model to use for completion (e.g., gpt-4-turbo). This parameter is optional as we have included it in the upstream of the Cloudsway endpoint.
  • messages: A list of messages containing the conversation history. Each message has a 'role' and 'content' property.

For other query parameters, you can refer to the OpenAI API documentation for more details.

The chat completion response will be a JSON object containing the generated response from the assistant. For example:

{
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "logprobs": null,
      "messages": [
        {
          "role": "assistant",
          "content": "I'm doing well, thank you for asking. How can I help you today?"
        }
      ]
    }
  ],
  "created": 1632345678,
  "model": "gpt-4-turbo",
  "object": null,
  "system_fingerprint": null,
  "usage": {
    "completion_tokens": 100,
    "prompt_tokens": 20,
    "total_tokens": 120
  }
}

You can use the generated response to continue the conversation or display it in your application. The chat completion feature allows you to create engaging and interactive chat experiences for your users.

Seamless Integration

Here's an example of how you can integrate Cloudsway with the Python OpenAI library:

from openai import OpenAI
from openai import OpenAI

client=OpenAI(
    base_url="https://genaiapi.cloudsway.net/v1/ai/zUcfeMfrpNqyEhTN",
    api_key='YOUR_API_KEY'
)
message_text = [{"role":"user","content":"what's 1+1? Answer in one word."}]

completion = client.chat.completions.create(
  model="", # model = "deployment_name"
  messages = message_text,
  temperature=0.7,
  max_tokens=800,
  top_p=0.95,
)

print(completion)

By following these steps, you can effectively use OpenAI chat completion to generate responses based on the conversation history. Experiment with different messages and parameters to create engaging chat experiences for your users.

Stream Completion

OpenAI's Stream Completion is a powerful feature that allows developers to build applications that can carry out dynamic and interactive conversations in real-time. It uses the same underlying model as the text completion feature, but it's designed to handle continuous streams of messages. The only difference is that the messages are sent in a continuous stream instead of a single batch. So you can add the parameter stream to the request. For example:

curl -X POST 'https://genaiapi.cloudsway.net/v1/ai/sVUARttwSjilLOZY/chat/completions' -H 'Authorization: Bearer YOU_API_KEY' -H 'Content-Type: application/json' --data '{
    "model": "gpt-4",
    "messages": [
        {
            "role": "user",
            "content": "hello"
        }
    ],
    "stream": True
}'

The parameter in python is:

completion = client.chat.completions.create(
  model="", # model = "deployment_name"
  stream = True,
  messages = message_text,
  temperature=0.7,
  max_tokens=800,
  top_p=0.95,
)

The stream completion response will be a JSON object containing the generated response from the assistant. For example:

{"id":"chatcmpl-123","object":"chat.completion.chunk","created":1694268190,"model":"gpt-3.5-turbo-0125", "system_fingerprint": "fp_44709d6fcb", "choices":[{"index":0,"delta":{"role":"assistant","content":""},"logprobs":null,"finish_reason":null}]}

You can use the generated response to continue the conversation or display it in your application. The stream completion feature allows you to create engaging and interactive chat experiences for your users in real-time.

Multimodal

Introduction

A multimodal model is an artificial intelligence model capable of processing and comprehending various types of data, such as text, images, audio, and more. Unlike unimodal models, multimodal models can integrate information from different modalities, thereby offering a more comprehensive and precise understanding and generative ability. For instance, MaaS 4o is a multimodal model that can handle both text and image inputs, producing corresponding outputs.

Best Practices

Here is an exemplary code snippet demonstrating the best practices for utilizing MaaS 4o to process text and image inputs, specifically by analyzing the ingredients and calories in a food image.

{
    "messages": [
        {
            "role": "system", 
            "content": "As an expert in food analysis, I will analyze the provided image and provide the ingredients and calories in JSON format."
        }, 
        {
            "role": "user", 
            "content": [
                {
                    "type": "image_url", 
                    "image_url": {
                        "url": "https://img0.baidu.com/it/u=4078772867,1411546811&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500"
                    }
                }
            ]
        }
    ], 
    "stream": false,
     "stream_options": {
    "include_usage": true
  }
}

In this example, we send a POST request to the endpoint with the following parameters:

  • role: Represents the role of the message, which can be "system", "user", or "assistant".

  • content: Contains the content of the message, with different formats depending on the role.

  • stream: A boolean value indicating whether to use streaming. In this example, the value is false, indicating that streaming is not used.

The response would be a JSON object containing the generated text. For example:

{
    "created": 1721984825,
    "usage": {
        "completion_tokens": 313,
        "prompt_tokens": 248,
        "total_tokens": 561
    },
    "model": null,
    "id": "chatcmpl-9pBCjZZoDwaTKYjiRz5HomWHnSEZC",
    "choices": [
        {
            "finish_reason": "stop",
            "index": 0,
            "message": {
                "role": "assistant",
                "function_call": null,
                "tool_calls": null,
                "content": "Here is an image of a burger. Based on the image, it contains the following ingredients:\n\n- Sesame bun (top and bottom)\n- Lettuce\n- Tomato slices\n- Bacon strips\n- Grilled beef patty\n- Possibly some sauces (such as mayonnaise or salad dressing)\n\nThe approximate calorie breakdown for each ingredient is as follows:\n\n1. **Sesame bun** (top and bottom): approximately 150 calories\n2. **Lettuce**: approximately 5 calories\n3. **Tomato slices**: approximately 10 calories\n4. **Bacon strips** (2-3 pieces): approximately 120 calories\n5. **Grilled beef patty**: approximately 250 calories\n6. **Sauces**: approximately 50 calories\n\nThe total estimated calories are as follows:\n\n```json\n{\n  \"components\": {\n    \"sesame_bun\": 150,\n    \"lettuce\": 5,\n    \"tomato_slices\": 10,\n    \"bacon_strips\": 120,\n    \"grilled_beef_patty\": 250,\n    \"sauces\": 50\n  },\n  \"total_calories\": 585\n}\n```\n\nPlease note that this is only an estimate, and the actual calorie count may vary depending on the specific quantity and type of ingredients."
            },
            "logprobs": null
        }
    ],
    "system_fingerprint": null,
    "object": "chat.completion"
}

Completion

Introduction

OpenAI completion is a powerful language model developed by OpenAI. It is designed to generate human-like text based on given prompts. By providing a partial sentence or paragraph as input, the completion model can generate coherent and contextually relevant text to complete the prompt.

The completion model uses advanced deep learning techniques, such as transformers, to understand the context and semantics of the input text. It has been trained on a vast amount of diverse data from the internet, making it capable of generating high-quality text across various domains and topics.

Using OpenAI completion, developers and researchers can automate the process of generating text for a wide range of applications, including content generation, chatbots, language translation, and much more. With its ability to generate natural and fluent text, OpenAI completion has become a valuable tool in the field of natural language processing.

Best Practices

The completion model works by taking a prompt as input and generating a continuation of the text based on that prompt. Here's how you can use OpenAI completion:

curl -X POST 'https://genaiapi.cloudsway.net/v1/ai/zUcfeMfrpNqyEhTN/completions' -H 'Authorization: Bearer YOUR_API_KEY' -H 'Content-Type: application/json' --data '{
    "model": "gpt-4",
    "prompt":"Once upon a time, there was a",
    "temperature":0.7,
    "max_tokens":100
}'

In this example, we are sending a POST request to the completion endpoint with the following parameters:

  • model: The name of the model to use for completion (e.g., gpt-4-turbo) which is is optional for we have include this parameter in the upstream of cloudsway endpoint.
  • prompt: The partial text that serves as input to the model (e.g., "Once upon a time, there was a").
  • temperature(Optional): A parameter that controls the randomness of the generated text. A higher temperature value results in more diverse and creative outputs, default value is 1.
  • max_tokens: The maximum number of tokens to generate in the completion.

For other query parameters, you can refer to the OpenAI API documentation for more details.

The completion response will be a JSON object containing the generated text. For example:

{
  "choices": [
    {
      "finish_reason": "length",
      "index": 0,
      "logprobs": null,
      "text": " beautiful princess named Cinderella. She lived in a small cottage in the woods with her wicked stepmother and two stepsisters. One day, she received an invitation to the royal ball, where she met the prince and fell in love. The clock struck midnight, and she had to leave in a hurry, leaving behind her glass slipper. The prince searched the kingdom to find her, and when he did, they lived happily ever after."
    }
  ],
  "created": 1632345678,
  "model": "gpt-4-turbo",
  "object": null,
  "system_fingerprint": null,
  "usage": {
    "completion_tokens": 100,
    "prompt_tokens": 10,
    "total_tokens": 110
  }
}

You can use the generated text for various purposes, such as displaying it on a website, saving it to a file, or using it as input for further processing.

Seamless Integration

Here's an example of how you can integrate Cloudsway with the Python OpenAI library:

from openai import OpenAI
client=OpenAI(
    base_url="https://genaiapi.cloudsway.net/v1/ai/zUcfeMfrpNqyEhTN",
    api_key='YOUR_API_KEY'
)
message_text = [{"role":"user","content":"what's 1+1? Answer in one word."}]

completion = client.completions.create(
  model="", # model = "deployment_name"
  prompt="the result of 1 plus 1 is?",
  temperature=0.7,
  max_tokens=800,
  top_p=0.95,
)
print(completion)

By following these steps, you can effectively use OpenAI completion to generate text based on your prompts. Experiment with different prompts and parameters to get the desired output.