Skip to content

Commit

Permalink
Gemini Pro 1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
msveshnikov committed Mar 27, 2024
1 parent a829171 commit c8b91f6
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 26 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ALLCHAT

Node.js backend and a React MUI frontend for an application that interacts with the Gemini Pro model, with history, image generating/recognition, PDF/Word/Excel upload and markdown support. Written fully by _Claude 3 Sonnet_.
Node.js backend and a React MUI frontend for an application that interacts with the Gemini Pro 1.5 model, with history, image generating/recognition, PDF/Word/Excel upload and markdown support. Written fully by _Claude 3 Sonnet_.

![image](https://github.com/msveshnikov/allchat/assets/8682996/42b2e4f2-b91b-4712-8ef2-630ebb8919e9)

Expand Down Expand Up @@ -29,6 +29,7 @@ https://allchat.online/
You have to get those APIs and set environment variables (or put to .env file):

- GOOGLE_KEY - key of Google Cloud Project with Vertex AI API enabled (for Gemini Pro)
- GEMINI_KEY - key for 1.5 preview access from https://aistudio.google.com/app/apikey?utm_source=newsletter&utm_medium=email&utm_campaign=1.5p-api-launch_march&utm_content=
- google.json - https://console.cloud.google.com/apis/credentials/key
- AWS_SECRET_KEY
- AWS_ACCESS_KEY - (for Titan iamge generation) - https://eu-central-1.console.aws.amazon.com/console/home?region=eu-central-1
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ services:
environment:
- NODE_ENV=production
- GOOGLE_KEY=${GOOGLE_KEY}
- GEMINI_KEY=${GEMINI_KEY}
- AWS_ACCESS_KEY=${AWS_ACCESS_KEY}
- AWS_SECRET_KEY=${AWS_SECRET_KEY}
- CLAUDE_KEY=${CLAUDE_KEY}
Expand Down
4 changes: 2 additions & 2 deletions public/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"short_name": "AllChat",
"name": "Chat with Gemini Pro",
"name": "Chat with Gemini Pro 1.5",
"icons": [
{
"src": "favicon.ico",
Expand Down Expand Up @@ -28,5 +28,5 @@
"dir": "auto",
"lang": "en",
"orientation": "any",
"description": "Chat with Gemini Pro"
"description": "Chat with Gemini Pro 1.5"
}
62 changes: 44 additions & 18 deletions server/gemini.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,13 @@ import dotenv from "dotenv";
dotenv.config({ override: true });

const vertex_ai = new VertexAI({ project: process.env.GOOGLE_KEY, location: "us-central1" });
const model = "gemini-pro";
// const model = "gemini-1.5-pro-latest";
// const model = "gemini-pro";
const model = "gemini-1.5-pro-latest";
const model_vision = "gemini-1.0-pro-vision-001";

process.env["GOOGLE_APPLICATION_CREDENTIALS"] = "./allchat.json";

export const getTextGemini = async (prompt, temperature) => {
const generativeModel = vertex_ai.preview.getGenerativeModel({
model: model,
generation_config: {
max_output_tokens: 4096,
temperature: temperature || 0.8,
top_p: 0.8,
},
});

const chat = generativeModel.startChat({});
const result = await chat.sendMessage([{ text: prompt }]);
return result?.response?.candidates?.[0].content?.parts?.[0]?.text;
};

export const getTextVision = async (prompt, imageBase64, temperature) => {
export const getTextVision = async (prompt, imageBase64) => {
const generativeModel = vertex_ai.preview.getGenerativeModel({
model: model_vision,
generation_config: {
Expand All @@ -48,3 +33,44 @@ export const getTextVision = async (prompt, imageBase64, temperature) => {
const aggregatedResponse = await response.response;
return aggregatedResponse.candidates[0].content.parts[0].text;
};

const URL = `https://generativelanguage.googleapis.com/v1beta/models/${model}:generateContent?key=${process.env.GEMINI_KEY}`;

export const getTextGemini = async (prompt, temperature) => {
const headers = {
"Content-Type": "application/json",
};

const data = {
contents: [
{
role: "user",
parts: [
{
text: prompt,
},
],
},
],
generation_config: {
maxOutputTokens: 4096,
temperature: temperature || 0.5,
topP: 0.8,
},
};

const response = await fetch(URL, {
method: "POST",
headers,
body: JSON.stringify(data),
});

if (!response.ok) {
console.error(response.statusText);
throw new Error("Gemini Error:" + response.statusText);
}

const result = await response.json();
console.log(result);
return result?.candidates?.[0]?.content?.parts?.[0]?.text;
};
2 changes: 1 addition & 1 deletion src/components/ModelSwitch.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const ModelSwitch = ({ onModelChange, model }) => {
<Box sx={{ display: "flex" }}>
<FormControlLabel
control={<Switch checked={model === "claude"} onChange={handleModelChange} color="primary" />}
label={model === "claude" ? "Claude Haiku" : "Gemini Pro"}
label={model === "claude" ? "Claude Haiku" : "Gemini Pro 1.5"}
labelPlacement="start"
/>
</Box>
Expand Down
6 changes: 3 additions & 3 deletions src/components/__tests__/ModelSwitch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import '@testing-library/jest-dom'
describe("ModelSwitch Component", () => {
it("renders with default props", () => {
const { getByLabelText } = render(<ModelSwitch model="gemini" />);
const switchElement = getByLabelText("Gemini Pro");
const switchElement = getByLabelText("Gemini Pro 1.5");

expect(switchElement).toBeInTheDocument();
});

it("changes model on switch toggle", () => {
const onModelChangeMock = jest.fn();
const { getByLabelText } = render(<ModelSwitch model="gemini" onModelChange={onModelChangeMock} />);
const switchElement = getByLabelText("Gemini Pro");
const switchElement = getByLabelText("Gemini Pro 1.5");

fireEvent.click(switchElement);

Expand All @@ -23,7 +23,7 @@ describe("ModelSwitch Component", () => {

it("displays correct label based on model prop", () => {
const { getByLabelText, rerender } = render(<ModelSwitch model="gemini" />);
let switchElement = getByLabelText("Gemini Pro");
let switchElement = getByLabelText("Gemini Pro 1.5");

expect(switchElement).toBeInTheDocument();

Expand Down
2 changes: 1 addition & 1 deletion src/components/__tests__/SideDrawer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe("SideDrawer Component", () => {
onClearAll={mockOnClearAll}
/>
);
fireEvent.click(getByLabelText("Gemini Pro"));
fireEvent.click(getByLabelText("Gemini Pro 1.5"));
expect(mockOnModelChange).toHaveBeenCalledWith("claude");
});

Expand Down

0 comments on commit c8b91f6

Please sign in to comment.