|
| 1 | +import logging |
| 2 | +from pprint import pformat |
| 3 | +from typing import List, Optional |
| 4 | + |
| 5 | +from steamship import Block, File, MimeTypes, PluginInstance, Steamship, SteamshipError, Tag |
| 6 | +from steamship.data import GenerationTag, TagKind |
| 7 | +from steamship.plugin.capabilities import ( |
| 8 | + Capability, |
| 9 | + CapabilityPluginRequest, |
| 10 | + CapabilityPluginResponse, |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +class SteamshipLLM: |
| 15 | + """A class wrapping LLM plugins.""" |
| 16 | + |
| 17 | + def __init__(self, plugin_instance: PluginInstance): |
| 18 | + self.client = plugin_instance.client |
| 19 | + self.plugin_instance = plugin_instance |
| 20 | + |
| 21 | + class Impls: |
| 22 | + @staticmethod |
| 23 | + def gpt( |
| 24 | + client: Steamship, |
| 25 | + plugin_version: Optional[str] = None, |
| 26 | + model: str = "gpt-4", |
| 27 | + temperature: float = 0.4, |
| 28 | + max_tokens: int = 256, |
| 29 | + **kwargs, |
| 30 | + ): |
| 31 | + gpt = client.use_plugin( |
| 32 | + "gpt-4", |
| 33 | + version=plugin_version, |
| 34 | + config={ |
| 35 | + "model": model, |
| 36 | + "temperature": temperature, |
| 37 | + "max_tokens": max_tokens, |
| 38 | + **kwargs, |
| 39 | + }, |
| 40 | + ) |
| 41 | + return SteamshipLLM(gpt) |
| 42 | + |
| 43 | + def generate( |
| 44 | + self, |
| 45 | + messages: List[Block], |
| 46 | + capabilities: List[Capability] = None, |
| 47 | + assert_capabilities: bool = True, |
| 48 | + **kwargs, |
| 49 | + ) -> List[Block]: |
| 50 | + """ |
| 51 | + Call the LLM plugin's generate method. Generate requests for plugin capabilities based on input parameters. |
| 52 | +
|
| 53 | + :param messages: Messages to be passed to the LLM to construct the prompt. |
| 54 | + :param capabilities: Capabilities that will be asked of the LLM. See the docstring for |
| 55 | + steamship.plugins.capabilities. |
| 56 | +
|
| 57 | + If default_capabilities was provided at construction, those capabilities will be requested unless overridden |
| 58 | + by this parameter. |
| 59 | + :param assert_capabilities: If True (default), raise a SteamshipError if the LLM plugin did not respond with a |
| 60 | + block that asserts what level capabilities were fulfilled at. |
| 61 | + :param kwargs: Options that can be passed to the LLM model as other parameters. |
| 62 | + :return: a List of Blocks that are returned from the plugin. |
| 63 | + """ |
| 64 | + file_ids = {b.file_id for b in messages} |
| 65 | + block_ids = None |
| 66 | + temp_file = None |
| 67 | + if len(file_ids) != 1 and next(iter(file_ids)) is not None: |
| 68 | + file_id = next(iter(file_ids)) |
| 69 | + block_ids = [b.id for b in messages] |
| 70 | + else: |
| 71 | + temp_file = File.create( |
| 72 | + client=self.client, |
| 73 | + blocks=messages, |
| 74 | + tags=[Tag(kind=TagKind.GENERATION, name=GenerationTag.PROMPT_COMPLETION)], |
| 75 | + ) |
| 76 | + file_id = temp_file.id |
| 77 | + |
| 78 | + request_block = CapabilityPluginRequest(requested_capabilities=capabilities).create_block( |
| 79 | + client=self.client, file_id=file_id |
| 80 | + ) |
| 81 | + if block_ids: |
| 82 | + block_ids.append(request_block.id) |
| 83 | + |
| 84 | + try: |
| 85 | + generation_task = self.plugin_instance.generate( |
| 86 | + input_file_id=file_id, input_file_block_index_list=block_ids, options=kwargs |
| 87 | + ) |
| 88 | + generation_task.wait() |
| 89 | + |
| 90 | + for block in generation_task.output.blocks: |
| 91 | + if block.mime_type == MimeTypes.STEAMSHIP_PLUGIN_CAPABILITIES_RESPONSE: |
| 92 | + if logging.DEBUG >= logging.root.getEffectiveLevel(): |
| 93 | + response = CapabilityPluginResponse.from_block(block) |
| 94 | + logging.debug( |
| 95 | + f"Plugin capability fulfillment:\n\n{pformat(response.json())}" |
| 96 | + ) |
| 97 | + break |
| 98 | + else: |
| 99 | + if assert_capabilities: |
| 100 | + version_string = f"{self.plugin_instance.plugin_handle}, v.{self.plugin_instance.plugin_version_handle}" |
| 101 | + raise SteamshipError( |
| 102 | + f"Asserting capabilities are used, but capability response was not returned by plugin ({version_string})" |
| 103 | + ) |
| 104 | + finally: |
| 105 | + if temp_file: |
| 106 | + temp_file.delete() |
| 107 | + |
| 108 | + return generation_task.output.blocks |
0 commit comments