[Python] Generative AI console based [closed]

Discussão em 'Python' iniciado por Stack, Julho 13, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to set up a Streamlit application using LangChain with the Google Gemini API. Below are the steps I followed and the code I'm using, but I'm running into issues (e.g., indentation errors or setup problems). Can someone help me verify the setup process and ensure the code is correct?

    •First create a folder and a subfolder
    •In cmd enter py -3.10 -m venv myvenv
    • Then myvenv\Scripts\activate to activate the environment
    • python -m pip install --upgrade pip
    • pip install --upgrade --quiet langchain-google-genai pillow
    • pip install streamlit
    • pip install python-dotenv
    • then copy the api key from ai for developers google


    in cmd run streamlit run appname.py

    In the .env file GOOGLE_API_KEY=''
    LANGCHAIN_API_KEY=""
    LANGCHAIN_PROJECT="any name"


    The above will be run on the cmd,and the below mentioned part is the code which will connect the LLM to the console with the help of language chain which is a framework in python and JavaScript

    import streamlit as st
    from dotenv import load_dotenv
    from langchain_core.prompts import ChatPromptTemplate
    from langchain_google_genai import ChatGoogleGenerativeAI
    from langchain_core.output_parsers import StrOutputParser

    # Load environment variables
    load_dotenv()

    # Instantiate the model
    llm = ChatGoogleGenerativeAI(
    model="gemini-1.5-flash",
    temperature=0,
    max_tokens=None,
    timeout=None,
    max_retries=2,
    )

    # Create the prompt template
    prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a chatbot"),
    ("human", "Question: {question}")
    ])

    # Streamlit app interface
    st.title('Langchain Demo With Gemini')

    input_text = st.text_input("Enter your question here")

    # Output parser
    output_parser = StrOutputParser()

    # Build the chain
    chain = prompt | llm | output_parser

    # Handle user input
    if input_text:
    st.write(chain.invoke({'question': input_text}))```

    Continue reading...

Compartilhe esta Página