Skip to content

Commit

Permalink
Add support for conversations
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonbosco committed Mar 13, 2024
1 parent 277bc97 commit 51b191e
Show file tree
Hide file tree
Showing 33 changed files with 1,492 additions and 6 deletions.
372 changes: 372 additions & 0 deletions dist/typesense.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/typesense.js.map

Large diffs are not rendered by default.

171 changes: 171 additions & 0 deletions doc/examples/server/conversations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/*
These examples walk you through all the operations you can do with Conversations
See clientInitalization.js for quick instructions on starting the Typesense server.
*/
require("@babel/register");

// eslint-disable-next-line @typescript-eslint/no-var-requires
const Typesense = require("../../../lib/Typesense");

// Create a client
const typesense = new Typesense.Client({
nodes: [
{
host: "localhost",
port: "8108",
protocol: "http",
},
],
apiKey: "xyz",
numRetries: 3, // A total of 4 tries (1 original try + 3 retries)
connectionTimeoutSeconds: 120, // Set a longer timeout for large imports
logLevel: "debug",
});

let schema = {
name: "companies",
num_documents: 0,
fields: [
{
name: "company_name",
type: "string",
facet: false,
},
{
name: "num_employees",
type: "int32",
facet: false,
},
{
name: "country",
type: "string",
facet: true,
},
{
name: "embedding",
type: "float[]",
embed: {
from: ["company_name", "country"],
model_config: {
model_name: "ts/all-MiniLM-L12-v2",
},
},
},
],
default_sorting_field: "num_employees",
};

let documents = [
{
id: "124",
company_name: "Stark Industries",
num_employees: 5215,
country: "USA",
},
{
id: "126",
company_name: "Wayne Enterprises",
num_employees: 1002,
country: "Canada",
},
];

async function runExample() {
try {
// Delete if the collection already exists from a previous example run
await typesense.collections("companies").delete();
} catch (error) {
// do nothing
}

try {
// Delete if the conversation model already exists from a previous example run
await typesense.collections("companies").delete();
} catch (error) {
// do nothing
}

try {
let result;
// create a collection
result = await typesense.collections().create(schema);
console.log(result);

// create a couple of documents
result = await typesense
.collections("companies")
.documents()
.import(documents);
console.log(result);

// Create a conversation model
let conversationModelResult = await typesense
.conversations()
.models()
.create({
model_name: "openai/gpt-3.5-turbo",
api_key: process.env.OPENAI_API_KEY,
system_prompt: "Be very elaborate in your responses",
max_bytes: 1024,
});
console.log(result);

// Retrieve all conversation models
result = await typesense.conversations().models().retrieve();
console.log(result);

// Retrieve a particular conversation model
result = await typesense
.conversations()
.models(conversationModelResult.id)
.retrieve();
console.log(result);

// Search for documents in conversation mode
result = await typesense.collections("companies").documents().search({
q: "What is the name of the company that Batman ran?",
query_by: "embedding",
conversation: true,
conversation_model_id: conversationModelResult.id,
});
console.log(result.conversation);

let conversationId = result.conversation.conversation_id;

// Ask a follow-up question, in the same conversation
result = await typesense.collections("companies").documents().search({
q: "Tell me more about it",
query_by: "embedding",
conversation: true,
conversation_model_id: conversationModelResult.id,
conversation_id: conversationId,
});
console.log(result.conversation);

// Fetch all conversations
result = await typesense.conversations().retrieve();
console.log(result);

// Fetch a past conversation
result = await typesense.conversations(conversationId).retrieve();
console.log(result);

// Update TTL of past conversation
result = await typesense.conversations(conversationId).update({ ttl: 10 });
console.log(result);

// Delete conversation model
result = await typesense
.conversations()
.models(conversationModelResult.id)
.delete();
console.log(result);
} catch (error) {
console.log(error);
} finally {
// Cleanup
typesense.collections("companies").delete();
}
}

runExample();
2 changes: 1 addition & 1 deletion lib/Typesense/AnalyticsRule.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ApiCall from "./ApiCall";
export interface AnalyticsRuleCreateSchema {
type: "popular_queries";
type: "popular_queries" | "nohits_queries";
params: {
source: {
collections: string[];
Expand Down
2 changes: 1 addition & 1 deletion lib/Typesense/AnalyticsRule.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions lib/Typesense/Client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import Preset from "./Preset";
import Analytics from "./Analytics";
import Stopwords from "./Stopwords";
import Stopword from "./Stopword";
import Conversations from "./Conversations";
import Conversation from "./Conversation";
export default class Client {
configuration: Configuration;
apiCall: ApiCall;
Expand All @@ -35,6 +37,8 @@ export default class Client {
private readonly individualPresets;
private readonly _stopwords;
private readonly individualStopwords;
private readonly _conversations;
private readonly individualConversations;
constructor(options: ConfigurationOptions);
collections(): Collections;
collections<T extends Record<string, any> = object>(collectionName: string): Collection<T>;
Expand All @@ -46,4 +50,6 @@ export default class Client {
presets(id: string): Preset;
stopwords(): Stopwords;
stopwords(id: string): Stopword;
conversations(): Conversations;
conversations(id: string): Conversation;
}
15 changes: 15 additions & 0 deletions lib/Typesense/Client.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/Typesense/Client.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions lib/Typesense/Conversation.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import ApiCall from "./ApiCall";
export interface ConversationDeleteSchema {
id: number;
}
export interface ConversationUpdateSchema {
ttl: number;
}
export interface ConversationSchema {
id: number;
conversation: object[];
last_updated: number;
ttl: number;
}
export default class Conversation {
private id;
private apiCall;
constructor(id: string, apiCall: ApiCall);
retrieve(): Promise<ConversationSchema[]>;
update(params: ConversationUpdateSchema): Promise<ConversationUpdateSchema>;
delete(): Promise<ConversationDeleteSchema>;
private endpointPath;
}
Loading

0 comments on commit 51b191e

Please sign in to comment.