OpenAI

Intro

Use with

from pyalm import OpenAI
llm = OpenAI("gpt4", openai_key=KEY)

Alternatively the key can be ignored and set via the env var OPENAI_API_KEY. You can set the used model to a non supported one via llm.model = NAME Cost can be accessed via llm.finish_meta after a call or with OpenAI.pricing and OpenAI.pricing_meta to get the rates.

Native OpenAI function calls are currently not supported

Documentation

class pyalm.models.openai.OpenAI(model_path_or_name, openai_key=None, verbose=0, azure_endpoint=None, api_version='2023-05-15', **kwargs)
available_models = ['gpt-3.5-turbo', 'gpt-3.5-turbo-16k', 'gpt-4']

All models that are known to work

build_prompt(conv_history=None, system_msg=None)

Build prompt in format native to library

Parameters:

preserve_flow – Block suffix for purely text based models

Returns:

prompt obj

create_native_completion(text, max_tokens=256, stop=None, keep_dict=False, token_prob_delta=None, token_prob_abs=None, log_probs=None, **kwargs)

Library native completion retriever. Different for each library. No processing of output is done

Parameters:
  • text – Prompt or prompt obj

  • max_tokens – maximum tokens generated in completion

  • stop – Additional stop sequences

  • keep_dict – If library or API returns something else than raw tokens, whether to return native format

  • token_prob_delta – dict, relative added number for token logits

  • token_prob_abs – dict, Absolute logits for tokens

  • log_probs – int, when not None return the top X log probs and their tokens

  • kwargs – kwargs

Returns:

completion

create_native_generator(text, keep_dict=False, token_prob_delta=None, token_prob_abs=None, **kwargs)

Library native generator for tokens. Different for each library. No processing of output is done

Parameters:
  • text – Prompt or prompt obj

  • keep_dict – If library or API returns something else than raw tokens, whether to return native format

  • token_prob_delta – dict, Absolute logits for tokens

  • token_prob_abs – dict, relative added number for token logits

  • kwargs – kwargs

Returns:

generator

get_available_models()
get_n_tokens(text)

How many tokens are in a string

Parameters:

text – tokenizable text

Returns:

amount

pricing = {'gpt-3.5-turbo': {'input': 0.0015, 'output': 0.002}, 'gpt-3.5-turbo-16k': {'input': 0.003, 'output': 0.004}, 'gpt-4': {'input': 0.03, 'output': 0.06}}

Pricing per input and output tokens in a prompt. Streaming costs the same

pricing_meta = {'currency': '$', 'token_unit': 1000}
tokenize(text)

Text to token as vector representation

Parameters:

text

Returns:

List of tokens as ints

tokenize_as_str(text)

Text to token as vector representation but each token is converted to string

Parameters:

text

Returns:

List of tokens as strings