Skip to content

Commit

Permalink
Merge pull request #124 from edgio-docs/cohere-ai
Browse files Browse the repository at this point in the history
Cohere ai
  • Loading branch information
tristanlee85 authored Mar 14, 2024
2 parents c533ce9 + fd1ad45 commit 70a5ad1
Show file tree
Hide file tree
Showing 21 changed files with 14,108 additions and 1 deletion.
3 changes: 2 additions & 1 deletion examples/v7-ai/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ A collection of sample AI projects for deploying large language models with Edgi
- [LangChain](/langchain/README.md)
- [LlamaIndex](/llamaindex/README.md)
- [Mistral](/mistral/README.md)
- [Cohere](/cohere/README.md)

**Important**: [OpenAI](https://help.openai.com/en/articles/4936850-where-do-i-find-my-api-key) and [Mistral](https://docs.mistral.ai/#api-access) require a subscription to access their APIs.
**Important**: [OpenAI](https://help.openai.com/en/articles/4936850-where-do-i-find-my-api-key), [Mistral](https://docs.mistral.ai/#api-access) and [Cohere](https://docs.cohere.com/reference/about) require a subscription to access their APIs.
2 changes: 2 additions & 0 deletions examples/v7-ai/cohere/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Copy this file to .env and set your Cohere API token
COHERE_API_TOKEN="<YOUR_COHERE_API_TOKEN>"
39 changes: 39 additions & 0 deletions examples/v7-ai/cohere/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js
.yarn/install-state.gz

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts

# Edgio generated build directory
.edgio
57 changes: 57 additions & 0 deletions examples/v7-ai/cohere/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
This is a Next.js project bootstrapped with `create-next-app`, integrating with Cohere AI and powered by Edgio.

## Getting Started

### Install Packages

```bash
npm install
```

### Setup Environment Variables

Set up environment variables in a `.env` file by copying the `.env.example` file to `.env`.

```bash
cp .env.example .env
```

Replace `YOUR_COHERE_API_KEY` with your Cohere AI API key.

### Local Development Server

```bash
npm run edgio:dev
```

Open http://localhost:3000 with your browser to see the result.

You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.

This project uses `next/font` to automatically optimize and load Inter, a custom Google Font.

### Cohere AI Integration

This application demonstrates a simple use case for text summarization using Cohere AI's powerful natural language processing capabilities. Users can input a text of at least 250 characters, and the app will display a summarized version of the text.

To get started with the text summarization feature:

1. Navigate to the home page at http://localhost:3000.
2. Enter your text into the textarea provided.
3. Click the `Summarize` button to see the summarized text.

Please ensure you have set up your Cohere AI API key correctly in your environment variables or within the application to use the summarization feature.

To learn more about Edgio, take a look at the following resources:

- [Edgio Documentation](https://docs.edg.io) - learn about Edgio features

## Deploy on Edgio

Deploy this project on Edgio with the following command:

```bash
npm run edgio:deploy
```

Check out our [Cohere AI documentation](https://docs.edg.io/guides/v7/ai_integrations/cohere) for more details.
105 changes: 105 additions & 0 deletions examples/v7-ai/cohere/edgio.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// This file was automatically added by edgio init.
// You should commit this file to source control.
// Learn more about this file at https://docs.edg.io/guides/edgio_config
require('dotenv').config();

console.log('token:', process.env.COHERE_API_TOKEN);
module.exports = {
connector: '@edgio/next',

// The name of the site in Edgio to which this app should be deployed.
// name: 'my-site-name',

// The name of the organization in Edgio to which this app should be deployed.
// organization: 'my-organization-name',

// Overrides the default path to the routes file. The path should be relative to the root of your app.
// routes: 'routes.js',

// When set to true or omitted entirely, Edgio includes the deployment number in the cache key,
// effectively purging the cache each time you deploy.
// purgeCacheOnDeploy: false,

next: {
// Output sourcemaps so that stack traces have original source filenames and line numbers when tailing
// the logs in the Edgio developer console.
// This config options replaces the edgioSourceMaps option in next.config.js.
// @default true
// generateSourceMaps: true
//
// Disables the Edgio image optimizer and allows to use the Next's built in image optimizer.
// This config options replaces the disableImageOptimizer option in edgio.config.js root.
// @default false
// disableImageOptimizer: false
//
// Disables the build of the service worker.
// @default false
// disableServiceWorker: false
//
// Forces the @edgio/next connector to use the server build.
// This config option replaces the NEXT_FORCE_SERVER_BUILD env variable.
// @default false
// forceServerBuild: false
//
// Optimizes the server build by bundling all server assets and decreasing the overall startup time.
// This option has no effect on apps with serverless build.
// This option is set to false for Next 13.x apps.
// @default true
// optimizeServerBuild: true
//
// Set this option to false to remove the default rule that proxies all requests to Next.js in serverless.
// This is useful if you want to proxy all unmatched pages to different origin.
// @default true
// proxyToServerlessByDefault: true
//
// Set this option to true to honor Next's internal redirects that either add or remove a trailing slash
// depending on the value of the `trailingSlash` config. When set to false, these internal redirects are not honored,
// so sites that fallback to serving from an origin do not add or remove the trailing slash for origin URLs.
// @default true
// enforceTrailingSlash: true
},

// If you need to proxy some URLs to an origin instead of your Next.js app, you can configure the origins here:
// origins: [
// {
// // The name of the backend origin
// name: "origin",
//
// // When provided, the following value will be sent as the host header when connecting to the origin.
// // If omitted, the host header from the browser will be forwarded to the origin.
// override_host_header: "test-origin.edgio.net",
//
// // The list of backend hosts
// hosts: [
// {
// // The domain name or IP address of the origin server
// location: "test-origin.edgio.net"
// }
// ]
// }
// ],

// Options for hosting serverless functions on Edgio
// serverless: {
// // Set to true to include all packages listed in the dependencies property of package.json when deploying to Edgio.
// // This option generally isn't needed as Edgio automatically includes all modules imported by your code in the bundle that
// // is uploaded during deployment
// includeNodeModules: true,
//
// // Include additional paths that are dynamically loaded by your app at runtime here when building the serverless bundle.
// include: ['views/**/*'],
// },

// The maximum number of URLs that will be concurrently prerendered during deployment when static prerendering is enabled.
// Defaults to 200, which is the maximum allowed value.
// prerenderConcurrency: 200,

// A list of glob patterns identifying which prerenderConcurrency source files should be uploaded when running edgio deploy --includeSources.
// This option is primarily used to share source code with Edgio support personnel for the purpose of debugging. If omitted,
// edgio deploy --includeSources will result in all files which are not gitignored being uploaded to Edgio.
//
// sources : [
// '**/*', // include all files
// '!(**/secrets/**/*)', // except everything in the secrets directory
// ],
};
29 changes: 29 additions & 0 deletions examples/v7-ai/cohere/edgio/cohere.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { CohereClient } from 'cohere-ai';

const { COHERE_API_TOKEN } = process.env;

export default function cohereHandler({ compute }) {
compute(async (req, res) => {
const { text } = JSON.parse(req.body);
if (!text) {
res.statusCode = 400;
res.statusMessage = 'Text is required for summarization.';
return;
}
const cohere = new CohereClient({
token: COHERE_API_TOKEN,
});

try {
const summary = await cohere.summarize({
text: text,
});

res.body = JSON.stringify({ summary });
} catch (error) {
console.error('Error summarizing text:', error);
res.statusCode = 500;
res.statusMessage = 'Failed to summarize text.';
}
});
}
7 changes: 7 additions & 0 deletions examples/v7-ai/cohere/jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"paths": {
"@/*": ["./src/*"]
}
}
}
8 changes: 8 additions & 0 deletions examples/v7-ai/cohere/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// This file was automatically added by edgio init.
// You should commit this file to source control.
const { withEdgio } = require('@edgio/next/config')

module.exports = (phase, config) =>
withEdgio({
// Write your Next config here
})
4 changes: 4 additions & 0 deletions examples/v7-ai/cohere/next.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @type {import('next').NextConfig} */
const nextConfig = {};

export default nextConfig;
Loading

0 comments on commit 70a5ad1

Please sign in to comment.