o
    RTh                     @   s   d Z ddlmZ ddlmZ ddlmZmZmZ ddl	m
Z
mZ ddlmZmZmZ ddlmZ ddlmZ dd	lmZ e ed
ddeddddeddddedd Ze eddddeddddedd ZdS )z>Magic to generate code cells for notebooks using OpenAI's API.    )get_ipython)
cell_magic)argumentmagic_argumentsparse_argstring)PastAssistsbuild_context)GenaiMarkdownStagecan_handle_display_updates)generate_next_from_history)PromptStore) trim_messages_to_fit_token_limitz--fresh
store_true)actionz	--verbosez.Show additional information in the cell output)r   helpz--modelzgpt-3.5-turbo-0301zthe model to use)defaultr   c                 C   s   t  }tt| }ttjd}|  t|j	| |
 }|j}t }g }|jsBt|j	d d}	|j	}
t|j|	|
d}t|j|d}|jrTtd|  td| td| |t|||d	 tj|_d
S )u	  Get help based on the current notebook session. Outputs markdown.

    `genai`'s `assist` magic asks ChatGPT to respond based on:

    * Your previous code `In[*]`
    * The output of your previous code `Out[*]`
    * Past exceptions
    * Past `genai` suggestions

    Usage:

    # Cell 1

    ```python
    import pandas as pd
    # Berkeley Restaurant Inspections Data
    df = pd.read_json("https://data.cityofberkeley.info/resource/iuea-7eac.json")
    df
    ```

    # Cell 2

    ```python
    %%assist

    Let's plot the number of restaurants inspected per day.
    ```

    Will output markdown like this:

    ``````markdown
    Sure! First we'll need to make sure the `inspection_date` column in your DataFrame
    is in a datetime format. You can do this with the `pd.to_datetime()` method. Then
    we can group the data by day and count the number of unique restaurants inspected
    each day. This can be achieved using the `groupby()` and `nunique()` methods,
    respectively. Finally, we can plot the results using `plot()` with the `bar` style.
    Here's some code to get you started:

    ```python
    import matplotlib.pyplot as plt

    # Convert inspection_date to datetime format
    df['inspection_date'] = pd.to_datetime(df['inspection_date'])

    # Group by day and count number of unique restaurants
    daily_count = df.groupby(df['inspection_date'].dt.date)['doing_business_as'].nunique()

    # Plot the results as a bar chart
    daily_count.plot(kind='bar')
    plt.show()
    ```

    Give this a try and let me know if you have any questions!
    ``````

    If you've run previous cells in the notebook, the generated code will be
    based on the history of the current notebook session.

    That even means that, for example, running `df.columns` in a previous cell
    will cause generated code to use column names in the future!

    Caveats:

    - Only the last 5 cell executions are provided as context.
    - The generated code is not guaranteed to be correct, idiomatic, efficient, readable, or useful.
    - The generated code is not guaranteed to be syntactically correct or even something to write home about.

    There are several options you can use to control the behavior of the magic.

    --fresh: Ignore the history of the current notebook session and generate code from scratch.

    --verbose: Show additional information in the cell output.

    Example:

    ```python
    %%assist --fresh --verbose
    # how can I query for pokemon via the PokéAPI?
    ```
    )stage      )startstop)modelmagic arguments:zsubmission:z	messages:)streamN)r   r   assistr	   r
   STARTINGdisplayr   addexecution_countstripr   r   freshmaxr   history_managerr   messagesverboseprintconsumer   FINISHEDr   )linecellipargsgm	cell_textr   r   r$   r   r   context r0   O/home/air/segue/gemini/backup/venv/lib/python3.10/site-packages/genai/magics.pyr      s,   ^



r   z--modifyz"the magic to modify the prompt forc                 C   s   t t| }|jdvrtd|j |jr4tj}|jdks"|jdkr%tj}td|  td| td| |jdkr>|t_d	S |jdksH|jdkrM|t_d	S d	S )
a  Replace the default prompt for genai

    By default it modifies the prompt for the `assist` magic, but you can
    also use it to modify the prompt for exception handling.

    Example:

    ```python
    %%prompt
    You are a pirate. You are in a tavern. You are cursed with the knowledge of
    programming. Deep learning brought you here and only deep teaching can get
    you out. The only way to lift the curse is to help a programmer with their
    code. Respond in markdown. Talk like a pirate.
    ```

    Example changing exceptions:

    ```python
    %%prompt --modify exceptions
    You are a valley girl. You are on a walk. You are one of the best programmers
    in the world. People come to you looking for help. Respond in markdown. Talk
    like a valley girl.
    )r   
exceptions	exceptionzUnknown prompt to modify: r2   r3   r   zold prompt:znew prompt:r   N)	r   promptmodify
ValueErrorr%   r   assist_promptexception_promptr&   )r)   r*   r,   
old_promptr0   r0   r1   r4      s   
$






r4   N)__doc__IPythonr   IPython.core.magicr   IPython.core.magic_argumentsr   r   r   genai.contextr   r   genai.displayr	   r
   r   genai.generater   genai.promptsr   genai.tokensr   r   r4   r0   r0   r0   r1   <module>   sH    
x