@markprompt/core
Advanced tools
Comparing version 0.19.1 to 0.20.0
@@ -1,3 +0,3 @@ | ||
import type { OpenAI } from 'openai'; | ||
import type { ChatCompletionMetadata, FileSectionReference, OpenAIModelId } from './types.js'; | ||
import type { Chat, ChatCompletionMessage, ChatCompletionMessageParam, ChatCompletionTool, ChatCompletionToolChoiceOption, ChatCompletionMetadata, FileSectionReference, OpenAIModelId } from './types.js'; | ||
export type { ChatCompletionMessageParam, ChatCompletionAssistantMessageParam, ChatCompletionFunctionMessageParam, ChatCompletionToolMessageParam, ChatCompletionUserMessageParam, ChatCompletionSystemMessageParam, } from 'openai/resources/index.mjs'; | ||
export interface SubmitChatOptions { | ||
@@ -120,58 +120,2 @@ /** | ||
export declare function submitChat(messages: ChatMessage[], projectKey: string, onAnswerChunk: (answerChunk: string) => boolean | undefined | void, onReferences: (references: FileSectionReference[]) => void, onConversationId: (conversationId: string) => void, onPromptId: (promptId: string) => void, onError: (error: Error) => void, options?: SubmitChatOptions, debug?: boolean): Promise<void>; | ||
interface ToolCall { | ||
/** The ID of the tool call. */ | ||
id: string; | ||
/** The type of the tool. Currently, only `function` is supported. */ | ||
type: 'function'; | ||
/** The function that the model called. */ | ||
function: { | ||
/** The name of the function to call. */ | ||
name: string; | ||
/** The arguments to call the function with, as generated by the model in JSON format. Note that the model does not always generate valid JSON, and may hallucinate parameters not defined by your function schema. Validate the arguments in your code before calling your function. */ | ||
arguments: string; | ||
}; | ||
} | ||
interface SystemMessage { | ||
/** The contents of the system message. */ | ||
content: string | null; | ||
/** The role of the messages author, in this case `system`. */ | ||
role: 'system'; | ||
/** An optional name for the participant. Provides the model information to differentiate between participants of the same role. */ | ||
name?: string; | ||
} | ||
interface TextContentPart { | ||
type: 'text'; | ||
text: string; | ||
} | ||
interface ImageUrlContentPart { | ||
type: 'image_url'; | ||
image_url: string; | ||
} | ||
interface UserMessage { | ||
/** The contents of the user message. */ | ||
content: string | (TextContentPart | ImageUrlContentPart)[]; | ||
/** The role of the messages author, in this case `user`. */ | ||
role: 'user'; | ||
/** An optional name for the participant. Provides the model information to differentiate between participants of the same role. */ | ||
name?: string; | ||
} | ||
interface AssistantMessage { | ||
/** The contents of the assistant message. */ | ||
content: string | null; | ||
/** The role of the messages author, in this case `assistant`. */ | ||
role: 'assistant'; | ||
/** An optional name for the participant. Provides the model information to differentiate between participants of the same role. */ | ||
name?: string; | ||
/** The tool calls generated by the model, such as function calls. */ | ||
tool_calls?: ToolCall[]; | ||
} | ||
interface ToolMessage { | ||
/** The role of the messages author, in this case `tool`. */ | ||
role: 'tool'; | ||
/** The contents of the tool message. */ | ||
content: string | null; | ||
/** Tool call that this message is responding to. */ | ||
tool_call_id: string; | ||
} | ||
type MessageParam = SystemMessage | UserMessage | AssistantMessage | ToolMessage; | ||
export interface SubmitChatGeneratorOptions { | ||
@@ -269,2 +213,6 @@ /** | ||
/** | ||
* Disable streaming and return the entire response at once. | ||
*/ | ||
stream?: boolean; | ||
/** | ||
* A list of tools the model may call. Currently, only functions are | ||
@@ -274,3 +222,3 @@ * supported as a tool. Use this to provide a list of functions the model may | ||
*/ | ||
tools?: OpenAI.ChatCompletionTool[]; | ||
tools?: ChatCompletionTool[]; | ||
/** | ||
@@ -286,3 +234,3 @@ * Controls which (if any) function is called by the model. `none` means the | ||
*/ | ||
tool_choice?: OpenAI.ChatCompletionToolChoiceOption; | ||
tool_choice?: ChatCompletionToolChoiceOption; | ||
} | ||
@@ -302,7 +250,7 @@ export declare const DEFAULT_SUBMIT_CHAT_GENERATOR_OPTIONS: { | ||
topP: number; | ||
stream: true; | ||
}; | ||
export type SubmitChatYield = OpenAI.Chat.Completions.ChatCompletionChunk.Choice.Delta & ChatCompletionMetadata; | ||
export type SubmitChatReturn = OpenAI.ChatCompletionMessage & ChatCompletionMetadata; | ||
export declare function submitChatGenerator(messages: MessageParam[], projectKey: string, options?: SubmitChatGeneratorOptions): AsyncGenerator<SubmitChatYield, SubmitChatReturn | undefined>; | ||
export {}; | ||
export type SubmitChatYield = Chat.Completions.ChatCompletionChunk.Choice.Delta & ChatCompletionMetadata; | ||
export type SubmitChatReturn = ChatCompletionMessage & ChatCompletionMetadata; | ||
export declare function submitChatGenerator(messages: ChatCompletionMessageParam[], projectKey: string, options?: SubmitChatGeneratorOptions): AsyncGenerator<SubmitChatYield, SubmitChatReturn | undefined>; | ||
//# sourceMappingURL=chat.d.ts.map |
import defaults from 'defaults'; | ||
import { EventSourceParserStream } from 'eventsource-parser/stream'; | ||
import mergeWith from 'lodash-es/mergeWith.js'; | ||
import { safeStringify, isFileSectionReferences, parseEncodedJSONHeader, isMarkpromptMetadata, isChatCompletion, isChatCompletionChunk, isChatCompletionMessage, } from './utils.js'; | ||
import { isChatCompletion, isChatCompletionChunk, isChatCompletionMessage, isFileSectionReferences, isMarkpromptMetadata, parseEncodedJSONHeader, safeStringify, } from './utils.js'; | ||
export const DEFAULT_SUBMIT_CHAT_OPTIONS = { | ||
@@ -27,2 +27,21 @@ apiUrl: 'https://api.markprompt.com/v1/chat', | ||
}; | ||
const validSubmitChatOptionsKeys = [ | ||
'apiUrl', | ||
'conversationId', | ||
'conversationMetadata', | ||
'debug', | ||
'iDontKnowMessage', | ||
'model', | ||
'systemPrompt', | ||
'temperature', | ||
'topP', | ||
'frequencyPenalty', | ||
'presencePenalty', | ||
'maxTokens', | ||
'sectionsMatchCount', | ||
'sectionsMatchThreshold', | ||
]; | ||
const isValidSubmitChatOptionsKey = (key) => { | ||
return validSubmitChatOptionsKeys.includes(key); | ||
}; | ||
/** | ||
@@ -50,3 +69,4 @@ * Submit a prompt to the Markprompt Chat API. | ||
try { | ||
const { signal, ...cloneableOpts } = options; | ||
const validOptions = Object.fromEntries(Object.entries(options).filter(([key]) => isValidSubmitChatOptionsKey(key))); | ||
const { signal, ...cloneableOpts } = validOptions; | ||
const { apiUrl, ...resolvedOptions } = defaults({ ...cloneableOpts }, DEFAULT_SUBMIT_CHAT_OPTIONS); | ||
@@ -133,3 +153,29 @@ const res = await fetch(apiUrl, { | ||
topP: 1, | ||
stream: true, | ||
}; | ||
const validSubmitChatGeneratorOptionsKeys = [ | ||
'apiUrl', | ||
'conversationId', | ||
'conversationMetadata', | ||
'debug', | ||
'doNotInjectContext', | ||
'excludeFromInsights', | ||
'frequencyPenalty', | ||
'iDontKnowMessage', | ||
'maxTokens', | ||
'model', | ||
'presencePenalty', | ||
'sectionsMatchCount', | ||
'sectionsMatchThreshold', | ||
'stream', | ||
'systemPrompt', | ||
'temperature', | ||
'tool_choice', | ||
'tools', | ||
'topP', | ||
'version', | ||
]; | ||
const isValidSubmitChatGeneratorOptionsKey = (key) => { | ||
return validSubmitChatGeneratorOptionsKeys.includes(key); | ||
}; | ||
export async function* submitChatGenerator(messages, projectKey, options = {}) { | ||
@@ -139,3 +185,7 @@ if (!projectKey) { | ||
} | ||
const { signal, ...cloneableOpts } = options; | ||
if (!messages || !Array.isArray(messages) || messages.length === 0) { | ||
return; | ||
} | ||
const validOptions = Object.fromEntries(Object.entries(options).filter(([key]) => isValidSubmitChatGeneratorOptionsKey(key))); | ||
const { signal, ...cloneableOpts } = validOptions; | ||
const { apiUrl, debug, ...resolvedOptions } = defaults({ ...cloneableOpts }, DEFAULT_SUBMIT_CHAT_GENERATOR_OPTIONS); | ||
@@ -168,6 +218,3 @@ const res = await fetch(apiUrl, { | ||
const data = parseEncodedJSONHeader(res, 'x-markprompt-data'); | ||
if (isMarkpromptMetadata(data)) { | ||
yield data; | ||
} | ||
if (res.headers.get('Content-Type') === 'application/json') { | ||
if (res.headers.get('Content-Type')?.includes('application/json')) { | ||
const json = await res.json(); | ||
@@ -183,3 +230,7 @@ if (isChatCompletion(json) && isMarkpromptMetadata(data)) { | ||
} | ||
const completion = {}; | ||
if (isMarkpromptMetadata(data)) { | ||
yield data; | ||
} | ||
// eslint-disable-next-line prefer-const | ||
let completion = {}; | ||
const stream = res.body | ||
@@ -199,3 +250,4 @@ .pipeThrough(new TextDecoderStream()) | ||
} | ||
const json = JSON.parse(event.data); | ||
// eslint-disable-next-line prefer-const | ||
let json = JSON.parse(event.data); | ||
if (!isChatCompletionChunk(json)) { | ||
@@ -206,9 +258,9 @@ throw new Error('Malformed response from Markprompt API', { | ||
} | ||
const delta = json.choices[0].delta; | ||
mergeWith(completion, delta, (destValue, srcValue) => { | ||
const type = typeof srcValue; | ||
if (type === 'string') | ||
return (destValue ?? '') + srcValue; | ||
}); | ||
yield completion; | ||
mergeWith(completion, json.choices[0].delta, concatStrings); | ||
/** | ||
* If we do not yield a structuredClone here, the completion object will | ||
* become read-only/frozen and TypeErrors will be thrown when trying to | ||
* merge the next chunk into it. | ||
*/ | ||
yield structuredClone(completion); | ||
} | ||
@@ -219,2 +271,8 @@ if (isChatCompletionMessage(completion) && isMarkpromptMetadata(data)) { | ||
} | ||
function concatStrings(dest, src) { | ||
if (typeof dest === 'string' && typeof src === 'string') { | ||
return dest + src; | ||
} | ||
return undefined; | ||
} | ||
//# sourceMappingURL=chat.js.map |
@@ -1,6 +0,6 @@ | ||
export { submitChat, type SubmitChatOptions, type ChatMessage, DEFAULT_SUBMIT_CHAT_OPTIONS, submitChatGenerator, type SubmitChatGeneratorOptions, DEFAULT_SUBMIT_CHAT_GENERATOR_OPTIONS, } from './chat.js'; | ||
export { submitFeedback, type SubmitFeedbackOptions, type SubmitFeedbackBody, DEFAULT_SUBMIT_FEEDBACK_OPTIONS, } from './feedback.js'; | ||
export { submitSearchQuery, submitAlgoliaDocsearchQuery, type SubmitSearchQueryOptions, DEFAULT_SUBMIT_SEARCH_QUERY_OPTIONS, } from './search.js'; | ||
export { OPENAI_CHAT_COMPLETIONS_MODELS, OPENAI_COMPLETIONS_MODELS, OPENAI_EMBEDDINGS_MODEL, type AlgoliaDocSearchHit, type AlgoliaDocSearchResultsResponse, type FileReferenceFileData, type FileSectionReference, type FileSectionReferenceSectionData, type OpenAIChatCompletionsModelId, type OpenAICompletionsModelId, type OpenAIEmbeddingsModelId, type OpenAIModelId, type PromptFeedback, type SearchResult, type SearchResultSection, type SearchResultsResponse, type Source, type SourceType, } from './types.js'; | ||
export { isAbortError } from './utils.js'; | ||
export { DEFAULT_SUBMIT_CHAT_GENERATOR_OPTIONS, DEFAULT_SUBMIT_CHAT_OPTIONS, submitChat, submitChatGenerator, type ChatMessage, type SubmitChatGeneratorOptions, type SubmitChatOptions, type SubmitChatReturn, type SubmitChatYield, } from './chat.js'; | ||
export { DEFAULT_SUBMIT_FEEDBACK_OPTIONS, submitFeedback, type SubmitFeedbackBody, type SubmitFeedbackOptions, } from './feedback.js'; | ||
export { DEFAULT_SUBMIT_SEARCH_QUERY_OPTIONS, submitAlgoliaDocsearchQuery, submitSearchQuery, type SubmitSearchQueryOptions, } from './search.js'; | ||
export { OPENAI_CHAT_COMPLETIONS_MODELS, OPENAI_COMPLETIONS_MODELS, OPENAI_EMBEDDINGS_MODEL, type AlgoliaDocSearchHit, type AlgoliaDocSearchResultsResponse, type Chat, type ChatCompletion, type ChatCompletionAssistantMessageParam, type ChatCompletionChunk, type ChatCompletionFunctionMessageParam, type ChatCompletionMessage, type ChatCompletionMessageParam, type ChatCompletionMessageToolCall, type ChatCompletionSystemMessageParam, type ChatCompletionTool, type ChatCompletionToolChoiceOption, type ChatCompletionToolMessageParam, type ChatCompletionUserMessageParam, type FileReferenceFileData, type FileSectionReference, type FileSectionReferenceSectionData, type OpenAIChatCompletionsModelId, type OpenAICompletionsModelId, type OpenAIEmbeddingsModelId, type OpenAIModelId, type PromptFeedback, type SearchResult, type SearchResultSection, type SearchResultsResponse, type Source, type SourceType, } from './types.js'; | ||
export { getErrorMessage, isAbortError, isChatCompletion, isChatCompletionChunk, isChatCompletionMessage, isFileSectionReferences, isKeyOf, isMarkpromptMetadata, isToolCall, isToolCalls, parseEncodedJSONHeader, safeStringify, } from './utils.js'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -1,6 +0,6 @@ | ||
export { submitChat, DEFAULT_SUBMIT_CHAT_OPTIONS, submitChatGenerator, DEFAULT_SUBMIT_CHAT_GENERATOR_OPTIONS, } from './chat.js'; | ||
export { submitFeedback, DEFAULT_SUBMIT_FEEDBACK_OPTIONS, } from './feedback.js'; | ||
export { submitSearchQuery, submitAlgoliaDocsearchQuery, DEFAULT_SUBMIT_SEARCH_QUERY_OPTIONS, } from './search.js'; | ||
export { DEFAULT_SUBMIT_CHAT_GENERATOR_OPTIONS, DEFAULT_SUBMIT_CHAT_OPTIONS, submitChat, submitChatGenerator, } from './chat.js'; | ||
export { DEFAULT_SUBMIT_FEEDBACK_OPTIONS, submitFeedback, } from './feedback.js'; | ||
export { DEFAULT_SUBMIT_SEARCH_QUERY_OPTIONS, submitAlgoliaDocsearchQuery, submitSearchQuery, } from './search.js'; | ||
export { OPENAI_CHAT_COMPLETIONS_MODELS, OPENAI_COMPLETIONS_MODELS, OPENAI_EMBEDDINGS_MODEL, } from './types.js'; | ||
export { isAbortError } from './utils.js'; | ||
export { getErrorMessage, isAbortError, isChatCompletion, isChatCompletionChunk, isChatCompletionMessage, isFileSectionReferences, isKeyOf, isMarkpromptMetadata, isToolCall, isToolCalls, parseEncodedJSONHeader, safeStringify, } from './utils.js'; | ||
//# sourceMappingURL=index.js.map |
import type { DocSearchHit } from './docsearch.js'; | ||
export type { Chat, ChatCompletion, ChatCompletionAssistantMessageParam, ChatCompletionChunk, ChatCompletionFunctionMessageParam, ChatCompletionMessage, ChatCompletionMessageParam, ChatCompletionMessageToolCall, ChatCompletionSystemMessageParam, ChatCompletionTool, ChatCompletionToolChoiceOption, ChatCompletionToolMessageParam, ChatCompletionUserMessageParam, } from 'openai/resources/index.mjs'; | ||
export type RequiredKeys<T, K extends keyof T> = Required<Pick<T, K>> & Omit<T, K>; | ||
@@ -3,0 +4,0 @@ export declare const OPENAI_CHAT_COMPLETIONS_MODELS: readonly ["gpt-3.5-turbo", "gpt-4-1106-preview", "gpt-4-32k", "gpt-4"]; |
@@ -1,3 +0,3 @@ | ||
import type { OpenAI } from 'openai'; | ||
import type { ChatCompletionMetadata, FileSectionReference } from './types.js'; | ||
import type { ChatCompletion, ChatCompletionChunk, ChatCompletionMessage, ChatCompletionMessageToolCall, ChatCompletionMetadata, FileSectionReference } from './types.js'; | ||
export type { ChatCompletion, ChatCompletionChunk, ChatCompletionMessage, ChatCompletionMessageToolCall, } from 'openai/resources/index.mjs'; | ||
export declare const getErrorMessage: (res: Response) => Promise<string>; | ||
@@ -11,8 +11,8 @@ export declare const parseEncodedJSONHeader: (response: Response, name: string) => unknown | undefined; | ||
export declare function isMarkpromptMetadata(json: unknown): json is ChatCompletionMetadata; | ||
export declare function isChatCompletion(json: unknown): json is OpenAI.ChatCompletion; | ||
export declare const isChatCompletionMessage: (obj: unknown) => obj is OpenAI.Chat.Completions.ChatCompletionMessage; | ||
export declare const isToolCall: (tool_call: unknown) => tool_call is OpenAI.Chat.Completions.ChatCompletionMessageToolCall; | ||
export declare const isToolCalls: (tool_calls: unknown) => tool_calls is OpenAI.Chat.Completions.ChatCompletionMessageToolCall[]; | ||
export declare const isChatCompletionChunk: (json: unknown) => json is OpenAI.Chat.Completions.ChatCompletionChunk; | ||
export declare function isChatCompletion(json: unknown): json is ChatCompletion; | ||
export declare const isChatCompletionMessage: (obj: unknown) => obj is ChatCompletionMessage; | ||
export declare const isToolCall: (tool_call: unknown) => tool_call is ChatCompletionMessageToolCall; | ||
export declare const isToolCalls: (tool_calls: unknown) => tool_calls is ChatCompletionMessageToolCall[]; | ||
export declare const isChatCompletionChunk: (json: unknown) => json is ChatCompletionChunk; | ||
export declare const isKeyOf: <T extends object>(obj: T, key: PropertyKey) => key is keyof T; | ||
//# sourceMappingURL=utils.d.ts.map |
@@ -87,9 +87,3 @@ export const getErrorMessage = async (res) => { | ||
tool_call.type === 'function' && | ||
'function' in tool_call && | ||
typeof tool_call.function === 'object' && | ||
tool_call.function !== null && | ||
'name' in tool_call.function && | ||
typeof tool_call.function.name === 'string' && | ||
'arguments' in tool_call.function && | ||
typeof tool_call.function.arguments === 'string'); | ||
'function' in tool_call); | ||
}; | ||
@@ -96,0 +90,0 @@ export const isToolCalls = (tool_calls) => { |
{ | ||
"name": "@markprompt/core", | ||
"version": "0.19.1", | ||
"version": "0.20.0", | ||
"repository": { | ||
@@ -5,0 +5,0 @@ "type": "git", |
@@ -31,2 +31,3 @@ # `@markprompt/core` | ||
submitChat, | ||
submitChatGenerator, | ||
submitSearchQuery, | ||
@@ -41,3 +42,3 @@ submitFeedback, | ||
```js | ||
import { submitChat } from '@markprompt/core'; | ||
import { submitChatGenerator } from '@markprompt/core'; | ||
@@ -49,26 +50,2 @@ // User input | ||
// Called when a new answer chunk is available | ||
// Should be concatenated to previous chunks | ||
function onAnswerChunk(chunk) { | ||
// Process an answer chunk | ||
} | ||
// Called when references are available | ||
function onReferences(references) { | ||
// Process references | ||
} | ||
function onConversationId(conversationId) { | ||
// Store conversationId for future use | ||
} | ||
function onPromptId(promptId) { | ||
// Store promptId for future use | ||
} | ||
// Called when submitChat encounters an error | ||
function onError(error) { | ||
// Handle errors | ||
} | ||
// Optional parameters, defaults displayed | ||
@@ -78,15 +55,12 @@ const options = { | ||
iDontKnowMessage: 'Sorry, I am not sure how to answer that.', | ||
apiUrl: 'https://api.markprompt.com/v1/chat', // Or your own chat API endpoint | ||
apiUrl: 'https://api.markprompt.com/chat', // Or your own chat API endpoint | ||
}; | ||
await submitChat( | ||
for await (const chunk of submitChatGenerator( | ||
[{ content: prompt, role: 'user' }], | ||
projectKey, | ||
onAnswerChunk, | ||
onReferences, | ||
onConversationId, | ||
onPromptId, | ||
onError, | ||
options, | ||
); | ||
)) { | ||
console.log(chunk); | ||
} | ||
``` | ||
@@ -98,2 +72,4 @@ | ||
**Deprecated**. Use `submitChatGenerator` instead. | ||
Submit a prompt to the Markprompt Completions API. | ||
@@ -100,0 +76,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
71835
1001
171