Skip to content

Commit

Permalink
change OpenAI to Bedrock
Browse files Browse the repository at this point in the history
  • Loading branch information
wadabee committed Sep 25, 2023
1 parent da87c18 commit 26e5ef6
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 211 deletions.
138 changes: 38 additions & 100 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 77 additions & 0 deletions packages/cdk/lambda/bedrockApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import aws4Interceptor from 'aws4-axios';
import axios from 'axios';
import { UnrecordedMessage } from 'generative-ai-use-cases-jp';
import { IncomingMessage } from 'http';

const api = axios.create();
api.interceptors.request.use(
aws4Interceptor({
options: {
region: 'us-east-1',
service: 'bedrock',
},
})
);

const PARAMS = {
max_tokens_to_sample: 3000,
temperature: 0.6,
top_k: 300,
top_p: 0.8,
};

const generatePrompt = (messages: UnrecordedMessage[]): string => {
return (
messages
.map(
(m) =>
`${
m.role === 'system' || m.role === 'user' ? 'Human:' : 'Assistant:'
} ${m.content}`
)
.join('\n') + 'Assistant: '
);
};

const invoke = (messages: UnrecordedMessage[]) => {
return api.post<{
completion: string;
stop_reason: string;
}>(
'https://bedrock.us-east-1.amazonaws.com/model/anthropic.claude-v2/invoke',
{
...PARAMS,
prompt: generatePrompt(messages),
},
{
headers: {
Accept: '*/*',
'Content-Type': 'application/json',
'X-Amzn-Bedrock-Save': 1, // true | false
},
}
);
};

const invokeStream = (messages: UnrecordedMessage[]) => {
return api.post<IncomingMessage>(
'https://bedrock.us-east-1.amazonaws.com/model/anthropic.claude-v2/invoke-with-response-stream',
{
...PARAMS,
prompt: generatePrompt(messages),
},
{
headers: {
Accept: '*/*',
'Content-Type': 'application/json',
'X-Amzn-Bedrock-Save': 1, // true | false
},
responseType: 'stream',
}
);
};

export default {
invoke,
invokeStream,
};
20 changes: 4 additions & 16 deletions packages/cdk/lambda/predict.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,22 @@
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
import { Configuration, OpenAIApi } from 'openai';
import { PredictRequest } from 'generative-ai-use-cases-jp';
import { fetchOpenApiKey } from './secret';
import bedrockApi from './bedrockApi';

export const handler = async (
event: APIGatewayProxyEvent
): Promise<APIGatewayProxyResult> => {
try {
const req: PredictRequest = JSON.parse(event.body!);

// Secret 情報の取得
const apiKey = await fetchOpenApiKey();

// OpenAI API の初期化
const configuration = new Configuration({ apiKey });
const openai = new OpenAIApi(configuration);

// OpenAI API を使用してチャットの応答を取得
const chatCompletion = await openai.createChatCompletion({
model: 'gpt-3.5-turbo',
messages: req.messages,
});
const res = await bedrockApi.invoke(req.messages);

return {
statusCode: 200,
headers: {
'Content-Type': 'text/plain',
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
body: chatCompletion.data.choices[0].message!.content!,
body: JSON.stringify(res.data.completion),
};
} catch (error) {
console.log(error);
Expand Down
Loading

0 comments on commit 26e5ef6

Please sign in to comment.