I am using Bedrock LLAMA-2 70b API to generate content based on query asked - similar to GPT, and receiving back response from it but within a single paragraph. This is messy and difficult to read sometimes since it doesn't format nor completes it. How can I apply formatting such as recognizing bullet points, new line characters, code snippets shared and more? Im using Nodejs for the backend with Reactjs for frontend. Are there any existing libraries out there that can do this?
Demonstration:
Generate Fibonacci code using Javascript
Here is an example of how you can generate Fibonacci numbers using JavaScript:
function fibonacci(n) { if (n <= 1) { return n; } return fibonacci(n-1) + fibonacci(n-2); } console.log(fibonacci(5)); // Output: 5 console.log(fibonacci(8)); // Output: 21 console.log(fibonacci(13)); // Output: 89This function uses a recursive approach to calculate the nth Fibonacci number. It starts by checking if n is less than or equal to 1, in which case the result is simply n. Otherwise, it calculates the (n-1)th and (n-2)th Fibonacci numbers using the same function, and then adds them together to get the nth Fibonacci number. You can also use a loop to calculate the Fibonacci numbers, here is an example:function fibonacci(n) { var a = 0, b = 1, result = 0; for (var i = 0; i < n; i++) { result = a + b; a = b; b = result; } return result; } console.log(fibonacci(5)); // Output: 5 console.log(fibonacci(8)); // Output: 21 console.log(fibonacci(13)); // Output: 89This function uses a loop to calculate the Fibonacci numbers, it starts by initializing two variables a and b to 0 and 1, and then uses a loop to iterate n times, in each iteration it adds a and b together and assigns the result to a, then it assigns b to the result of the previous iteration, and at the end it returns the result. You can also use a memoized function to calculate the Fibonacci numbers, here is an example: ``` function fibonacci(n) { var memo = { 0: 0, 1: 1 }; for (var i =
As noticed above, the code generated is within a paragraph and difficult to read.
This is the API:
app.post('/api/chat', authenticateToken, async (req, res) => {
const { message } = req.body; //user's query
const fetch = await import('node-fetch');
const url = process.env.API; //connects to lambda function
//BedRock requestBody
const requestBody = {
role: "user",
content: message
};
try {
const response = await fetch.default(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(requestBody)
});
if (!response.ok) {
throw new Error(`Network response was not ok: ${response.status} - ${response.statusText}`);
}
const responseJson = await response.json();
const generation = responseJson.generation;
res.json({
status: 200,
message: generation
});
} catch (error) {
res.json({
status: 400,
message: error
});
}
})
This is AWS Lambda function implementing the Bedrock API model via boto3 and is called when the API is activated:
import boto3
import json
def lambda_handler(event, context):
runtime= boto3.client(
service_name = 'bedrock-runtime',
region_name = 'us-east-2'
)
contentData = json.loads(event['body'])
content = contentData['content']
inputData = {
"modelId": "meta.llama2-70b-chat-v1",
"contentType": "application/json",
"accept": "application/json",
"body": {
"prompt": content,
"max_gen_len": 512,
"temperature": 0.8,
"top_p": 0.2
}
}
try:
response = runtime.invoke_model(body=json.dumps(inputData['body']),
contentType=inputData['contentType'],
modelId = inputData['modelId'],
accept = inputData['accept']
)
response_content = response.get('body').read()
final_response = json.loads(response_content)
return {
"statusCode": 200,
'headers': {"Access-Control-Allow-Origin": "*"},
"body": json.dumps(final_response)
}
except Exception as e:
return {
'statusCode': 500,
'body': str(e)
}
Any help is appreciated, thanks.