To have
Generative AI Controller understand the format of the inputs and outputs of your custom LLM, you must write transformer scripts. When you create a transformer record, code and comments are provided for
you to use as a guide while you edit. These scripts depend on the expected request and response objects that are interpreted by your model.
For example, the Azure OpenAI request structure looks like the following
script:
{"messages": [{"role":"user", "content":"Summarize the following text: <<content>>"}], "max_tokens": 800, "temperature": 0.7}
The request transformer script for that request
structure is the following script:
(function(inputs) {
/* write code here to construct the request body and any custom headers needed using the inputs object.
inputs structure: {
prompt_data: object,
request_data: object
} */
var requestData = inputs.request_data;
var promptData = inputs.prompt_data;
var prompt = promptData.prompt;
var model = promptData.model;
// construct body using the inputs
var body = {
messages: [{
"role": "user",
"content": prompt
}],
max_tokens: parseInt(promptData.max_tokens),
temperature: parseInt(promptData.temperature)
};
//construct headers using the inputs
var headers = {};
return {
body: body,
headers: headers
};
})(inputs);
The response structure from
Azure OpenAI looks like this script:
{
"choices": [{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "<<response>>",
"role": "assistant"
}
}],
"created": 1714994995,
"id": "chatcmpl-9LqpXeLVXDAi6kciPfLeIDjmALeea",
"model": "gpt-35-turbo-16k",
"object": "chat.completion",
"usage": {
"completion_tokens": 47,
"prompt_tokens": 70,
"total_tokens": 117
}
}
Because of that response structure, the response transformer script looks like this script:
(function(inputs) {
/* write code here to transform the llm response into an array of text responses, using the inputs object
inputs structure: {
prompt_data: object,
request_data: object,
response_body: string,
response_headers: string
} */
var requestData = inputs.request_data;
var promptData = inputs.prompt_data;
var responseBody = JSON.parse(inputs.response_body);
gs.info("response : " + inputs.response_body);
var responseTexts = [];
// write code here to populate the responseTexts array.
responseTexts.push(responseBody.choices[0].message.content);
return responseTexts;
})(inputs);