-
Notifications
You must be signed in to change notification settings - Fork 0
Add new providers to allow selecting LLMs #6
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,51 +1,57 @@ | ||
| require('dotenv').config() | ||
| const express = require('express') | ||
| const cors = require('cors') | ||
| const OpenAI = require('openai') | ||
| const path = require('path') | ||
| const providers = require('./src/llm/providers') | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: Path './src/llm/providers' doesn't match actual file location './src/llms/providers' |
||
|
|
||
| const app = express() | ||
| const port = process.env.PORT || 3000 | ||
|
|
||
| // Initialize OpenAI client | ||
| const openai = new OpenAI({ | ||
| apiKey: process.env.OPENAI_API_KEY | ||
| }) | ||
|
|
||
| // Middleware | ||
| app.use(cors()) | ||
| app.use(express.json()) | ||
| app.use(express.static('public')) | ||
|
|
||
| // Get available providers and models | ||
| app.get('/api/providers', (req, res) => { | ||
| const providerInfo = Object.entries(providers).map(([id, provider]) => ({ | ||
| id, | ||
| name: provider.name, | ||
| models: provider.models | ||
| })) | ||
| res.json(providerInfo) | ||
| }) | ||
|
|
||
| // Generate article endpoint | ||
| app.post('/api/generate-article', async (req, res) => { | ||
| try { | ||
| const { topic } = req.body | ||
| const { topic, provider: providerId, model } = req.body | ||
|
|
||
| if (!topic) { | ||
| return res.status(400).json({ error: 'Topic is required' }) | ||
| } | ||
|
|
||
| const completion = await openai.chat.completions.create({ | ||
| model: "gpt-3.5-turbo", | ||
| messages: [ | ||
| { | ||
| role: "system", | ||
| content: "You are a helpful assistant that generates short, informative articles." | ||
| }, | ||
| { | ||
| role: "user", | ||
| content: `Write a short, informative article about ${topic}. The article should be between 200-300 words.` | ||
| } | ||
| ], | ||
| temperature: 0.7, | ||
| max_tokens: 500 | ||
| }) | ||
|
|
||
| const article = completion.choices[0].message.content | ||
| res.json({ article }) | ||
| if (!providerId || !providers[providerId]) { | ||
| return res.status(400).json({ error: 'Invalid provider' }) | ||
| } | ||
|
|
||
| if (!model || !providers[providerId].models.includes(model)) { | ||
| return res.status(400).json({ error: 'Invalid model for provider' }) | ||
| } | ||
|
|
||
| const provider = providers[providerId] | ||
| const prompt = `Write a short, informative article about ${topic}. The article should be between 200-300 words.` | ||
|
|
||
| try { | ||
| const article = await provider.generate(provider.client, model, prompt) | ||
| res.json({ article, provider: provider.name, model }) | ||
| } catch (error) { | ||
| console.error(`Error with ${provider.name}:`, error) | ||
| res.status(500).json({ error: `Failed to generate article using ${provider.name}` }) | ||
| } | ||
| } catch (error) { | ||
| console.error('Error:', error) | ||
| res.status(500).json({ error: 'Failed to generate article' }) | ||
| res.status(500).json({ error: 'Failed to process request' }) | ||
| } | ||
| }) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,68 @@ | ||||||
| const OpenAI = require('openai') | ||||||
| const Anthropic = require('@anthropic-ai/sdk') | ||||||
| const cohere = require('cohere-ai') | ||||||
|
|
||||||
| const providers = { | ||||||
| openai: { | ||||||
| name: 'OpenAI', | ||||||
| client: new OpenAI({ | ||||||
| apiKey: process.env.OPENAI_API_KEY | ||||||
| }), | ||||||
| models: ['gpt-3.5-turbo', 'gpt-4'], | ||||||
| generate: async (client, model, prompt) => { | ||||||
| const completion = await client.chat.completions.create({ | ||||||
| model, | ||||||
| messages: [ | ||||||
| { | ||||||
| role: "system", | ||||||
| content: "You are a helpful assistant that generates short, informative articles." | ||||||
| }, | ||||||
| { | ||||||
| role: "user", | ||||||
| content: prompt | ||||||
| } | ||||||
| ], | ||||||
| temperature: 0.7, | ||||||
| max_tokens: 500 | ||||||
| }) | ||||||
|
Comment on lines
+13
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: Consider extracting common configuration (max_tokens, temperature) into shared constants |
||||||
| return completion.choices[0].message.content | ||||||
| } | ||||||
| }, | ||||||
| anthropic: { | ||||||
| name: 'Anthropic', | ||||||
| client: new Anthropic({ | ||||||
| apiKey: process.env.ANTHROPIC_API_KEY | ||||||
| }), | ||||||
| models: ['claude-2', 'claude-instant-1'], | ||||||
| generate: async (client, model, prompt) => { | ||||||
| const completion = await client.messages.create({ | ||||||
| model, | ||||||
| max_tokens: 500, | ||||||
| messages: [ | ||||||
| { | ||||||
| role: "user", | ||||||
| content: prompt | ||||||
| } | ||||||
| ] | ||||||
| }) | ||||||
|
Comment on lines
+38
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: Missing system prompt and temperature settings that other providers use. Could lead to inconsistent article generation. |
||||||
| return completion.content[0].text | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: Potential undefined error if completion.content is empty array. Add null check or default value.
Suggested change
|
||||||
| } | ||||||
| }, | ||||||
| cohere: { | ||||||
| name: 'Cohere', | ||||||
| client: cohere, | ||||||
| models: ['command', 'command-light'], | ||||||
| generate: async (client, model, prompt) => { | ||||||
| client.init(process.env.COHERE_API_KEY) | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: Initializing Cohere client on every request is inefficient. Move client.init() to where other clients are initialized. |
||||||
| const response = await client.generate({ | ||||||
| model, | ||||||
| prompt, | ||||||
| max_tokens: 500, | ||||||
| temperature: 0.7 | ||||||
| }) | ||||||
| return response.body.generations[0].text | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| module.exports = providers | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: langchain dependency is not used anywhere in the codebase. Remove if not needed.