### Install Cohere Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/representation/llm.md Install the Cohere package to use their API for topic representation. Ensure you have a Cohere API key. ```bash pip install cohere ``` -------------------------------- ### Install Required Packages Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/representation/llm.md Install `tiktoken` and `openai` for tokenization and OpenAI API integration. ```bash pip install tiktoken openai ``` -------------------------------- ### Install LangChain and OpenAI Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/representation/llm.md Install the necessary packages for using LangChain with OpenAI. Ensure you have an OpenAI API key. ```bash pip install langchain, openai ``` -------------------------------- ### Install BERTopic for Image Topic Modeling Source: https://github.com/maartengr/bertopic/blob/master/README.md Install BERTopic with the 'vision' extra to enable topic modeling with images. ```bash # Topic modeling with images pip install bertopic[vision] ``` -------------------------------- ### Install Development Requirements Source: https://github.com/maartengr/bertopic/blob/master/CONTRIBUTING.md Install the necessary packages for development, including testing and documentation tools, using pip. ```bash pip install .[dev] ``` -------------------------------- ### Install BERTopic Source: https://github.com/maartengr/bertopic/blob/master/README.md Install BERTopic using pip. For multilingual support, install with the `all-languages` extra. ```bash pip install bertopic ``` ```bash pip install bertopic[all-languages] ``` -------------------------------- ### Install BERTopic with uv Source: https://github.com/maartengr/bertopic/blob/master/README.md Use uv to add the bertopic package to your project. This is the recommended installation method. ```bash uv add bertopic ``` -------------------------------- ### Install llama-cpp-python Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/representation/llm.md Provides commands for installing the llama-cpp-python library, including options for hardware acceleration like CUDA. ```bash pip install llama-cpp-python ``` ```bash CMAKE_ARGS="-DLLAMA_CUBLAS=on" FORCE_CMAKE=1 pip install llama-cpp-python ``` -------------------------------- ### Install OpenAI Library Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/representation/llm.md Provides the command to install the OpenAI Python client library, which is necessary for using OpenAI's API with BERTopic. ```bash pip install openai ``` -------------------------------- ### Install LiteLLM Package Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/representation/llm.md Install the 'litellm' package to simplify connections to various LLM providers, including OpenAI. ```bash pip install litellm ``` -------------------------------- ### Rendered Prompt Example Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/representation/llm.md This is an example of how the prompt with placeholders is rendered after BERTopic inserts the actual documents and keywords for a specific topic. ```python """ I have a topic that contains the following documents: - Our videos are also made possible by your support on patreon.co. - If you want to help us make more videos, you can do so on patreon.com or get one of our posters from our shop. - If you want to help us make more videos, you can do so there. - And if you want to support us in our endeavor to survive in the world of online video, and make more videos, you can do so on patreon.com. The topic is described by the following keywords: videos video you our support want this us channel patreon make on we if facebook to patreoncom can for and more watch Based on the above information, can you give a short label of the topic? """ ``` -------------------------------- ### Install cuML and BERTopic Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/clustering/clustering.md Install cuML first, then BERTopic. Installing both in a single command can fail due to pip resolver limitations with CUDA runtime dependencies. cuML is pre-installed on Google Colab. ```bash !pip install cuml-cu12 !pip install bertopic ``` ```bash !pip install cuml-cu13 !pip install bertopic ``` -------------------------------- ### Topic Modeling with Topic Modeling Examples Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/visualization/datamapplot.html Provides a collection of examples demonstrating various BERTopic functionalities. ```python from bertopic import BERTopic from sentence_transformers import SentenceTransformer docs = [ "This is the first document.", "This document is the second document.", "And this is the third one.", "Is this the first document?" ] # Example 1: Basic Usage topic_model_1 = BERTopic(language="english", verbose=True) topics_1, probs_1 = topic_model_1.fit_transform(docs) print("--- Basic Usage ---") print(topic_model_1.get_topic_info()) # Example 2: Custom Embeddings embedding_model = SentenceTransformer("all-MiniLM-L6-v2") topic_model_2 = BERTopic(embedding_model=embedding_model, verbose=True) topics_2, probs_2 = topic_model_2.fit_transform(docs) print("\n--- Custom Embeddings ---") print(topic_model_2.get_topic_info()) # Example 3: Saving and Loading topic_model_3 = BERTopic(verbose=True) topics_3, probs_3 = topic_model_3.fit_transform(docs) topic_model_3.save("my_model") loaded_model = BERTopic.load("my_model") print("\n--- Saving and Loading ---") print(loaded_model.get_topic_info()) ``` -------------------------------- ### Full Topic Tree Example Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/hierarchicaltopics/hierarchicaltopics.md An example of a detailed, text-based representation of a topic hierarchy, showing nested topics and their associated topic numbers. ```bash .\n├─people_armenian_said_god_armenians\n│ ├─god_jesus_jehovah_lord_christ\n│ │ ├─god_jesus_jehovah_lord_christ\n│ │ │ ├─jehovah_lord_mormon_mcconkie_god\n│ │ │ │ ├─■──ra_satan_thou_god_lucifer ── Topic: 94\n│ │ │ │ └─■──jehovah_lord_mormon_mcconkie_unto ── Topic: 78\n│ │ │ └─jesus_mary_god_hell_sin\n│ │ │ ├─jesus_hell_god_eternal_heaven\n│ │ │ │ ├─hell_jesus_eternal_god_heaven\n│ │ │ │ │ ├─■──jesus_tomb_disciples_resurrection_john ── Topic: 69\n│ │ │ │ │ └─■──hell_eternal_god_jesus_heaven ── Topic: 53\n│ │ │ │ └─■──aaron_baptism_sin_law_god ── Topic: 89\n│ │ │ └─■──mary_sin_maria_priest_conception ── Topic: 56\n│ │ └─■──marriage_married_marry_ceremony_marriages ── Topic: 110\n│ └─people_armenian_armenians_said_mr\n│ ├─people_armenian_armenians_said_israel\n│ │ ├─god_homosexual_homosexuality_atheists_sex\n│ │ │ ├─homosexual_homosexuality_sex_gay_homosexuals\n│ │ │ │ ├─■──kinsey_sex_gay_men_sexual ── Topic: 44\n│ │ │ │ └─homosexuality_homosexual_sin_homosexuals_gay\n│ │ │ │ ├─■──gay_homosexual_homosexuals_sexual_cramer ── Topic: 50\n│ │ │ │ └─■──homosexuality_homosexual_sin_paul_sex ── Topic: 27\n│ │ │ └─god_atheists_atheism_moral_atheist\n│ │ │ ├─islam_quran_judas_islamic_book\n│ │ │ │ ├─■──jim_context_challenges_articles_quote ── Topic: 36\n│ │ │ │ └─islam_quran_judas_islamic_book\n│ │ │ │ ├─■──islam_quran_islamic_rushdie_muslims ── Topic: 31\n│ │ │ │ └─■──judas_scripture_bible_books_greek ── Topic: 33\n│ │ │ └─atheists_atheism_god_moral_atheist\n│ │ │ ├─atheists_atheism_god_atheist_argument\n│ │ │ │ ├─■──atheists_atheism_god_atheist_argument ── Topic: 21\n│ │ │ │ └─■──br_god_exist_genetic_existence ── Topic: 124\n│ │ │ └─■──moral_morality_objective_immoral_morals ── Topic: 29\n│ │ └─armenian_armenians_people_israel_said\n│ │ ├─armenian_armenians_israel_people_jews\n│ │ │ ├─tax_rights_government_income_taxes\n│ │ │ │ ├─■──rights_right_slavery_slaves_residence ── Topic: 106\n│ │ │ │ └─tax_government_taxes_income_libertarians\n│ │ │ │ ├─■──government_libertarians_libertarian_regulation_party ── Topic: 58\n│ │ │ │ └─■──tax_taxes_income_billion_deficit ── Topic: 41\n│ │ │ └─armenian_armenians_israel_people_jews\n│ │ │ ├─gun_guns_militia_firearms_amendment\n│ │ │ │ ├─■──blacks_penalty_death_cruel_punishment ── Topic: 55\n│ │ │ │ └─■──gun_guns_militia_firearms_amendment ── Topic: 7\n│ │ │ └─armenian_armenians_turkish_armenia_azerbaijan\n│ │ │ ├─■──israel_israeli_jews_arab_jewish ── Topic: 4\n│ │ │ └─■──armenian_armenians_turkish_armenia_azerbaijan ── Topic: 15\n│ │ └─stephanopoulos_president_mr_myers_ms ``` -------------------------------- ### Install ctransformers and transformers Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/representation/llm.md Install the necessary packages for using quantized LLMs with CUDA support. Ensure transformers is up-to-date. ```python pip install ctransformers[cuda] pip install --upgrade git+https://github.com/huggingface/transformers ``` -------------------------------- ### BERTopic with Topic Modeling and Visualization Example Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/visualization/datamapplot.html A complete example of training a BERTopic model and visualizing the resulting topics and their associated terms. ```python from bertopic import BERTopic topic_model = BERTopic() topics, probs = topic_model.fit_transform(documents) topic_model.visualize_topics().show() topic_model.visualize_barchart(top_n_words=5).show() ``` -------------------------------- ### BERTopic with Topic Modeling and Topic Prioritization Example Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/visualization/datamapplot.html Guiding the topic modeling process by specifying words that should be prioritized in certain topics. ```python from bertopic import BERTopic topic_model = BERTopic() # Prioritize documents containing 'technology' topics, probs = topic_model.fit_transform(documents, topic_priorities=['technology']) ``` -------------------------------- ### Initialize Background Rendering Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/visualization/viz.html Sets up the background rendering buffer and VAO. This function should be called once during initialization. ```javascript e.exports=function(t){for(var e=[],r=[],s=0,l=0;l<3;++l)for(var c=(l+1)%3,u=(l+2)%3,f=[0,0,0],h=[0,0,0],p=-1;p<=1;p+=2){r.push(s,s+2,s+1,s+1,s+2,s+3),f[l]=p,h[l]=p;for(var d=-1;d<=1;d+=2){f[c]=d;for(var g=-1;g<=1;g+=2)f[u]=g,e.push(f[0],f[1],f[2],h[0],h[1],h[2]),s+=1}}var v=n(t,new Float32Array(e)),y=n(t,new Uint16Array(r),t.ELEMENT_ARRAY_BUFFER),x=i(t, [{buffer:v,type:t.FLOAT,size:3,offset:0,stride:24},{buffer:v,type:t.FLOAT,size:3,offset:12,stride:24}],y),b=a(t);return b.attributes.position.location=0,b.attributes.normal.location=1,new o(t,v,x,b)};var n=t("gl-buffer"),i=t("gl-vao"),a=t("./shaders").bg;function o(t,e,r,n){this.gl=t,this.buffer=e,this.vao=r,this.shader=n} ``` -------------------------------- ### Build Documentation Source: https://github.com/maartengr/bertopic/blob/master/CONTRIBUTING.md Generate the project's documentation locally. This command is optional but recommended for checking documentation changes. ```bash make docs ``` -------------------------------- ### Basic BERTopic Usage Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/visualization/datamapplot.html Create and train a BERTopic model with default settings. This is a quick way to get started with topic modeling. ```python from bertopic import BERTopic docs = [ "This is the first document.", "This document is the second document.", "And this is the third one.", "Is this the first document?" ] topic_model = BERTopic() topics, probs = topic_model.fit_transform(docs) ``` -------------------------------- ### Basic BERTopic Usage Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/visualization/datamapplot.html A simple example of how to use BERTopic to model topics from a list of documents. Ensure you have a list of documents to start with. ```python from bertopic import BERTopic documents = [ "This topic is about apples and oranges.", "This topic is about basketball and soccer.", "This topic is about apples and bananas.", "This topic is about soccer and tennis.", "This topic is about oranges and grapes.", "This topic is about tennis and basketball." ] topic_model = BERTopic() topics, probs = topic_model.fit_transform(documents) print(topic_model.get_topic_info()) ``` -------------------------------- ### BERTopic with Model2Vec Embeddings Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/tips_and_tricks/tips_and_tricks.md Example of initializing BERTopic with Model2Vec for embeddings, suitable for environments where PyTorch or SentenceTransformers are not desired or available. Requires model2vec to be installed. ```python from bertopic import BERTopic from model2vec import StaticModel # Model2Vec embedding_model = StaticModel.from_pretrained("minishlab/potion-base-8M") ``` -------------------------------- ### Define Prompt for Text Generation Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/representation/llm.md Constructs a prompt template for guiding a language model to generate topic labels. It includes system instructions, an example, and the main query structure. ```python example_prompt = """ I have a topic that contains the following documents: - Traditional diets in most cultures were primarily plant-based with a little meat on top, but with the rise of industrial style meat production and factory farming, meat has become a staple food. - Meat, but especially beef, is the word food in terms of emissions. - Eating meat doesn't make you a bad person, not eating meat doesn't make you a good one. The topic is described by the following keywords: 'meat, beef, eat, eating, emissions, steak, food, health, processed, chicken'. Based on the information about the topic above, please create a short label of this topic. Make sure you to only return the label and nothing more. [/INST] Environmental impacts of eating meat """ # Our main prompt with documents ([DOCUMENTS]) and keywords ([KEYWORDS]) tags main_prompt = """ [INST] I have a topic that contains the following documents: [DOCUMENTS] The topic is described by the following keywords: '[KEYWORDS]'. Based on the information about the topic above, please create a short label of this topic. Make sure you to only return the label and nothing more. [/INST] " " prompt = system_prompt + example_prompt + main_prompt ``` -------------------------------- ### BERTopic with Topic Modeling and Loading Example 3 Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/visualization/datamapplot.html Loading a model from a custom path. ```python from bertopic import BERTopic loaded_model = BERTopic.load("/path/to/my/bertopic_model") new_topics, new_probs = loaded_model.transform(new_documents) ``` -------------------------------- ### Topic Modeling with Document Lengths Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/visualization/datamapplot.html BERTopic can handle documents of varying lengths. This example shows a basic setup where document lengths are not explicitly managed, relying on the default behavior. ```python from bertopic import BERTopic documents = [ "Short doc.", "This is a much longer document that contains more information and potentially more topics.", "Another short one." ] topic_model = BERTopic() topics, probs = topic_model.fit_transform(documents) print(topic_model.get_topic_info()) ``` -------------------------------- ### Retrieve Detailed Topic Terms Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/vectorizers/vectorizers.md Get a list of terms and their associated scores for a specific topic. This example shows topic 8, demonstrating how ngram_range can combine words like 'tear gas'. ```python >>> topic_model.get_topic(8) [('fbi', 0.019637149205975653), ('koresh', 0.019054514637064403), ('gas', 0.014156057632897179), ('compound', 0.012381224868591681), ('batf', 0.010349992314076047), ('children', 0.009336408916322387), ('tear gas', 0.008941747802855279), ('tear', 0.008446786597564537), ('davidians', 0.007911119583253022), ('started', 0.007398687505638955)] ``` -------------------------------- ### 3D Camera Setup Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/visualization/viz.html Initializes camera parameters for 3D rendering, including center, up direction, right direction, radius, theta, and phi. ```javascript e.exports=function(t){var e=(t=t||{}).center||[0,0,0],r=t.up||[0,1,0],n=t.right||f(r),i=t.radius||1,a=t.theta||0,u=t.phi||0;if(e=[].slice.call(e,0,3),r=[].s ``` -------------------------------- ### Visualizing Topics with c-TF-IDF Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/visualization/datamapplot.html This example demonstrates how to visualize topics using the c-TF-IDF method. It requires the `get_document_info` method to get document-topic assignments and then uses `visualize_topics` to generate the visualization. This helps in understanding topic relationships. ```python from bertopic import BERTopic docs = [ "This is the first document.", "This document is the second document.", "And this is the third one.", "Is this the first document?" ] topic_model = BERTopic(verbose=True) topics, probs = topic_model.fit_transform(docs) # Visualize topics fig = topic_model.visualize_topics() fig.show() # Visualize documents fig = topic_model.visualize_documents(docs) fig.show() ``` -------------------------------- ### Initialize Bertopic Framework Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/visualization/viz.html Initializes the Bertopic framework with provided configuration. ```javascript ash={},this.layers={},this.basePaths={},this.dataPaths={},this.dataPoints={},this.clipDef=null,this.clipRect=null,this.bgRect=null,this.makeFramework()}t("./projections")(n);var T=w.prototype; ``` -------------------------------- ### Example Prompt with Placeholders Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/representation/llm.md This prompt uses `[DOCUMENTS]` and `[KEYWORDS]` tags, which are placeholders for topic-specific information that will be dynamically inserted by BERTopic when using LLMs for representation. ```python prompt = """ I have topic that contains the following documents: \n[DOCUMENTS] The topic is described by the following keywords: [KEYWORDS] Based on the above information, can you give a short label of the topic? """ ``` -------------------------------- ### Install BERTopic with pip Source: https://github.com/maartengr/bertopic/blob/master/README.md Use pip to install the bertopic package. This is a standard installation method. ```bash pip install bertopic ``` -------------------------------- ### Install BERTopic Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/quickstart/quickstart.md Install BERTopic using pip. Additional dependencies can be installed based on the desired embedding backend or for vision-based topic modeling. ```bash pip install bertopic ``` ```bash # Choose an embedding backend pip install bertopic[flair, gensim, spacy, use] ``` ```bash # Topic modeling with images pip install bertopic[vision] ``` -------------------------------- ### Initialize MultiModalBackend for Image Embeddings Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/multimodal/multimodal.md Sets up the MultiModalBackend using a CLIP model for generating image embeddings. Adjust batch_size for performance. ```python from bertopic.backend import MultiModalBackend # Image embedding model embedding_model = MultiModalBackend('clip-ViT-B-32', batch_size=32) ``` -------------------------------- ### Radial Guide Interaction Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/visualization/viz.html Implements mouse interaction for a radial guide. Updates the guide's radius and displays tooltip information based on mouse movement. ```javascript var ht=ot.select("circle").style({stroke:"grey",fill:"none"});R.on("mousemove.radial-guide",(function(t,e){var n=o.util.getMousePos(Y).radius;ht.attr({r:n}).style({opacity:.5}),at=r.invert(o.util.getMousePos(Y).radius);var i=o.util.convertToCartesian(n,h.radialAxis.orientation);ct.text(o.util.round(at)).move(\[i[0]+\_[0],i[1]+\_[1]\])})).on("mouseout.radial-guide",(function(t,e){ht.style({opacity:0}),ut.hide(),lt.hide(),ct.hide()})),t.selectAll(".geometry-group .mark").on("mouseover.tooltip",(function(e,r){var i=n.select(this),a=this.style.fill,s="black",l=this.style.opacity||1;if(i.attr({"data-opacity":l}),a&&"none"!==a){i.attr({"data-fill":a}),s=n.hsl(a).darker().toString(),i.style({fill:s,opacity:1});var c={t:o.util.round(e[0]),r:o.util.round(e[1])};k&&(c.t=w[e[0]]);var u="t: "+c.t+", r: "+c.r,f=this.getBoundingClientRect(),h=t.node().getBoundingClientRect(),p=\[f.left+f.width/2-U[0]-h ``` -------------------------------- ### Choropleth Mapbox Trace Setup Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/visualization/viz.html Sets up a choropleth mapbox trace, handling data sources and layer creation. It manages the lifecycle of mapbox layers and sources. ```javascript "use strict"; var n=t("./convert").convert,i=t("./convert").convertOnSelect,a=t("../../plots/mapbox/constants").traceLayerPrefix; function o(t,e){this.type="choroplethmapbox",this.subplot=t,this.uid=e,this.sourceId="source-"+e,this.layerList=["fill",a+e+"-fill"],["line",a+e+"-line"]],this.below=null} var s=o.prototype;s.update=function(t){this._update(n(t))},s.updateOnSelect=function(t){this._update(i(t))},s._update=function(t){var e=this.subplot,r=this.layerList,n=e.belowLookup["trace-"+this.uid];e.map.getSource(this.sourceId).setData(t.geojson),n!==this.below&&(this._removeLayers(),this._addLayers(t,n),this.below=n);for(var i=0;i=0;r--)t.removeLayer(e[r][1])},s.dispose=function(){var t=this.subplot.map;this._removeLayers(),t.removeSource(this.sourceId)},e.exports=function(t,e){var r=e[0].trace,i=new o(t,r.uid),a=i.sourceId,s=n(e),l=i.below=t.belowLookup["trace-"+r.uid];return t.map.addSource(a,{type:"geojson",data:s.geojson}),i._addLayers(s,l),e[0].trace._glTrace=i,i}} ``` -------------------------------- ### Angular Guide Interaction Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/visualization/viz.html Implements mouse interaction for an angular guide. Updates the guide's position and displays tooltip information based on mouse movement. ```javascript if(!k){var ft=ot.select("line").attr({x1:0,y1:0,y2:0}).style({stroke:"grey","pointer-events":"none"});R.on("mousemove.angular-guide",(function(t,e){var r=o.util.getMousePos(Y).angle;ft.attr({x2:-x,transform:"rotate("+r+")"}).style({opacity:.5});var n=(r+180+360-h.orientation)%360;it=s.invert(n);var i=o.util.convertToCartesian(x+12,r+180);lt.text(o.util.round(it)).move(\[i[0]+\_[0],i[1]+\_[1]\])})).on("mouseout.angular-guide",(function(t,e){ot.select("line").style({opacity:0})}))} ``` -------------------------------- ### Install Additional Dependencies for Lightweight BERTopic Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/tips_and_tricks/tips_and_tricks.md After a lightweight BERTopic installation, install specific libraries like model2vec and umap-learn if you intend to use them for embedding generation or dimensionality reduction. ```bash pip install model2vec umap-learn ``` -------------------------------- ### Initialize LineBucket Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/visualization/viz.html Initializes a new LineBucket instance with specified options. It sets up internal structures for storing layout and paint properties, and prepares for vertex and index buffer management. ```javascript Cs.prototype.destroy=function(){this.layoutVertexBuffer&&(this.layoutVertexBuffer.destroy(),this.indexBuffer.destroy(),this.programConfigurations.destroy(),this.segments.destroy())},Cs.prototype.addFeature=function(t,e,r,n,i){for(var a=this.layers\[0\]\.layout,o=a.get("line-join").evaluate(t,{})),s=a.get("line-cap"),l=a.get("line-miter-limit"),c=a.get("line-round-limit"),u=0,f=e;u=2&&t\[l-1\].equals(t\[l-2\]);)l--;for(var c=0;c desired label) few_shot_examples = [ {"topic_words": "...', 'label": "Category A"}, # ... more examples ... ] # Use an LLM with few-shot prompting to label topics # ... (LLM integration logic) ... # Placeholder return: return y # Return original labels for now topic_model = BERTopic() docs = ["This is the first document.", "This document is the second document.", "And this is the third one.", "Is this the first document?"] topics, probs = topic_model.fit_transform(docs) ``` -------------------------------- ### Initialize BERTopic with OpenAI Representation Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/representation/llm.md Demonstrates how to configure BERTopic to use OpenAI's API for topic representation. Requires the 'openai' library to be installed and an API key to be set. ```python import openai from bertopic.representation import OpenAI from bertopic import BERTopic ``` -------------------------------- ### SymbolInstanceArray Get Method Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/quickstart/viz.html Provides a method to get a SymbolInstance from the array at a given index. ```javascript e.prototype.get=function(t){return new Ki(this,t)} ``` -------------------------------- ### Modular Arithmetic Setup Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/quickstart/viz.html Sets up a modular arithmetic context. This is essential for performing calculations within a specific modulus, commonly used in cryptography. ```javascript function w(t){if("string"==typeof t){var e=a._prime(t);this.m=e.p,this.prime=e}else n(t.gtn(1),"modulus must be greater than 1"),this.m=t,this.prime=null} ``` -------------------------------- ### FeatureIndexArray Get Method Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/quickstart/viz.html Provides a method to get a FeatureIndex object from the array at a given index. ```javascript e.prototype.get=function(t){return new ea(this,t)} ``` -------------------------------- ### BERTopic with Topic Modeling and Loading Example 5 Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/visualization/datamapplot.html Loading a model with a specific filename. ```python from bertopic import BERTopic loaded_model = BERTopic.load("my_model_v2.bertopic") new_topics, new_probs = loaded_model.transform(new_documents) ``` -------------------------------- ### BERTopic with Topic Modeling and Loading Example 2 Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/visualization/datamapplot.html Loading a model saved with a different extension. ```python from bertopic import BERTopic loaded_model = BERTopic.load("bertopic_model.joblib") new_topics, new_probs = loaded_model.transform(new_documents) ``` -------------------------------- ### Get Topic Representation Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/quickstart/viz.html Gets the most important words for a specific topic. Use this to interpret the meaning of each topic. ```python model.get_topic(topic_id=0) ``` -------------------------------- ### Get Last Child (j) Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/visualization/viz.html Accessor function to get the last child of a node, or the node itself if it has no children. ```javascript function j(t){var e=t.children;return e?e[e.length-1]:t.t} ``` -------------------------------- ### Get First Child (N) Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/visualization/viz.html Accessor function to get the first child of a node, or the node itself if it has no children. ```javascript function N(t){var e=t.children;return e?e[0]:t.t} ``` -------------------------------- ### Initialize BERTopic with LlamaCPP and Custom Parameters Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/representation/llm.md Shows how to initialize BERTopic with LlamaCPP, allowing for fine-grained control over LLM parameters such as GPU layers, context size, and stop tokens. ```python from bertopic import BERTopic from bertopic.representation import LlamaCPP from llama_cpp import Llama # Use llama.cpp to load in a 4-bit quantized version of Zephyr 7B Alpha llm = Llama(model_path="zephyr-7b-alpha.Q4_K_M.gguf", n_gpu_layers=-1, n_ctx=4096, stop="Q:") representation_model = LlamaCPP(llm) # Create our BERTopic model topic_model = BERTopic(representation_model=representation_model, verbose=True) ``` -------------------------------- ### Initialize Program Configurations Source: https://github.com/maartengr/bertopic/blob/master/docs/getting_started/quickstart/viz.html Initializes program configurations for different layer types. It maps layer IDs to their respective binder configurations. ```javascript var Ia=function(t,e,r,n){void 0===n&&(n=function(){return!0}),this.programConfigurations={};for(var i=0,a=e;i