
    Wi                         d dl Z d dlmZ d dlZd dlmZ d dlmZ d dlm	Z	m
Z
mZmZ d dlmZ d dlmZ dZ G d	 d
e      Zd Zy)    N)
completion)tqdm)
csr_matrix)MappingListTupleAny)BaseRepresentation)retry_with_exponential_backoffz
I have a topic that contains the following documents:
[DOCUMENTS]
The topic is described by the following keywords: [KEYWORDS]
Based on the information above, extract a short topic label in the following format:
topic: <topic label>
c                       e Zd ZdZddi ddddfdededz  deeef   d	edz  d
ede	dedz  fdZ
dej                  dedeeeeeef      f   deeeeeef      f   fdZd Zed        Zy)LiteLLMaT	  Using the LiteLLM API to generate topic labels.

    For an overview of models see:
    https://docs.litellm.ai/docs/providers

    Arguments:
        model: Model to use. Defaults to OpenAI's "gpt-3.5-turbo".
        generator_kwargs: Kwargs passed to `litellm.completion`.
        prompt: The prompt to be used in the model. If no prompt is given,
                `self.default_prompt_` is used instead.
                NOTE: Use `"[KEYWORDS]"` and `"[DOCUMENTS]"` in the prompt
                to decide where the keywords and documents need to be
                inserted.
        delay_in_seconds: The delay in seconds between consecutive prompts
                          in order to prevent RateLimitErrors.
        exponential_backoff: Retry requests with a random exponential backoff.
                             A short sleep is used when a rate limit error is hit,
                             then the requests is retried. Increase the sleep length
                             if errors are hit until 10 unsuccesfull requests.
                             If True, overrides `delay_in_seconds`.
        nr_docs: The number of documents to pass to LiteLLM if a prompt
                 with the `["DOCUMENTS"]` tag is used.
        diversity: The diversity of documents to pass to LiteLLM.
                   Accepts values between 0 and 1. A higher
                   values results in passing more diverse documents
                   whereas lower values passes more similar documents.

    Usage:

    To use this, you will need to install the litellm package first:

    `pip install litellm`

    Then, get yourself an API key of any provider (for instance OpenAI) and use it as follows:

    ```python
    import os
    from bertopic.representation import LiteLLM
    from bertopic import BERTopic

    # set ENV variables
    os.environ["OPENAI_API_KEY"] = "your-openai-key"

    # Create your representation model
    representation_model = LiteLLM(model="gpt-3.5-turbo")

    # Use the representation model in BERTopic on top of the default pipeline
    topic_model = BERTopic(representation_model=representation_model)
    ```

    You can also use a custom prompt:

    ```python
    prompt = "I have the following documents: [DOCUMENTS] 
These documents are about the following topic: '"
    representation_model = LiteLLM(model="gpt", prompt=prompt)
    ```
    zgpt-3.5-turboNF   modelpromptgenerator_kwargsdelay_in_secondsexponential_backoffnr_docs	diversityc                 @   || _         |r|nt        | _        t        | _        || _        || _        || _        || _        || _        | j                  j                  d      r|j                  d      | _         | j                  j                  d      r| j                  d= y y )Nr   r   )
r   DEFAULT_PROMPTr   default_prompt_r   r   r   r   r   get)selfr   r   r   r   r   r   r   s           l/home/sietch6/trending-topics-pipeline/venv/lib/python3.12/site-packages/bertopic/representation/_litellm.py__init__zLiteLLM.__init__O   s     
 &fN- 0#6 " 0  $$W-)--g6DJ  $$X.%%h/ /    	documentsc_tf_idftopicsreturnc                 8   |j                  |||d| j                  | j                        \  }}}}i }t        |j	                         |j
                         D ]  \  }}	| j                  |	||      }
| j                  rt        j                  | j                         dddd|
dg}| j                  |d| j                  }| j                  rt        di |}nt        di |}|d   d	   d
   d   j                         j!                  dd      }|dfg||<    |S )aJ  Extract topics.

        Arguments:
            topic_model: A BERTopic model
            documents: All input documents
            c_tf_idf: The topic c-TF-IDF representation
            topics: The candidate topics as calculated with c-TF-IDF

        Returns:
            updated_topics: Updated topic representations
        i  )disablesystemzYou are a helpful assistant.)rolecontentuser)r   messageschoicesr   messager&   ztopic:      )_extract_representative_docsr   r   r   itemsverbose_create_promptr   timesleepr   r   r   chat_completions_with_backoffr   stripreplace)r   topic_modelr   r   r    repr_docs_mappings_updated_topicstopicdocsr   r(   kwargsresponselabels                  r   extract_topicszLiteLLM.extract_topicsg   s9    '2&N&NidllDNN'
#Aq!
  2 8 8 :H[H[D[\ 	1KE4((uf=F $$

4001 ".LMF3H  $zzxY4CXCXYF''8B6B%//Y'*95i@FFHPPQZ\^_E&+QZLN5!%	1( r   c                 `   t        t        ||          }| j                  t        k(  r?| j                  j	                  ddj                  |            }| j                  ||      }|S | j                  }d|v r!|j	                  ddj                  |            }d|v r| j                  ||      }|S )Nz
[KEYWORDS] [DOCUMENTS])nextzipr   r   r6   join_replace_documents)r   r<   r;   r    keywordsr   s         r   r1   zLiteLLM._create_prompt   s    VE]+, ;;.([[((sxx7IJF,,VT:F  [[Fv%chhx6HI&00>r   c                 T    d}|D ]  }|d|d d  dz  } | j                  d|      } | S )Nr+   z-    
rC   )r6   )r   r<   
to_replacedocs       r   rG   zLiteLLM._replace_documents   sD    
 	-CBs4Cyk,,J	-z:r   )__name__
__module____qualname____doc__strr   r	   floatboolintr   pd	DataFramer   r   r   r@   r1   staticmethodrG   r-   r   r   r   r      s    8x %!.0)-$)"&00 d
0 "#s(+	0
  $,0 "0 0 4<00)&(ll)>H)RYZ]_cdijmotjtdu_vZvRw)	d5e,--	.)V&  r   r   c                  ,     t        t              di | S )Nr-   )r   r   )r=   s    r   r4   r4      s$    )  r   )r2   litellmr   pandasrV   r   scipy.sparser   typingr   r   r   r	   bertopic.representation._baser
   bertopic.representation._utilsr   r   r   r4   r-   r   r   <module>r`      s<        # , , < IW  Wtr   