OvidijusParsiunas / deep-chat

Fully customizable AI chatbot component for your website
https://deepchat.dev
MIT License
1.27k stars 175 forks source link

Google Gemini AI connection #113

Open CrisChr opened 4 months ago

OvidijusParsiunas commented 4 months ago

Hi @CrisChr;

Google Gemini is currently not available in EU - which is where I currently live and unfortunately using a VPN for me is not an option as I would run the risk of getting my GCP account locked.

Therefore unfortunately for these reasons I am unable to develop a directConnection property for this service.

If possible, it would be great to collaborate with you or somebody else who has access to its API. I could write the code and somebody could test my changes. After a couple of iterations, this would eventually be complete. Let me know if you could help. Thanks!

havle commented 3 months ago

Hi could you help write the code and then I could test? I could access Gemini but honestly the streaming is a bit hard for me :D (

const history = createReqChatBody(textRequestBody, true, systemPrompt);
  const model = genAI.getGenerativeModel({ model: "gemini-pro"});
  const chat = model.startChat({ 
    history: history });
  const msg = textRequestBody.messages[textRequestBody.messages.length - 1].text || '';
  const result = await chat.sendMessageStream(msg);
import {DeepChatOpenAITextRequestBody} from '../types/deepChatTextRequestBody';
import { GoogleGenerativeAI } from "@google/generative-ai";
import { Content, POSSIBLE_ROLES, Role } from '@google/generative-ai';

export function createReqChatBody(body: DeepChatOpenAITextRequestBody, stream?: boolean, systemPrompt?: string): Content[] {
  const messages = body.messages.slice(0, -1); // Exclude the last message
  const history: Content[] = messages.map((message) => {
    return {
      role: message.role === 'ai' ? "model" : message.role as Role,
      parts: [{ text: message.text || '' }]
    };
  });
  if (systemPrompt) {
    history.unshift({ role: "user", parts: [{ text: systemPrompt }] });
  }
  return history;
}
OvidijusParsiunas commented 3 months ago

Hi @havle.

I am currently working on other small features, but I will try to help you with this as soon as I can. It is however quite difficult for me to develop something that I will not know if it will work and I cannot debug, hence it will be a tedious process and I may be better off waiting for Gemini API to approved in UK. I'll let you know which is better after I get some free time.

To note, when implementing new APIs for directConnection, we usually do it in raw fetch form and not rely on other external dependencies. That way the component size can be kept to a minimum and we will have full control over what is happening when calling out to the service. So if you will attempt to edit our code, it will need to be done that way.

I'll update you on the progress @havle. Thanks again!

havle commented 3 months ago

Thank you @OvidijusParsiunas . I was working on it from a 'Server' perspective (copying and changing the code from OpenAI examples). I'm not technical enough to write the part about streaming :D.

tasdflkjweio commented 2 months ago

No guarantees, but I can try to pick this up in about a month.

OvidijusParsiunas commented 1 month ago

I had just checked the documentation and Gemini API is finally available in UK!!!!! 🎉🎉🎉🎉🎉

I am currently working on other features but will try to jump on this as soon as possible!!!!

duke7able commented 1 month ago

I work on gemini, is this done, looks like a cool project, I could probably add in this feature to start with. @OvidijusParsiunas have you started or should i pick it up and push it in a week or two. If you have started i would not like to pick it up as it would waste your efforts. So let me know

OvidijusParsiunas commented 1 month ago

Hey @duke7able. I have been extremely busy recently so barely had any time to do much on this project. As mentioned in some of the other open issues, I will have much more free time once the summer begins, hence I'll be able to start working on this and other issues. However, if you want to contribute this feature, feel free to work on it and I can provide guidance for it if needed. There are certain patterns to be followed for adding new services to Deep Chat and you can use OpenAI Chat or Cohere Chat implementations to help get you started. Ideally, the Gemini API integration should not require you to change any other existing files, except the DirectConnection file type itself. You can create a fork and use the following guide to run the project locally. Once you feel confident with the API integration or want me to review any of the work you are doing, you can create a Pull Request and I'll be able to help out. Let me know if you need any help. As I mentioned I'll be a lot more free starting the upcoming month.

OvidijusParsiunas commented 1 month ago

Hi @duke7able, I have completed the tasks I had lined up before this. Wanted to check if you have started working on this, if not I will pick it up. Thanks!

OvidijusParsiunas commented 3 weeks ago

Since there has been no further communication, I will now begin the integration work for Gemini API.