Shimeji API

Chatbots

ChatBot

class shimeji.ChatBot(name: str, model_provider: ModelProvider, **kwargs)

Bases: object

conditional_response(text, push_chain=True)

Respond to the given text or conversation chain if the chatbot should respond.

Parameters
  • text (str) – The response text.

  • push_chain (bool) – Whether to push the response to the conversation chain.

Returns

Whether the chatbot should respond.

Return type

bool

respond(text, push_chain)

Respond to the given text or conversation chain.

Parameters
  • text (str) – The response text.

  • push_chain (bool) – Whether to push the response to the conversation chain.

async respond_async(text, push_chain)

Respond to the given text or conversation chain asynchronously.

Parameters
  • text (str) – The response text.

  • push_chain (bool) – Whether to push the response to the conversation chain.

should_respond(text, push_chain)

Determine if the chatbot should respond to the given text or conversation chain.

Parameters
  • text (str) – The response text.

  • push_chain (bool) – Whether to push the response to the conversation chain.

Returns

Whether the chatbot should respond.

Return type

bool

async should_respond_async(text, push_chain)

Determine if the chatbot should respond to the given text or conversation chain asynchronously.

Parameters
  • text (str) – The response text.

  • push_chain (bool) – Whether to push the response to the conversation chain.

Returns

Whether the chatbot should respond.

Return type

bool

Model Providers

ModelProvider

class shimeji.ModelProvider(endpoint_url: str, **kwargs)

Bases: object

Abstract class for model providers that provide access to generative AI models.

auth()

Authenticate with the ModelProvider’s endpoint.

Raises

NotImplementedError – If the authentication method is not implemented.

generate(args)

Generate a response from the ModelProvider’s endpoint.

Parameters

args (dict) – The arguments to pass to the endpoint.

Raises

NotImplementedError – If the generate method is not implemented.

async generate_async(args)

Generate a response from the ModelProvider’s endpoint asynchronously.

Parameters

args (dict) – The arguments to pass to the endpoint.

Raises

NotImplementedError – If the generate method is not implemented.

async hidden_async(model, text, layer)

Fetch a layer’s hidden states from text.

Parameters
  • model (str) – The model to extract hidden states from.

  • text (str) – The text to use.

  • layer (int) – The layer to fetch the hidden states from.

async image_label_async(model, url, labels)

Classify an image with labels (CLIP).

Parameters
  • model (str) – The model to use for classification.

  • url (str) – The image URL to use.

  • labels (list) – The labels to use.

response(context)

Generate a response from the ModelProvider’s endpoint.

Parameters

context (str) – The context to use.

Raises

NotImplementedError – If the response method is not implemented.

response_async(context)

Generate a response from the ModelProvider’s endpoint asynchronously.

Parameters

context (str) – The context to use.

Raises

NotImplementedError – If the response method is not implemented.

should_respond(context, name)

Determine if the ModelProvider predicts that the name should respond to the given context.

Parameters
  • context (str) – The context to use.

  • name (str) – The name to check.

Raises

NotImplementedError – If the should_respond method is not implemented.

should_respond_async(context, name)

Determine if the ModelProvider predicts that the name should respond to the given context asynchronously.

Parameters
  • context (str) – The context to use.

  • name (str) – The name to check.

Raises

NotImplementedError – If the should_respond method is not implemented.

Sukima_ModelProvider

class shimeji.Sukima_ModelProvider(endpoint_url: str, **kwargs)

Bases: ModelProvider

auth()

Authenticate with the Sukima endpoint.

Raises

Exception – If the authentication fails.

conv_listobj_to_listdict(list_objects)

Convert the elements of a list to a dictionary for JSON compatability.

Parameters

list_objects (list) – The list.

Returns

A list which has it’s elements converted to dictionaries.

Return type

list

generate(args: ModelGenRequest)

Generate a response from the Sukima endpoint.

Parameters

args (dict) – The arguments to pass to the endpoint.

Returns

The response from the endpoint.

Return type

str

Raises

Exception – If the request fails.

async generate_async(args: ModelGenRequest)

Generate a response from the Sukima endpoint asynchronously.

Parameters

args (dict) – The arguments to pass to the endpoint.

Returns

The response from the endpoint.

Return type

str

Raises

Exception – If the request fails.

async hidden_async(model, text, layer)

Fetch a layer’s hidden states from text.

Parameters
  • model (str) – The model to extract hidden states from.

  • text (str) – The text to use.

  • layer (int) – The layer to fetch the hidden states from.

async image_label_async(model, url, labels)

Classify an image with labels (CLIP).

Parameters
  • model (str) – The model to use for classification.

  • url (str) – The image URL to use.

  • labels (list) – The labels to use.

response(context)

Generate a response from the Sukima endpoint.

Parameters

context (str) – The context to use.

Returns

The response from the endpoint.

Return type

str

async response_async(context)

Generate a response from the Sukima endpoint asynchronously.

Parameters

context (str) – The context to use.

Returns

The response from the endpoint.

Return type

str

should_respond(context, name)

Determine if the Sukima endpoint predicts that the name should respond to the given context.

Parameters
  • context (str) – The context to use.

  • name (str) – The name to check.

Returns

Whether or not the name should respond to the given context.

Return type

bool

async should_respond_async(context, name)

Determine if the Sukima endpoint predicts that the name should respond to the given context asynchronously.

Parameters
  • context (str) – The context to use.

  • name (str) – The name to check.

Returns

Whether or not the name should respond to the given context.

Return type

bool

Preprocessors

Preprocessor

class shimeji.Preprocessor

Bases: object

Abstract class for preprocessors.

ContextPreprocessor

class shimeji.ContextPreprocessor(token_budget=1024)

Bases: Preprocessor

A Preprocessor that builds a context from a list of ContextEntry objects.

add_entry(entry)

Add a ContextEntry to the ContextPreprocessor.

Parameters

entry (ContextEntry) – The ContextEntry to add.

cascade_lookup(entry, nest=0)

Search for other entries that are activated by a given entry.

Parameters
  • entry (ContextEntry) – The entry to recursively search for other entries in.

  • nest (int, optional) – The maximum amount of recursion to perform, defaults to 0.

Returns

A list of other entries that are activated by the given entry.

Return type

list

context(budget=1024)

Build the context from the ContextPreprocessor’s entries.

Parameters

budget (int, optional) – The maximum number of tokens that can be used in the context, defaults to 1024.

Returns

The built context.

Return type

str

del_entry(entry)

Delete a ContextEntry from the ContextPreprocessor.

Parameters

entry (ContextEntry) – The ContextEntry to delete.

key_lookup(entry_a, entry_b)

Check if a ContextEntry’s key is found in an entry’s text.

Parameters
  • entry_a (ContextEntry) – The ContextEntry to check.

  • entry_b (ContextEntry) – Another ContextEntry to check.

Returns

Whether the key is found in the text.

Return type

bool

ordinal_pos(position, length)

Postprocessors

Postprocessor

class shimeji.Postprocessor

Bases: object

Abstract class for postprocessors.

NewlinePrunerPostprocessor

class shimeji.NewlinePrunerPostprocessor

Bases: object

Postprocessor that removes newlines.