Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed streaming support using callback handler for Google genai #15459

Closed

Conversation

hasansustcse13
Copy link
Contributor

@efriis efriis added the partner label Jan 3, 2024
@efriis efriis self-assigned this Jan 3, 2024
@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Jan 3, 2024
Copy link

vercel bot commented Jan 3, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
langchain ⬜️ Ignored (Inspect) Visit Preview Jan 4, 2024 4:26am

@dosubot dosubot bot added Ɑ: models Related to LLMs or chat model modules 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature labels Jan 3, 2024
@hasansustcse13
Copy link
Contributor Author

@efriis Could you please look into this PR? Our current project has a large dependency on it.

@efriis
Copy link
Member

efriis commented Jan 7, 2024

Hey @hasansustcse13 ! This isn't the interface to follow anymore. Instead, you should stream using .stream() instead!

Let me know if you have any other questions.

Erick

@efriis efriis closed this Jan 7, 2024
@hasansustcse13
Copy link
Contributor Author

hasansustcse13 commented Jan 7, 2024

@efriis Streaming doesn't work if I don't use LangChain Expression Language (LCEL). In our project, we have used streaming using a Callback handler(on_llm_new_token) and different kinds of built-in chains. As Gemini is not implanted the same as OpenAI, it does not work with a callback handler (on_llm_new_token). To need to support streaming in Gemini along with other LLM we have to convert all the code to LCEL. I believe this will be faced by other users also. That's why I have made the Change in the PR. To understand the depth please see the Attached bug in the description. Is there any other way to support streaming without LCEL and with minimal changes?

from langchain.chains import LLMChain
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import PromptTemplate
from langchain_google_genai import ChatGoogleGenerativeAI

prompt_template = "Tell me details about the Company {name} with 4 bullet point?"


def gemini_stream():
    llm = ChatGoogleGenerativeAI(model="gemini-pro", google_api_key='AIzaSyliRCeTRontPQ')
    llm_chain = LLMChain(llm=llm, prompt=PromptTemplate.from_template(prompt_template), verbose=True)
    for content in llm_chain.stream({'name': "Google"}):
        print(content)


def gemini_stream_with_lang_expression():
    llm = ChatGoogleGenerativeAI(model="gemini-pro", google_api_key='AIzaSyCuQ')
    prompt = PromptTemplate.from_template(prompt_template)
    chain = prompt | llm | StrOutputParser()
    for content in chain.stream({'name': "Google"}):
        print(content)


if __name__ == '__main__':
    # This is not working though I have use .stream() method. It gives the output after finising the completetion completely.
    gemini_stream()

    # This is working
    # gemini_stream_with_lang_expression()

@pseudotensor
Copy link

pseudotensor commented Feb 7, 2024

@efriis I also agree with @hasansustcse13 that you are confused.

One does not just invoke llm and a .stream() directly, but one can pass an llm with callback into something like load_qa_chain, then streaming is broken for gemini but not other models.

Gemini wasn't always broken, it started with the recent changes that improved exception handling.

@atuncer
Copy link

atuncer commented Apr 17, 2024

Is there a version that I can import via pip

hangingman added a commit to hangingman/my-competitive-pg that referenced this pull request Jun 16, 2024
hangingman added a commit to hangingman/my-competitive-pg that referenced this pull request Jun 21, 2024
* Gemini導入
* GeminiとOpenAIの呼び出しをfactoryで実装
* stream形式とLCELを両立させた書き方を実装
* langchain-ai/langchain#15459 (comment)
* prompt, few-shot prompt修正
* codeのフォーマット
@RushikeshKothawade07
Copy link

how to use map reduce chain type with gemini models using chatgooglegenerativeai and also get streaming responses at the same time using custom callback handler where I want token level streaming by appending in a queue

@efriis
Copy link
Member

efriis commented Sep 8, 2024

try ChatGoogleGenerativeAI(...).bind(stream=True)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature Ɑ: models Related to LLMs or chat model modules partner size:S This PR changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants