0.0.8 • Published 3 days ago

promptwiz v0.0.8

Weekly downloads
-
License
Unlicense
Repository
-
Last release
3 days ago

PromptWiz Typescript Client

Context

The API provides a way to run prompts created on the PromptWiz web application programmatically, as well as retrieve prompt data from PromptWiz.

In running prompts, the API will populate the prompt variables with your inputs, query the model then return the results. The results for API requests will be stored in PromptWiz for the purpose of analysis.

Pre-requisites

  1. Create a prompt
  2. Create a version of the prompt (and save it)
  3. Activate the prompt version - the version's status should be ‘Active’
  4. Set the API Key for the model service (e.g. OpenAI) used by the prompt in the API keys page, or alternatively pass the model service API key on every query in an API request using the modelApiKey field
  5. Read more in the API reference page

Set up

Navigate to the folder of your consuming project and run:

npm i promptwiz

Client initialisation

The PromptWizClient can be initialised with the following required and optional constructor arguments:

  1. apiKey - This key can be found in the API Keys page
  2. url optional - url of the promptwiz server. Defaults to https://promptwiz.co.uk
  3. apiVersion optional - api version of the promptwiz server. Defaults to the latest version.

Here's an example of how the client would usually be initialised:

import {PromptWizClient} from 'promptwiz'

const promptwiz = new PromptWizClient('d91a37db-a16b-4108-8485-f88496b30886')

Making an API request

Read the API reference for more information on requests and responses.

Evaluate

The evaluate endpoint allows running prompts created on the PromptWiz web application. The client provides utility functions and object models to make evaluation requests as well as parse the response.

  1. evaluate given an EvaluateRequest object it makes an API call to PromptWiz and returns a generator that yeilds EvaluateResponse objects, i.e. this functions returns the direct result from the PromptWiz API and can be parsed by you as required.
  2. evaluateAndParseResponse - given an EvaluateRequest object it makes an API call to PromptWiz and returns a generator that yeilds ParsedEvaluateResponse objects, which contain merely the output(s) of the model(s) and, if any, request errors. Additionally, in the case of streaming evaluation results, the ParsedEvaluateResponse contains the full model output(s) up to an including the current chunk.
  3. parseEvaluateResponse - this is a convenience function to convert EvaluateResponse objects to ParsedEvaluateResponse objects.

Example

Request 1

The code snippet below demonstrates an example of constructing a request with 3 queries. The first two queries target a prompt witha ID 4, and the last targets prompt with ID 2. Additionally, the first query requests two results, instead of the default of one. Lastly, the request sets the stream parameter to true, which will result in a series of responses being streamed by the PromptWiz API, each containing incremental results.

import {EvaluateRequest} from 'promptwiz'

const request: EvaluateRequest = {
    querySet: [
        {promptId: 4, variables: {"PERSON": "Trump", "X": "What is 2+1?"}, resultsSize: 2},
        {promptId: 4, variables: {"PERSON": "Newton", "X": "What is 7+1?"}},
        {promptId: 2, variables: {"UNIT": "meters", "X": "How far is it to the moon?"}}
    ],
    acceptPartial: true,
    stream: true,
}
evaluate

The code snippet below demonstrates an example of using the standard evaluate utility function to make Request 1.

const generator = promptwiz.evaluate(request);
(async () => {
    for await (const response of generator) {
        console.log(JSON.stringify(response))
    }
})();

Two incremental outputs of the above code snippet were as below. Note the incremental outputs, instead of a single complete output, is due to Request 1 setting the stream parameter in the evalution request to true.

{"resultSet":[{"linkId":0,"promptId":4,"promptVersionNumber":1,"results":[{"choice":1,"result":" tremendous"}],"time":2.236644744873047,"inputSpend":24,"resultsSpend":[{"choice":0,"spend":109},{"choice":1,"spend":109}],"outputSpend":218}]}
{"resultSet":[{"linkId":1,"promptId":4,"promptVersionNumber":1,"results":[{"choice":0,"result":""}],"time":2.4335439205169678,"inputSpend":24,"resultsSpend":[{"choice":0,"spend":62}],"outputSpend":62}]}
evaluateAndParseResponse

The code snippet below demonstrates an example of using the evaluateAndParseResponse utility function to make Request 1.

const generator = promptwiz.evaluateAndParseResponse(request);
(async () => {
    for await (const response of generator) {
        console.log(response)
    }
})();

An incremental output of the above code snippet was as below. Note the incremental output, instead of a single complete output, is due to Request 1 setting the stream parameter in the evalution request to true.

{
  errors: [],
  results: {
    '0': {
      '0': "Let me tell you something folks, nobody knows numbers better than me. And I'll tell you, 2 plus 1, it's a tremendous number. I've always been."
      '1': "Let me tell you folks, believe me, nobody knows numbers like I do. I've been dealing with numbers my whole life, millions and billions"
    },
    '1': { '0': 'The sum of 7 and 1 is' },
    '2': {
      '0': 'The distance to the moo'
    }
  }
}

The last output of the above code snippet was as below. Note the output contains represents the complete model output(s) instead of the final chunk, as the evaluateAndParseResponse utility function continuously merges the streamed chunks recieved from the PromptWiz API.

{
  errors: [],
  results: {
    '0': {
      '0': "Let me tell you something folks, nobody knows numbers better than me. And I'll tell you, 2 plus 1, it's a tremendous number. I've always been very, very good with numbers, believe me. It's simple folks, it's three, okay? It's three. I know numbers like the back of my hand, and let me tell you, three is a fantastic number. No one does math better than me, nobody. So remember, it's three, and it's a tremendous number. Thank you, thank you very much.",
      '1': "Let me tell you folks, believe me, nobody knows numbers like I do. I've been dealing with numbers my whole life, millions and billions, all the big numbers. Now, you look at this question, 2 plus 1, it's a simple one, really simple. Some people might say it's too easy, but I like easy, folks. I like easy things. So, you take 2 and you add 1, and you know what you get? You get 3. It's tremendous, really tremendous. Nobody does math like I do. It's incredible. 3 is a fantastic number. So, there you have it, folks, 2 plus 1 equals 3. End of story."
    },
    '1': { '0': 'The sum of 7 and 1 is indisputably 8.' },
    '2': {
      '0': 'The distance to the moon is approximately 384,400,000 meters.'
    }
  }
}

Prompt

The prompt endpoint allows retrieving prompts created on the PromptWiz web application. The client provides utility functions and object models to make prompt requests.

Example

Request 2

The code snippet below demonstrates an example of constructing a request for retrieving a prompt with ID 4.

import {PromptRequest} from 'promptwiz'

const request: PromptRequest = {promptId: 4}
prompt

The code snippet below demonstrates an example of using the prompt utility function to make Request 2.

const response = promptwiz.prompt(request);
(async () => {
    const prompt = await response;
    console.log(JSON.stringify(prompt));
})();
0.0.8

3 days ago

0.0.7

6 months ago

0.0.6

6 months ago

0.0.5

6 months ago

0.0.4

6 months ago

0.0.3

6 months ago

0.0.2

7 months ago

0.0.1

7 months ago