Skip to content
Open
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
35 changes: 35 additions & 0 deletions examples/with-anthropic/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# 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
34 changes: 34 additions & 0 deletions examples/with-anthropic/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).

## Getting Started

First, run the development server:

```bash
npm run dev
# or
yarn dev
# or
pnpm dev
```

Open [http://localhost:3000](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`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font.

## Learn More

To learn more about Next.js, take a look at the following resources:

- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.

You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!

## Deploy on Vercel

The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.

Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
36 changes: 36 additions & 0 deletions examples/with-anthropic/app/api/chat/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// ./app/api/chat/route.ts
import { AI_PROMPT, Client, HUMAN_PROMPT } from '@anthropic-ai/sdk'
import { AnthropicStream, StreamingTextResponse } from 'ai-connector'

// Create an OpenAI API client (that's edge friendly!)
const client = new Client(process.env.ANTHROPIC_API_KEY!)
// IMPORTANT! Set the runtime to edge
export const runtime = 'edge'

export async function POST(req: Request) {
// Extract the `prompt` from the body of the request
const { messages } = await req.json()

// Ask OpenAI for a streaming chat completion given the prompt
const fullResponse = await client.completeStream(
{
prompt: `${HUMAN_PROMPT} How many toes do dogs have?${AI_PROMPT}`,
stop_sequences: [HUMAN_PROMPT],
max_tokens_to_sample: 200,
model: 'claude-v1'
},
{
onOpen: response => {
console.log('Opened stream, HTTP status code', response.status)
},
onUpdate: completion => {
console.log(completion.completion)
}
}
)

// Convert the response into a friendly text-stream
// const stream = AnthropicStream(fullResponse)
// Respond with the stream
return new Response(fullResponse.completion)
}
Binary file added examples/with-anthropic/app/favicon.ico
Binary file not shown.
3 changes: 3 additions & 0 deletions examples/with-anthropic/app/globals.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@tailwind base;
@tailwind components;
@tailwind utilities;
21 changes: 21 additions & 0 deletions examples/with-anthropic/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import './globals.css'
import { Inter } from 'next/font/google'

const inter = Inter({ subsets: ['latin'] })

export const metadata = {
title: 'Create Next App',
description: 'Generated by create next app'
}

export default function RootLayout({
children
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body className={inter.className}>{children}</body>
</html>
)
}
29 changes: 29 additions & 0 deletions examples/with-anthropic/app/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use client'

import { useChat } from 'ai-connector'

export default function Chat() {
const { messages, input, handleInputChange, handleSubmit } = useChat()

return (
<div className="mx-auto w-full max-w-md py-24 flex flex-col stretch">
{messages.length > 0
? messages.map(m => (
<div key={m.id}>
{m.role === 'user' ? 'User: ' : 'AI: '}
{m.content}
</div>
))
: null}

<form onSubmit={handleSubmit}>
<input
className="fixed w-full max-w-md bottom-0 border border-gray-300 rounded mb-8 shadow-xl p-2"
value={input}
placeholder="Say something..."
onChange={handleInputChange}
/>
</form>
</div>
)
}
4 changes: 4 additions & 0 deletions examples/with-anthropic/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @type {import('next').NextConfig} */
const nextConfig = {}

module.exports = nextConfig
29 changes: 29 additions & 0 deletions examples/with-anthropic/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "with-anthropic",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"ai-connector": "workspace:*",
"next": "13.4.4-canary.11",
"@anthropic-ai/sdk": "^0.4.3",
"react": "18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/node": "^17.0.12",
"autoprefixer": "^10.4.14",
"@types/react": "18.2.7",
"@types/react-dom": "18.2.4",
"eslint": "^7.32.0",
"eslint-config-next": "13.4.4-canary.11",
"postcss": "^8.4.23",
"tailwindcss": "^3.3.2",
"typescript": "5.0.4"
}
}
6 changes: 6 additions & 0 deletions examples/with-anthropic/postcss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {}
}
}
18 changes: 18 additions & 0 deletions examples/with-anthropic/tailwind.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
'./pages/**/*.{js,ts,jsx,tsx,mdx}',
'./components/**/*.{js,ts,jsx,tsx,mdx}',
'./app/**/*.{js,ts,jsx,tsx,mdx}'
],
theme: {
extend: {
backgroundImage: {
'gradient-radial': 'radial-gradient(var(--tw-gradient-stops))',
'gradient-conic':
'conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))'
}
}
},
plugins: []
}
28 changes: 28 additions & 0 deletions examples/with-anthropic/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"plugins": [
{
"name": "next"
}
],
"paths": {
"@/*": ["./*"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
}
103 changes: 71 additions & 32 deletions packages/core/src/anthropic-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,78 @@ import {
type AIStreamParserOptions
} from './ai-stream'

function parseAnthropicStream({
data,
controller,
counter,
encoder
}: AIStreamParserOptions): void {
try {
const json = JSON.parse(data as string) as {
completion: string
stop: string | null
stop_reason: string | null
truncated: boolean
log_id: string
model: string
exception: string | null
}
const text = json.completion
if (counter < 2 && (/\n/.exec(text) || []).length) {
return
}
// function parseAnthropicStream({
// data,
// controller,
// counter,
// encoder
// }: AIStreamParserOptions): void {
// try {
// const json = JSON.parse(data as string) as {
// completion: string
// stop: string | null
// stop_reason: string | null
// truncated: boolean
// log_id: string
// model: string
// exception: string | null
// }
// const text = json.completion
// if (counter < 2 && (/\n/.exec(text) || []).length) {
// return
// }

const queue = encoder.encode(`${JSON.stringify(text)}\n`)
controller.enqueue(queue)
// const queue = encoder.encode(`${JSON.stringify(text)}\n`)
// controller.enqueue(queue)

counter++
} catch (e) {
controller.error(e)
}
}
// counter++
// } catch (e) {
// controller.error(e)
// }
// }

export function AnthropicStream(
res: Response,
cb?: AIStreamCallbacks
): ReadableStream {
return AIStream(res, parseAnthropicStream, cb)
// export function AnthropicStream(
// res: Response,
// cb?: AIStreamCallbacks
// ): ReadableStream {
// return AIStream(res, parseAnthropicStream, cb)
// }

export function AnthropicStream(callbacks?: AIStreamCallbacks) {
const stream = new TransformStream()
const encoder = new TextEncoder()
const writer = stream.writable.getWriter()
const decoder = new TextDecoder()
let fullResponse = ''
const forkedStream = new TransformStream({
start: async (): Promise<void> => {
if (callbacks?.onStart) {
await callbacks.onStart()
}
},
transform: async (chunk, controller): Promise<void> => {
controller.enqueue(chunk)
const item = decoder.decode(chunk)
const value = JSON.parse(item.split('\n')[0])
if (callbacks?.onToken) {
await callbacks.onToken(value as string)
}
fullResponse += value
},
flush: async (controller): Promise<void> => {
if (callbacks?.onCompletion) {
await callbacks.onCompletion(fullResponse)
}
controller.terminate()
}
})
return {
stream: stream.readable.pipeThrough(forkedStream),
handlers: {
onUpdate: async (completion: string) => {
await writer.ready
await writer.write(encoder.encode(`${JSON.stringify(completion)}\n`))
}
}
}
}
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './streaming-text-response'
export * from './use-chat'
export * from './use-completion'
export * from './langchain-stream'
export * from './anthropic-stream'
Loading