-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
277bc97
commit 51b191e
Showing
33 changed files
with
1,492 additions
and
6 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.