how to integrate tiktoken library with Azure OpenAI

jack gerrad 0 Reputation points
2023-03-28T16:35:44.0766667+00:00

hello, is there any code sample I can use for tiktoken? It is supported inOpenAI https://github.com/openai/tiktoken

Please help with the plug-in implementation!

Azure OpenAI Service
Azure OpenAI Service
An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
2,935 questions
{count} votes

1 answer

Sort by: Most helpful
  1. YutongTie-MSFT 50,856 Reputation points
    2023-03-28T18:39:05.7233333+00:00

    Hello @jack gerrad

    Thanks for reaching out to us, there is an example in Azure doc which leverages the tiktoken you mentioned in your question. Please take a look and have a try. The following code sample shows a simple chat loop example with a technique for handling a 4096 token count using OpenAI's tiktoken library.

    import tiktoken
    import openai
    import os
    openai.api_type = "azure"
    openai.api_version = "2023-03-15-preview" 
    openai.api_base = os.getenv("OPENAI_API_BASE")  # Your Azure OpenAI resource's endpoint value .
    openai.api_key = os.getenv("OPENAI_API_KEY")
    
    system_message = {"role": "system", "content": "You are a helpful assistant."}
    max_response_tokens = 250
    token_limit= 4096
    conversation=[]
    conversation.append(system_message)
    
    def num_tokens_from_messages(messages, model="gpt-3.5-turbo-0301"):
        encoding = tiktoken.encoding_for_model(model)
        num_tokens = 0
        for message in messages:
            num_tokens += 4  # every message follows <im_start>{role/name}\n{content}<im_end>\n
            for key, value in message.items():
                num_tokens += len(encoding.encode(value))
                if key == "name":  # if there's a name, the role is omitted
                    num_tokens += -1  # role is always required and always 1 token
        num_tokens += 2  # every reply is primed with <im_start>assistant
        return num_tokens
    
    while(True):
        user_input = input("")     
        conversation.append({"role": "user", "content": user_input})
        conv_history_tokens = num_tokens_from_messages(conversation)
    
        while (conv_history_tokens+max_response_tokens >= token_limit):
            del conversation[1] 
            conv_history_tokens = num_tokens_from_messages(conversation)
            
        response = openai.ChatCompletion.create(
            engine="gpt-35-turbo", # The deployment name you chose when you deployed the ChatGPT or GPT-4 model.
            messages = conversation,
            temperature=.7,
            max_tokens=max_response_tokens,
        )
    
        conversation.append({"role": "assistant", "content": response['choices'][0]['message']['content']})
        print("\n" + response['choices'][0]['message']['content'] + "\n")
    
    

    The code requires tiktoken 0.3.0. If you have an older version run pip install tiktoken --upgrade.

    Reference - https://video2.skills-academy.com/en-us/azure/cognitive-services/openai/how-to/chatgpt?pivots=programming-language-chat-completions#managing-conversations

    I hope this helps!

    Regards,

    Yutong

    -Please kindly accept the answer and vote 'Yes' if you feel helpful to support the community, thanks a lot.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.