Skip to content
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

Added palm-2 api and have written test #285

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions JS/edgechains/lib/src/lib/endpoints/GeminiEndoints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import axios from 'axios';

export class GeminiAPI {
apiKey: string;
baseUrl: string;

constructor(
apiKey: string,
baseUrl: string = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-pro'
) {
this.apiKey = apiKey;
this.baseUrl = baseUrl;
}

async generateContent(text: string): Promise<any> {
const url = `${this.baseUrl}:generateContent?key=${this.apiKey}`;
const requestBody = {
contents: [
{
parts: [
{
text: text,
},
],
},
],
};

try {
const response = await axios.post(url, requestBody, {
headers: {
'Content-Type': 'application/json',
},
});
return response.data.candidates[0].content.parts[0].text;
} catch (error: any) {
throw new Error(`Error in generating content: ${error.message}`);
}
}
}
13 changes: 13 additions & 0 deletions JS/edgechains/lib/src/lib/endpoints/prompts/promptsGemini.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
local generateContentPrompt(text) = {
contents: [
{
parts: [
{
text: text,
},
],
},
],
};

generateContentPrompt("Hiii");// your prompts here
31 changes: 31 additions & 0 deletions JS/edgechains/lib/src/lib/endpoints/prompts/reader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Jsonnet } from '@hanazuki/node-jsonnet';
import * as path from 'path';
import { GeminiAPI } from '../GeminiEndoints';

const jsonnet = new Jsonnet();

export async function loadPrompt(promptPath: string): Promise<string>{
try {
const prompt = await jsonnet.evaluateFile(promptPath);
return prompt;
} catch (error: any) {
throw new Error(`Error loading prompt: ${error.message}`);
}
}

const gemini = new GeminiAPI(
'API_KEY',
'https://generativelanguage.googleapis.com/v1beta/models/gemini-pro'
);

const promptPath = path.join(__dirname, 'prompts.jsonnet');

(async () => {
try {
const prompt = await loadPrompt(promptPath);
const generatedContent = await gemini.generateContent(prompt);
console.log('Generated Content:', generatedContent);
} catch (error) {
console.error('Error:', error);
}
})();
83 changes: 81 additions & 2 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"prettier": "^3.1.1"
},
"devDependencies": {
"@types/express": "^4.17.21"
"@types/express": "^4.17.21",
"axios": "^1.6.3"
}
}
Loading