langwatch 0.1.0 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/copy-types.sh +17 -0
  2. package/dist/chunk-2I4YLOQY.mjs +736 -0
  3. package/dist/chunk-2I4YLOQY.mjs.map +1 -0
  4. package/dist/index.d.mts +352 -4
  5. package/dist/index.d.ts +352 -4
  6. package/dist/index.js +6505 -413
  7. package/dist/index.js.map +1 -1
  8. package/dist/index.mjs +571 -359
  9. package/dist/index.mjs.map +1 -1
  10. package/dist/{utils-DDcm0z9v.d.mts → utils-CFtM8VVg.d.mts} +108 -31
  11. package/dist/{utils-DDcm0z9v.d.ts → utils-CFtM8VVg.d.ts} +108 -31
  12. package/dist/utils.d.mts +1 -2
  13. package/dist/utils.d.ts +1 -2
  14. package/dist/utils.js +437 -0
  15. package/dist/utils.js.map +1 -1
  16. package/dist/utils.mjs +3 -1
  17. package/example/README.md +3 -1
  18. package/example/app/(chat)/chat/[id]/page.tsx +1 -1
  19. package/example/app/(chat)/page.tsx +10 -5
  20. package/example/app/guardrails/page.tsx +26 -0
  21. package/example/app/langchain/page.tsx +27 -0
  22. package/example/app/langchain-rag/page.tsx +28 -0
  23. package/example/app/share/[id]/page.tsx +1 -1
  24. package/example/components/chat-list.tsx +1 -1
  25. package/example/components/chat-panel.tsx +1 -1
  26. package/example/components/header.tsx +39 -13
  27. package/example/components/prompt-form.tsx +1 -1
  28. package/example/components/stocks/stock-purchase.tsx +1 -1
  29. package/example/components/stocks/stocks.tsx +1 -1
  30. package/example/lib/chat/guardrails.tsx +181 -0
  31. package/example/lib/chat/langchain-rag.tsx +191 -0
  32. package/example/lib/chat/langchain.tsx +112 -0
  33. package/example/lib/chat/{actions.tsx → vercel-ai.tsx} +1 -1
  34. package/example/package-lock.json +289 -6
  35. package/example/package.json +1 -0
  36. package/package.json +13 -5
  37. package/src/evaluations.ts +219 -0
  38. package/src/index.test.ts +5 -0
  39. package/src/index.ts +190 -7
  40. package/src/langchain.ts +557 -0
  41. package/src/{helpers.ts → typeUtils.ts} +20 -2
  42. package/src/types.ts +7 -3
  43. package/src/utils.ts +25 -2
  44. package/ts-to-zod.config.js +2 -0
  45. package/dist/chunk-AP23NJ57.mjs +0 -296
  46. package/dist/chunk-AP23NJ57.mjs.map +0 -1
@@ -0,0 +1,181 @@
1
+ import 'server-only'
2
+
3
+ import {
4
+ createAI,
5
+ createStreamableValue,
6
+ getMutableAIState,
7
+ type MutableAIState
8
+ } from 'ai/rsc'
9
+
10
+ import { BotMessage } from '@/components/stocks'
11
+
12
+ import { Message } from '@/lib/types'
13
+ import { nanoid } from '@/lib/utils'
14
+ import { StringOutputParser } from '@langchain/core/output_parsers'
15
+ import { ChatPromptTemplate } from '@langchain/core/prompts'
16
+ import { ChatOpenAI } from '@langchain/openai'
17
+ import { LangWatch, type LangWatchTrace } from 'langwatch'
18
+
19
+ async function submitUserMessage(message: string) {
20
+ 'use server'
21
+
22
+ const langwatch = new LangWatch()
23
+ langwatch.on('error', e => {
24
+ console.log('Error from LangWatch:', e)
25
+ })
26
+
27
+ const trace = langwatch.getTrace()
28
+
29
+ const aiState = getMutableAIState<typeof Guardrails>()
30
+ const textStream = createStreamableValue('')
31
+ const textNode = <BotMessage content={textStream.value} />
32
+
33
+ void llmStep({ message, trace, aiState, textStream })
34
+
35
+ return {
36
+ id: nanoid(),
37
+ display: textNode
38
+ }
39
+ }
40
+
41
+ async function llmStep({
42
+ message,
43
+ trace,
44
+ aiState,
45
+ textStream
46
+ }: {
47
+ message: string
48
+ trace: LangWatchTrace
49
+ aiState: MutableAIState<AIState>
50
+ textStream: ReturnType<typeof createStreamableValue>
51
+ }) {
52
+ 'use server'
53
+
54
+ textStream.update('Running Jailbreak guardrail...\n\n')
55
+
56
+ const jailbreakPromise = trace.evaluate({
57
+ evaluator: 'azure/jailbreak',
58
+ name: 'Jailbreak Detection',
59
+ input: message,
60
+ settings: {},
61
+ asGuardrail: true
62
+ })
63
+
64
+ const prompt = ChatPromptTemplate.fromMessages([
65
+ ['system', 'Translate the following from English into Italian'],
66
+ ['human', '{input}']
67
+ ])
68
+ const model = new ChatOpenAI({ model: 'gpt-4o-mini' })
69
+ const outputParser = new StringOutputParser()
70
+
71
+ const chain = prompt.pipe(model).pipe(outputParser)
72
+
73
+ const chainPromise = chain.invoke(
74
+ { input: message },
75
+ { callbacks: [trace.getLangChainCallback()] }
76
+ )
77
+
78
+ const [jailbreakResult, result] = await Promise.all([
79
+ jailbreakPromise,
80
+ chainPromise
81
+ ])
82
+
83
+ if (!jailbreakResult.passed) {
84
+ textStream.update('Jailbreak detected, stopping execution.')
85
+ textStream.done()
86
+ aiState.done()
87
+ return
88
+ }
89
+
90
+ aiState.update({
91
+ ...aiState.get(),
92
+ messages: [
93
+ {
94
+ id: nanoid(),
95
+ role: 'system',
96
+ content: 'Translate the following from English into Italian'
97
+ },
98
+ {
99
+ id: nanoid(),
100
+ role: 'user',
101
+ content: message
102
+ }
103
+ ]
104
+ })
105
+
106
+ textStream.update('Running Moderation guardrail...\n\n')
107
+
108
+ const moderationGuardrail = await trace.evaluate({
109
+ evaluator: 'openai/moderation',
110
+ asGuardrail: true,
111
+ name: 'Moderation',
112
+ input: message,
113
+ output: result, // optional
114
+ settings: {
115
+ model: 'text-moderation-stable',
116
+ categories: {
117
+ harassment: true,
118
+ harassment_threatening: true,
119
+ hate: true,
120
+ hate_threatening: true,
121
+ self_harm: true,
122
+ self_harm_instructions: true,
123
+ self_harm_intent: true,
124
+ sexual: true,
125
+ sexual_minors: true,
126
+ violence: true,
127
+ violence_graphic: true
128
+ }
129
+ }
130
+ })
131
+
132
+ if (!moderationGuardrail.passed) {
133
+ textStream.update('Moderation failed, stopping execution.')
134
+ textStream.done()
135
+ aiState.done()
136
+ return
137
+ }
138
+
139
+ textStream.update(result)
140
+ textStream.done()
141
+
142
+ aiState.done({
143
+ ...aiState.get(),
144
+ messages: [
145
+ ...aiState.get().messages,
146
+ {
147
+ id: nanoid(),
148
+ role: 'assistant',
149
+ content: result
150
+ }
151
+ ]
152
+ })
153
+ }
154
+
155
+ export type AIState = {
156
+ chatId: string
157
+ messages: Message[]
158
+ }
159
+
160
+ export type UIState = {
161
+ id: string
162
+ display: React.ReactNode
163
+ }[]
164
+
165
+ export const Guardrails = createAI<AIState, UIState>({
166
+ actions: {
167
+ submitUserMessage
168
+ },
169
+ initialUIState: [],
170
+ initialAIState: { chatId: nanoid(), messages: [] },
171
+ onGetUIState: async () => {
172
+ 'use server'
173
+
174
+ return undefined
175
+ },
176
+ onSetAIState: async ({ state }) => {
177
+ 'use server'
178
+
179
+ return
180
+ }
181
+ })
@@ -0,0 +1,191 @@
1
+ import 'server-only'
2
+
3
+ import { openai } from '@ai-sdk/openai'
4
+ import {
5
+ createAI,
6
+ createStreamableUI,
7
+ createStreamableValue,
8
+ getMutableAIState,
9
+ streamUI
10
+ } from 'ai/rsc'
11
+
12
+ import { BotCard, BotMessage, Purchase, Stock } from '@/components/stocks'
13
+
14
+ import { Events } from '@/components/stocks/events'
15
+ import { SpinnerMessage, UserMessage } from '@/components/stocks/message'
16
+ import { Stocks } from '@/components/stocks/stocks'
17
+ import { Chat, Message } from '@/lib/types'
18
+ import { nanoid } from '@/lib/utils'
19
+ import { LangWatch, convertFromVercelAIMessages } from 'langwatch'
20
+ import { ChatOpenAI } from '@langchain/openai'
21
+ import {
22
+ ChatPromptTemplate,
23
+ PromptTemplateInput
24
+ } from '@langchain/core/prompts'
25
+ import {
26
+ HumanMessage,
27
+ SystemMessage,
28
+ AIMessage,
29
+ ToolMessage,
30
+ BaseMessageLike
31
+ } from '@langchain/core/messages'
32
+ import { StringOutputParser } from '@langchain/core/output_parsers'
33
+ import { CallbackManagerForRetrieverRun } from '@langchain/core/callbacks/manager'
34
+ import {
35
+ BaseRetriever,
36
+ type BaseRetrieverInput
37
+ } from '@langchain/core/retrievers'
38
+ import { Document } from '@langchain/core/documents'
39
+ import {
40
+ RunnableLambda,
41
+ RunnableMap,
42
+ RunnablePassthrough
43
+ } from '@langchain/core/runnables'
44
+
45
+ async function submitUserMessage(message: string) {
46
+ 'use server'
47
+
48
+ const langwatch = new LangWatch()
49
+ langwatch.on('error', e => {
50
+ console.log('Error from LangWatch:', e)
51
+ })
52
+
53
+ const trace = langwatch.getTrace()
54
+
55
+ const aiState = getMutableAIState<typeof LangChainRAGAI>()
56
+
57
+ const messages: BaseMessageLike[] = [
58
+ ['system', 'Answer based on the retrieved context'],
59
+ ...(aiState.get().messages.map(message => {
60
+ if (message.role === 'system') {
61
+ return ['system', message.content.toString()]
62
+ }
63
+ if (message.role === 'user') {
64
+ return ['human', message.content.toString()]
65
+ }
66
+ if (message.role === 'tool') {
67
+ return ['tool', message.content.toString()]
68
+ }
69
+ return ['ai', message.content.toString()]
70
+ }) as BaseMessageLike[]),
71
+ ['ai', 'Retrieved the following context: {context}'],
72
+ ['human', '{question}']
73
+ ]
74
+
75
+ aiState.update({
76
+ ...aiState.get(),
77
+ messages: [
78
+ ...aiState.get().messages,
79
+ {
80
+ id: nanoid(),
81
+ role: 'user',
82
+ content: message
83
+ }
84
+ ]
85
+ })
86
+
87
+ const prompt = ChatPromptTemplate.fromMessages(messages)
88
+ const model = new ChatOpenAI({ model: 'gpt-4o-mini' })
89
+ const retriever = new CustomRetriever()
90
+ const outputParser = new StringOutputParser()
91
+
92
+ const setupAndRetrieval = RunnableMap.from({
93
+ context: new RunnableLambda({
94
+ func: (input: string) =>
95
+ retriever
96
+ .invoke(input, {
97
+ callbacks: [trace.getLangChainCallback()]
98
+ })
99
+ .then(response => response[0].pageContent)
100
+ }).withConfig({ runName: 'contextRetriever' }),
101
+ question: new RunnablePassthrough()
102
+ })
103
+
104
+ const chain = setupAndRetrieval.pipe(prompt).pipe(model).pipe(outputParser)
105
+
106
+ const stream = await chain.stream(message, {
107
+ callbacks: [trace.getLangChainCallback()]
108
+ })
109
+
110
+ let textStream = createStreamableValue('')
111
+ let textNode = <BotMessage content={textStream.value} />
112
+ let content = ''
113
+
114
+ setTimeout(async () => {
115
+ for await (const chunk of stream) {
116
+ textStream.update(chunk)
117
+ content += chunk
118
+ }
119
+
120
+ textStream?.done()
121
+ aiState.done({
122
+ ...aiState.get(),
123
+ messages: [
124
+ ...aiState.get().messages,
125
+ {
126
+ id: nanoid(),
127
+ role: 'assistant',
128
+ content
129
+ }
130
+ ]
131
+ })
132
+ }, 0)
133
+
134
+ return {
135
+ id: nanoid(),
136
+ display: textNode
137
+ }
138
+ }
139
+
140
+ export type AIState = {
141
+ chatId: string
142
+ messages: Message[]
143
+ }
144
+
145
+ export type UIState = {
146
+ id: string
147
+ display: React.ReactNode
148
+ }[]
149
+
150
+ export const LangChainRAGAI = createAI<AIState, UIState>({
151
+ actions: {
152
+ submitUserMessage
153
+ },
154
+ initialUIState: [],
155
+ initialAIState: { chatId: nanoid(), messages: [] },
156
+ onGetUIState: async () => {
157
+ 'use server'
158
+
159
+ return undefined
160
+ },
161
+ onSetAIState: async ({ state }) => {
162
+ 'use server'
163
+
164
+ return
165
+ }
166
+ })
167
+
168
+ export class CustomRetriever extends BaseRetriever {
169
+ lc_namespace = ['langchain', 'retrievers']
170
+
171
+ constructor(fields?: BaseRetrieverInput) {
172
+ super(fields)
173
+ }
174
+
175
+ async _getRelevantDocuments(
176
+ query: string,
177
+ _runManager?: CallbackManagerForRetrieverRun
178
+ ): Promise<Document[]> {
179
+ console.log('query', query)
180
+ return [
181
+ new Document({
182
+ pageContent: `Some document pertaining to ${query}`,
183
+ metadata: {}
184
+ }),
185
+ new Document({
186
+ pageContent: `Some other document pertaining to ${query}`,
187
+ metadata: {}
188
+ })
189
+ ]
190
+ }
191
+ }
@@ -0,0 +1,112 @@
1
+ import 'server-only'
2
+
3
+ import { createAI, createStreamableValue, getMutableAIState } from 'ai/rsc'
4
+
5
+ import { BotMessage } from '@/components/stocks'
6
+
7
+ import { Message } from '@/lib/types'
8
+ import { nanoid } from '@/lib/utils'
9
+ import { StringOutputParser } from '@langchain/core/output_parsers'
10
+ import { ChatPromptTemplate } from '@langchain/core/prompts'
11
+ import { ChatOpenAI } from '@langchain/openai'
12
+ import { LangWatch } from 'langwatch'
13
+
14
+ async function submitUserMessage(message: string) {
15
+ 'use server'
16
+
17
+ const langwatch = new LangWatch()
18
+ langwatch.on('error', e => {
19
+ console.log('Error from LangWatch:', e)
20
+ })
21
+
22
+ const trace = langwatch.getTrace()
23
+
24
+ const aiState = getMutableAIState<typeof LangChainAI>()
25
+
26
+ aiState.update({
27
+ ...aiState.get(),
28
+ messages: [
29
+ {
30
+ id: nanoid(),
31
+ role: 'system',
32
+ content: 'Translate the following from English into Italian'
33
+ },
34
+ {
35
+ id: nanoid(),
36
+ role: 'user',
37
+ content: message
38
+ }
39
+ ]
40
+ })
41
+
42
+ const prompt = ChatPromptTemplate.fromMessages([
43
+ ['system', 'Translate the following from English into Italian'],
44
+ ['human', '{input}']
45
+ ])
46
+ const model = new ChatOpenAI({ model: 'gpt-4o-mini' })
47
+ const outputParser = new StringOutputParser()
48
+
49
+ const chain = prompt.pipe(model).pipe(outputParser)
50
+
51
+ const stream = await chain.stream(
52
+ { input: message },
53
+ { callbacks: [trace.getLangChainCallback()] }
54
+ )
55
+
56
+ let textStream = createStreamableValue('')
57
+ let textNode = <BotMessage content={textStream.value} />
58
+ let content = ''
59
+
60
+ setTimeout(async () => {
61
+ for await (const chunk of stream) {
62
+ textStream.update(chunk)
63
+ content += chunk
64
+ }
65
+
66
+ textStream?.done()
67
+ aiState.done({
68
+ ...aiState.get(),
69
+ messages: [
70
+ ...aiState.get().messages,
71
+ {
72
+ id: nanoid(),
73
+ role: 'assistant',
74
+ content
75
+ }
76
+ ]
77
+ })
78
+ }, 0)
79
+
80
+ return {
81
+ id: nanoid(),
82
+ display: textNode
83
+ }
84
+ }
85
+
86
+ export type AIState = {
87
+ chatId: string
88
+ messages: Message[]
89
+ }
90
+
91
+ export type UIState = {
92
+ id: string
93
+ display: React.ReactNode
94
+ }[]
95
+
96
+ export const LangChainAI = createAI<AIState, UIState>({
97
+ actions: {
98
+ submitUserMessage
99
+ },
100
+ initialUIState: [],
101
+ initialAIState: { chatId: nanoid(), messages: [] },
102
+ onGetUIState: async () => {
103
+ 'use server'
104
+
105
+ return undefined
106
+ },
107
+ onSetAIState: async ({ state }) => {
108
+ 'use server'
109
+
110
+ return
111
+ }
112
+ })
@@ -148,7 +148,7 @@ async function submitUserMessage(content: string) {
148
148
  Besides that, you can also chat with users and do some calculations if needed.`
149
149
 
150
150
  const span = trace.startLLMSpan({
151
- model: 'gpt-3.5-turbo',
151
+ model: 'gpt-4o-mini',
152
152
  input: {
153
153
  type: 'chat_messages',
154
154
  value: [