Skip to content

study-iitm/iitmdocs

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

IITM BS Chatbot

Usage

  1. Set up a Weaviate cluster and get an API key.
  2. Get an OpenAI API key
  3. Fill in these keys in .env and copy it into .dev.vars (both are .git-ignored).
    WEAVIATE_URL=https://zlpipialqvivotmfurmzpw.c0.asia-southeast1.gcp.weaviate.cloud
    WEAVIATE_API_KEY=...
    OPENAI_API_KEY=...
  4. Run uv run embed.py to upload embeddings into Weaviate
  5. Run npm install to install dependencies
  6. Set up CloudFlare keys using the same keys from .dev.vars
    npx wrangler secret put WEAVIATE_URL
    npx wrangler secret put WEAVIATE_API_KEY
    npx wrangler secret put OPENAI_API_KEY
  7. Run npx wrangler dev to test at https://localhost:8787
  8. Run npx wrangler deploy to deploy to production

Embedding

The embedding system processes src/*.md and stores them in Weaviate Cloud with vector embeddings generated by OpenAI's text-embedding-3-small model.

embed.py creates a Document collection with the following properties:

  • filename: Name of the source file
  • filepath: Full path to the source file
  • content: Complete file content
  • file_size: File size in bytes
  • content_hash: SHA256 hash for duplicate detection
  • file_extension: File extension (.md)

These are embedded with OpenAI's text-embedding-3-small model (1536 dimensions) from content.

Modified files are replaced. New documents are inserted. Deleted documents are not deleted. #TODO

Now you can query the documents using Weaviate's GraphQL API or Python client. For example:

import os
import weaviate

client = weaviate.connect_to_weaviate_cloud(
    cluster_url=os.getenv("WEAVIATE_URL"),
    auth_credentials=weaviate.AuthApiKey(os.getenv("WEAVIATE_API_KEY")),
    headers={"X-OpenAI-Api-Key": os.getenv("OPENAI_API_KEY")},
)

collection = client.collections.get("Document")
print(collection.query.hybrid(query="admission process", limit=3))
client.close()

Querying

A CloudFlare Worker provides semantic document search and AI-powered question answering using Weaviate and OpenAI. Run:

curl http://localhost:8787/answer \
  -H 'Content-Type: application/json' \
  -d '{"q": "How do I register for courses", "ndocs": 3}'

The is a text/event-stream subset of the OpenAI chat completion object. Here are the fields:

data: {"choices": [{"delta": {"tool_calls": { "function": { "name": "document", "arguments": "{\"name\": ..., \"link\": ... }" }}}}]}

data: {"choices": [{"delta": {"tool_calls": { "function": { "name": "document", "arguments": "{\"name\": ..., \"link\": ... }" }}}}]}

data: {"choices": [{"delta": {"content": "..."}}]}

data: {"choices": [{"delta": {"content": "..."}}]}

data: [DONE]
  • It begins with choices[0].delta.tool_calls having one JSON-encoded arguments for each document, mentioning {name, link}.
  • It continues with choices[0].delta.content that has the streaming answer text

Chatbot

Add this code to the IITM BS website:

<script src="https://iitm-bs-chatbot.sanand.workers.dev/chatbot.js" type="module"></script>

See a live demo at https://iitm-bs-chatbot.sanand.workers.dev/.

chatbot.js script will automatically create the chatbot button, the chat app in an iframe, and inject all the necessary CSS for styling.

License

MIT

About

IIT Madras academic program document search using AI-powered semantic embeddings

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 64.8%
  • Python 22.4%
  • HTML 12.8%