-
Notifications
You must be signed in to change notification settings - Fork 2.3k
feat(firebase-ai): create ai
package, vertexai
wraps around it
#8555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
russellwheatley
wants to merge
71
commits into
main
Choose a base branch
from
firebase-ai
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
71 commits
Select commit
Hold shift + click to select a range
0857b47
chore: initial cp over to ai directory
russellwheatley b4b9407
refactor: move to AI implementation, including AIModel base class
russellwheatley f4280ae
chore: update types for FirebaseApp to match JS SDK
russellwheatley c725a09
chore: update CHANGELOG & package.json
russellwheatley d6f4264
format
russellwheatley 00f9990
request-helpers
russellwheatley 492d9cb
request.ts
russellwheatley 2b84bf5
response-helpers.ts
russellwheatley 7a22950
schema-builder.ts
russellwheatley 6402aa4
stream-reader.ts
russellwheatley 0636826
chat-session-helpers.ts
russellwheatley d3ae723
chat-session.ts
russellwheatley 01297c8
count-tokens.ts
russellwheatley c64c85c
generate-content.ts
russellwheatley 97c713e
models index
russellwheatley 8da0826
enums.ts
russellwheatley e6f54e6
error.ts
russellwheatley c12e76e
chore: add googleai to exports
russellwheatley f8d6ef6
request.ts types
russellwheatley 3b98d54
types/responses.ts
russellwheatley e8cb7d3
types/schema.ts
russellwheatley 9ef6afe
test: backend
russellwheatley 81bcbab
add license header to test file
russellwheatley 073db4b
test: googleai-mapper
russellwheatley a98ff8d
test: rename scripts and location to "ai"
russellwheatley 152ed66
chore: firebase_ai yarn.lock
russellwheatley fd3cdf9
test(ai): update unit test for firebase ai
russellwheatley 662183a
test: update convert mocks in line with latest mock response repo
russellwheatley 15b4b99
test(ai): update script name to run
russellwheatley be07881
test(ai): getMockResponse()
russellwheatley 0f8b8b2
test(ai): mock-response update for new mocks
russellwheatley b57874d
test(ai): count tokens unit tests
russellwheatley dd8782a
test: generate-content mock response
russellwheatley d8d5a52
test(ai): update unit tests to use updated mocks
russellwheatley b886279
test: fix unit tests
russellwheatley 70df14a
test: fixed another unit test suite
russellwheatley 5cfdb3c
test: add TS no-check to generated mocks
russellwheatley d3d06ab
refactor(vertexai): initial setup to use ai package
russellwheatley 7b3b299
refactor: remove vertexai code and wrap around firebase ai package
russellwheatley 223ed4e
chore: update script name
russellwheatley 9072011
chore: firebaseerror from utils
russellwheatley 9eeb3bb
chore: import vertex constant
russellwheatley fe24a9e
chore(vertexai): rm testing from vertex package
russellwheatley 185a1d0
chore: update logger to ai
russellwheatley 867a058
chore(vertexai): rm obsolete files
russellwheatley eae2fe8
chore(vertexai): revert back to prev. tsconfig
russellwheatley 57bbaa8
chore(vertexai): symlink ai lib/ folder
russellwheatley dfd5991
chore(vertexai): wrap around ai
russellwheatley 518c8f8
chore(vertexai): remove logger
russellwheatley cde4fae
test(vertexai): rm e2e tests
russellwheatley d53e7c7
refactor: make ai dependency on vertexai
russellwheatley 3b082d0
test: fix test
russellwheatley e8781b2
fix: do not use RN URL to construct url
russellwheatley 11cf91b
chore(ai): write example app for ai package
russellwheatley b1c952b
fix(ai): allow auth and app check to be passed in
russellwheatley 520f848
chore: clean up of exports
russellwheatley 6da0570
chore: rm mock script from vertex
russellwheatley 2b51ccd
chore: update linter ignore list
russellwheatley ca4053b
docs(ai): write usage docs
russellwheatley e47cb97
format
russellwheatley 1077ae5
rm note
russellwheatley 097d8cf
scrub TODO
russellwheatley 2ef02c0
Merge branch 'main' into firebase-ai
russellwheatley 2dd4249
chore: fix yarn.lock
russellwheatley ee1d556
test: ensure all types are exported
russellwheatley 61c77a6
chore: pr feedback
russellwheatley 191918c
chore: rm code comment
russellwheatley cb91a26
Apply suggestions from code review
russellwheatley 23ba283
Merge branch 'main' into firebase-ai
russellwheatley 96469ce
yarn.lock
russellwheatley aeadc6c
chore: finish string
russellwheatley File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Change Log | ||
|
||
All notable changes to this project will be documented in this file. | ||
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. | ||
|
||
## Feature | ||
|
||
Initial release of the Firebase AI Logic SDK (`FirebaseAI`). This SDK *replaces* the previous Vertex AI in Firebase SDK (`FirebaseVertexAI`) to accommodate the evolving set of supported features and services. | ||
The new Firebase AI Logic SDK provides **preview** support for the Gemini Developer API, including its free tier offering. | ||
Using the Firebase AI Logic SDK with the Vertex AI Gemini API is still generally available (GA). | ||
|
||
To start using the new SDK, import the `@react-native-firebase/ai` package and use the modular method `getAI()` to initialize. See details in the [migration guide](https://firebase.google.com/docs/vertex-ai/migrate-to-latest-sdk). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
Apache-2.0 License | ||
------------------ | ||
|
||
Copyright (c) 2016-present Invertase Limited <[email protected]> & Contributors | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this library except in compliance with the License. | ||
|
||
You may obtain a copy of the Apache-2.0 License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
|
||
|
||
Creative Commons Attribution 3.0 License | ||
---------------------------------------- | ||
|
||
Copyright (c) 2016-present Invertase Limited <[email protected]> & Contributors | ||
|
||
Documentation and other instructional materials provided for this project | ||
(including on a separate documentation repository or it's documentation website) are | ||
licensed under the Creative Commons Attribution 3.0 License. Code samples/blocks | ||
contained therein are licensed under the Apache License, Version 2.0 (the "License"), as above. | ||
|
||
You may obtain a copy of the Creative Commons Attribution 3.0 License at | ||
|
||
https://creativecommons.org/licenses/by/3.0/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<p align="center"> | ||
<a href="https://rnfirebase.io"> | ||
<img width="160px" src="https://i.imgur.com/JIyBtKW.png"><br/> | ||
</a> | ||
<h2 align="center">React Native Firebase - AI Logic</h2> | ||
</p> | ||
|
||
<p align="center"> | ||
<a href="https://api.rnfirebase.io/coverage/ai/detail"><img src="https://api.rnfirebase.io/coverage/ai/badge?style=flat-square" alt="Coverage"></a> | ||
<a href="https://www.npmjs.com/package/@react-native-firebase/ai"><img src="https://img.shields.io/npm/dm/@react-native-firebase/ai.svg?style=flat-square" alt="NPM downloads"></a> | ||
<a href="https://www.npmjs.com/package/@react-native-firebase/ai"><img src="https://img.shields.io/npm/v/@react-native-firebase/ai.svg?style=flat-square" alt="NPM version"></a> | ||
<a href="/LICENSE"><img src="https://img.shields.io/npm/l/react-native-firebase.svg?style=flat-square" alt="License"></a> | ||
<a href="https://lerna.js.org/"><img src="https://img.shields.io/badge/maintained%20with-lerna-cc00ff.svg?style=flat-square" alt="Maintained with Lerna"></a> | ||
</p> | ||
|
||
<p align="center"> | ||
<a href="https://invertase.link/discord"><img src="https://img.shields.io/discord/295953187817521152.svg?style=flat-square&colorA=7289da&label=Chat%20on%20Discord" alt="Chat on Discord"></a> | ||
<a href="https://twitter.com/rnfirebase"><img src="https://img.shields.io/twitter/follow/rnfirebase.svg?style=flat-square&colorA=1da1f2&colorB=&label=Follow%20on%20Twitter" alt="Follow on Twitter"></a> | ||
<a href="https://www.facebook.com/groups/rnfirebase"><img src="https://img.shields.io/badge/Follow%20on%20Facebook-4172B8?logo=facebook&style=flat-square&logoColor=fff" alt="Follow on Facebook"></a> | ||
</p> | ||
|
||
--- | ||
|
||
Firebase AI Logic gives you access to the latest generative AI models from Google. | ||
|
||
If you need to call the Gemini API directly from your mobile or web app — rather than server-side — you can use the Firebase AI Logic client SDKs. These client SDKs are built specifically for use with mobile and web apps, offering security options against unauthorized clients as well as integrations with other Firebase services. | ||
|
||
[> Learn More](https://firebase.google.com/docs/ai-logic/) | ||
|
||
## Installation | ||
|
||
Requires `@react-native-firebase/app` to be installed. | ||
|
||
```bash | ||
yarn add @react-native-firebase/ai | ||
``` | ||
|
||
## Documentation | ||
|
||
- [Quick Start](https://rnfirebase.io/ai/usage) | ||
|
||
## License | ||
|
||
- See [LICENSE](/LICENSE) | ||
|
||
--- | ||
|
||
<p> | ||
<img align="left" width="75px" src="https://static.invertase.io/assets/invertase-logo-small.png"> | ||
<p align="left"> | ||
Built and maintained with 💛 by <a href="https://invertase.io">Invertase</a>. | ||
</p> | ||
</p> | ||
|
||
--- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/** | ||
* @license | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { describe, expect, it } from '@jest/globals'; | ||
import { type ReactNativeFirebase } from '../../app/lib'; | ||
|
||
import { ModelParams, AIErrorCode } from '../lib/types'; | ||
import { AIError } from '../lib/errors'; | ||
import { getGenerativeModel } from '../lib/index'; | ||
|
||
import { AI } from '../lib/public-types'; | ||
import { GenerativeModel } from '../lib/models/generative-model'; | ||
|
||
import { AI_TYPE } from '../lib/constants'; | ||
import { VertexAIBackend } from '../lib/backend'; | ||
|
||
const fakeAI: AI = { | ||
app: { | ||
name: 'DEFAULT', | ||
options: { | ||
apiKey: 'key', | ||
appId: 'appId', | ||
projectId: 'my-project', | ||
}, | ||
} as ReactNativeFirebase.FirebaseApp, | ||
backend: new VertexAIBackend('us-central1'), | ||
location: 'us-central1', | ||
}; | ||
|
||
describe('getGenerativeModel()', () => { | ||
it('should throw an error if no model is provided', () => { | ||
try { | ||
getGenerativeModel(fakeAI, {} as ModelParams); | ||
} catch (e) { | ||
expect((e as AIError).code).toContain(AIErrorCode.NO_MODEL); | ||
expect((e as AIError).message).toContain( | ||
`AI: Must provide a model name. Example: ` + | ||
`getGenerativeModel({ model: 'my-model-name' }) (${AI_TYPE}/${AIErrorCode.NO_MODEL})`, | ||
); | ||
} | ||
}); | ||
|
||
it('getGenerativeModel throws if no apiKey is provided', () => { | ||
const fakeVertexNoApiKey = { | ||
...fakeAI, | ||
app: { options: { projectId: 'my-project', appId: 'my-appid' } }, | ||
} as AI; | ||
try { | ||
getGenerativeModel(fakeVertexNoApiKey, { model: 'my-model' }); | ||
} catch (e) { | ||
expect((e as AIError).code).toContain(AIErrorCode.NO_API_KEY); | ||
expect((e as AIError).message).toBe( | ||
`AI: The "apiKey" field is empty in the local ` + | ||
`Firebase config. Firebase AI requires this field to` + | ||
` contain a valid API key. (${AI_TYPE}/${AIErrorCode.NO_API_KEY})`, | ||
); | ||
} | ||
}); | ||
|
||
it('should throw an error if no projectId is provided', () => { | ||
const fakeVertexNoProject = { | ||
...fakeAI, | ||
app: { options: { apiKey: 'my-key' } }, | ||
} as AI; | ||
try { | ||
getGenerativeModel(fakeVertexNoProject, { model: 'my-model' }); | ||
} catch (e) { | ||
expect((e as AIError).code).toContain(AIErrorCode.NO_PROJECT_ID); | ||
expect((e as AIError).message).toBe( | ||
`AI: The "projectId" field is empty in the local` + | ||
` Firebase config. Firebase AI requires this field ` + | ||
`to contain a valid project ID. (${AI_TYPE}/${AIErrorCode.NO_PROJECT_ID})`, | ||
); | ||
} | ||
}); | ||
|
||
it('should throw an error if no appId is provided', () => { | ||
const fakeVertexNoProject = { | ||
...fakeAI, | ||
app: { options: { apiKey: 'my-key', projectId: 'my-projectid' } }, | ||
} as AI; | ||
try { | ||
getGenerativeModel(fakeVertexNoProject, { model: 'my-model' }); | ||
} catch (e) { | ||
expect((e as AIError).code).toContain(AIErrorCode.NO_APP_ID); | ||
expect((e as AIError).message).toBe( | ||
`AI: The "appId" field is empty in the local` + | ||
` Firebase config. Firebase AI requires this field ` + | ||
`to contain a valid app ID. (${AI_TYPE}/${AIErrorCode.NO_APP_ID})`, | ||
); | ||
} | ||
}); | ||
|
||
it('should return an instance of GenerativeModel', () => { | ||
const genModel = getGenerativeModel(fakeAI, { model: 'my-model' }); | ||
expect(genModel).toBeInstanceOf(GenerativeModel); | ||
expect(genModel.model).toBe('publishers/google/models/my-model'); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* @license | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { describe, it, expect } from '@jest/globals'; | ||
import { GoogleAIBackend, VertexAIBackend } from '../lib/backend'; | ||
import { BackendType } from '../lib/public-types'; | ||
import { DEFAULT_LOCATION } from '../lib/constants'; | ||
|
||
describe('Backend', () => { | ||
describe('GoogleAIBackend', () => { | ||
it('should set backendType to GOOGLE_AI', () => { | ||
const backend = new GoogleAIBackend(); | ||
expect(backend.backendType).toBe(BackendType.GOOGLE_AI); | ||
}); | ||
}); | ||
|
||
describe('VertexAIBackend', () => { | ||
it('should set backendType to VERTEX_AI', () => { | ||
const backend = new VertexAIBackend(); | ||
expect(backend.backendType).toBe(BackendType.VERTEX_AI); | ||
expect(backend.location).toBe(DEFAULT_LOCATION); | ||
}); | ||
|
||
it('should set a custom location', () => { | ||
const backend = new VertexAIBackend('test-location'); | ||
expect(backend.backendType).toBe(BackendType.VERTEX_AI); | ||
expect(backend.location).toBe('test-location'); | ||
}); | ||
|
||
it('should use a default location if location is empty string', () => { | ||
const backend = new VertexAIBackend(''); | ||
expect(backend.backendType).toBe(BackendType.VERTEX_AI); | ||
expect(backend.location).toBe(DEFAULT_LOCATION); | ||
}); | ||
|
||
it('uses default location if location is null', () => { | ||
const backend = new VertexAIBackend(null as any); | ||
expect(backend.backendType).toBe(BackendType.VERTEX_AI); | ||
expect(backend.location).toBe(DEFAULT_LOCATION); | ||
}); | ||
}); | ||
}); |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.