Skip to content

Forum in maintenance, we will back soon 🙂

create AI tool in W...
 
Notifications
Clear all

create AI tool in WP using Gemini API

34 Posts
4 Users
10 Reactions
70 Views
Hasan Aboul Hasan
(@admin)
Posts: 1276
Member Admin
 

@sonu-choudhary can you share the working code you used with Gemini

 
Posted : 02/03/2024 11:29 am
SSAdvisor
(@ssadvisor)
Posts: 1139
Noble Member
 

@sonu-choudhary but your code has a space in the URL that doesn't belong when you are using curl to make a call to the API.

Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack

 
Posted : 02/03/2024 11:54 am
(@sonu-choudhary)
Posts: 26
Eminent Member Customer
Topic starter
 

@admin sure im creating a streamlit apps using google gemini api but im stuck at wordpress what code you want ..?

 
Posted : 02/03/2024 1:22 pm
Hasan Aboul Hasan
(@admin)
Posts: 1276
Member Admin
 

@sonu-choudhary the python function you used to call Gemeni API

 
Posted : 02/03/2024 2:04 pm
(@sonu-choudhary)
Posts: 26
Eminent Member Customer
Topic starter
 

@admin

import os
import textwrap
from IPython.display import display
from IPython.display import Markdown
import streamlit as st
import google.generativeai as genai

from dotenv import load_dotenv

load_dotenv()  # take environment variables from .env.



# Initialize Gemini-Pro 
genai.configure(api_key=os.getenv("GOOGLE_GEMINI_KEY"))
model = genai.GenerativeModel('gemini-pro')

# Gemini uses 'model' for assistant; Streamlit uses 'assistant'
def role_to_streamlit(role):
  if role == "model":
    return "assistant"
  else:
    return role

# Add a Gemini Chat history object to Streamlit session state
if "chat" not in st.session_state:
    st.session_state.chat = model.start_chat(history = [])

# Display Form Title
st.title("Chat with Google Gemini-Pro!")

# Display chat messages from history above current input box
for message in st.session_state.chat.history:
    with st.chat_message(role_to_streamlit(message.role)):
        st.markdown(message.parts[0].text)

# Accept user's next message, add to context, resubmit context to Gemini
if prompt := st.chat_input("I possess a well of knowledge. What would you like to know?"):
    # Display user's last message
    st.chat_message("user").markdown(prompt)
    
    # Send user entry to Gemini and read the response
    response = st.session_state.chat.send_message(prompt) 
    
    # Display last 
    with st.chat_message("assistant"):
        st.markdown(response.text)
 
Posted : 02/03/2024 5:33 pm
SSAdvisor
(@ssadvisor)
Posts: 1139
Noble Member
 

@sonu-choudhary I would suggest getting rid of the IPython imports. I don't see that you use these in the code.

Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack

 
Posted : 02/03/2024 6:00 pm
Hasan Aboul Hasan
(@admin)
Posts: 1276
Member Admin
 

@sonu-choudhary Thank you, I will work on a WordPress code for this week

 
Posted : 02/04/2024 7:37 am
(@sonu-choudhary)
Posts: 26
Eminent Member Customer
Topic starter
 

@admin yes please share WordPress code im Waiting for ur Blog or Video about it

 
Posted : 02/05/2024 7:54 am
Hasan Aboul Hasan
(@admin)
Posts: 1276
Member Admin
 
function gemini_generate_text() {

    $topic = $_POST['topic'];
    $prompt = "Generate 5 youtube titles for a video about " . $topic;

 
    $api_key = 'AIzaSyDw4xb0';
    $api_url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=' . $api_key ;


    // Headers for the Gemini API
    $headers = [
        'Content-Type' => 'application/json'

    ];

    // Body for the Gemini API
    $body = [
        'contents' => [
            [
                'parts' => [
                    ['text' => $prompt]
                ]
            ]
        ]
    ];

    // Args for the WordPress HTTP API
    $args = [
        'method' => 'POST',
        'headers' => $headers,
        'body' => json_encode($body),
        'timeout' => 120
    ];

    // Send the request
    $response = wp_remote_request($api_url, $args);
    wp_send_json_success($response);

  

    // Always die in functions echoing AJAX content
    wp_die();
}

add_action('wp_ajax_gemini_generate_text', 'gemini_generate_text');
add_action('wp_ajax_nopriv_gemini_generate_text', 'gemini_generate_text');

here is a sample working example

 

 
Posted : 02/05/2024 2:03 pm
(@sonu-choudhary)
Posts: 26
Eminent Member Customer
Topic starter
 

@admin okk and as per your create ai tools in 5 minutes in wp blog other code is same right html and js one ...?

 
Posted : 02/05/2024 6:22 pm
(@sonu-choudhary)
Posts: 26
Eminent Member Customer
Topic starter
 

@admin or can you create one more blog about this how create tool step by step its being helpful for us

 
Posted : 02/05/2024 6:41 pm
Hasan Aboul Hasan
(@admin)
Posts: 1276
Member Admin
 

@sonu-choudhary this code is a generic function to call the gemeni APi and get a response, it is not exactly the same as the openAI code. you need to tweek a little bit to fit your needs. I will try to create a tutorial about it

 
Posted : 02/06/2024 9:20 am
(@sonu-choudhary)
Posts: 26
Eminent Member Customer
Topic starter
 

@admin yes do it quickly im waiting for that

 
Posted : 02/06/2024 5:35 pm
Hasan Aboul Hasan
(@admin)
Posts: 1276
Member Admin
 

@sonu-choudhary Sorry, friend, I can't promise to do it quickly. I will add it to my schedule.

 
Posted : 02/07/2024 7:27 am
(@sonu-choudhary)
Posts: 26
Eminent Member Customer
Topic starter
 

@admin sure bro

 
Posted : 02/07/2024 8:43 am
Page 2 / 3
Share:
[the_ad_group id="312"]