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

Streaming responses from OpenAI and GPT4All #221

Merged
merged 36 commits into from
Jul 6, 2023
Merged

Conversation

raulraja
Copy link
Contributor

@raulraja raulraja commented Jul 3, 2023

These changes introduce the capability of streaming in chat responses. It adds the following changes:

  • A new function, createChatCompletions, has been added to the Chat interface, which allows creating chat completions based on a ChatCompletionRequest. This function returns a Flow<ChatCompletionChunk> representing the generated chat completions.
  • The Chat interface now includes two overloaded versions of the promptStreaming function. These functions enable streaming by returning a Flow<String> that emits the generated chat responses as they become available.
  • The implementation of createChatCompletions in GPT4All utilizes a Flow and channels to enable streaming of chat completions.
  • The MockOpenAIClient has been updated to throw a NotImplementedError for chatCompletions since it's not implemented in the mock.
  • The OpenAIClient implementation has been updated to utilize the OpenAI API's chat completion functionality and transform the response into the corresponding domain models (ChatCompletionChunk, ChatChunk, ChatDelta, etc.).

Example

package com.xebia.functional.xef.auto.streaming

import com.xebia.functional.xef.auto.llm.openai.OpenAI
import com.xebia.functional.xef.auto.llm.openai.OpenAIEmbeddings
import com.xebia.functional.xef.llm.Chat
import com.xebia.functional.xef.vectorstores.LocalVectorStore

suspend fun main() {
  val chat: Chat = OpenAI.DEFAULT_CHAT
  val embeddings = OpenAIEmbeddings(OpenAI.DEFAULT_EMBEDDING)
  val vectorStore = LocalVectorStore(embeddings)
  chat.promptStreaming(
    question = "What is the meaning of life?",
    context = vectorStore
  ).collect {
    print(it)
  }
}

These changes enable developers to perform streaming chat completions and receive responses incrementally, enhancing the real-time interactive chat experience.

raulraja and others added 28 commits June 21, 2023 22:22
# Conflicts:
#	core/src/commonMain/kotlin/com/xebia/functional/xef/llm/openai/OpenAIEmbeddings.kt
# Conflicts:
#	core/src/commonMain/kotlin/com/xebia/functional/xef/auto/CoreAIScope.kt
#	core/src/commonMain/kotlin/com/xebia/functional/xef/llm/models/functions/CFunction.kt
#	core/src/commonMain/kotlin/com/xebia/functional/xef/llm/openai/models.kt
#	kotlin/src/commonMain/kotlin/com/xebia/functional/xef/auto/DeserializerLLMAgent.kt
#	kotlin/src/commonMain/kotlin/com/xebia/functional/xef/auto/serialization/functions/FunctionSchema.kt
#	scala/src/main/scala/com/xebia/functional/xef/scala/auto/package.scala
… and java depends on openai module for defaults. xef core does not depend on open ai
# Conflicts:
#	core/src/commonMain/kotlin/com/xebia/functional/xef/auto/AI.kt
#	core/src/commonMain/kotlin/com/xebia/functional/xef/auto/AIRuntime.kt
#	core/src/commonMain/kotlin/com/xebia/functional/xef/auto/AiDsl.kt
#	core/src/commonMain/kotlin/com/xebia/functional/xef/auto/CoreAIScope.kt
#	core/src/commonMain/kotlin/com/xebia/functional/xef/llm/models/chat/Message.kt
#	core/src/commonMain/kotlin/com/xebia/functional/xef/llm/models/chat/Role.kt
#	core/src/commonMain/kotlin/com/xebia/functional/xef/llm/models/text/CompletionRequest.kt
#	examples/kotlin/src/main/kotlin/com/xebia/functional/xef/auto/CustomRuntime.kt
#	java/src/main/java/com/xebia/functional/xef/java/auto/AIScope.java
#	openai/src/commonMain/kotlin/com/xebia/functional/xef/auto/llm/openai/DeserializerLLMAgent.kt
#	openai/src/commonMain/kotlin/com/xebia/functional/xef/auto/llm/openai/ImageGenerationAgent.kt
#	openai/src/commonMain/kotlin/com/xebia/functional/xef/auto/llm/openai/MockAIClient.kt
#	openai/src/commonMain/kotlin/com/xebia/functional/xef/auto/llm/openai/OpenAIClient.kt
#	openai/src/commonMain/kotlin/com/xebia/functional/xef/auto/llm/openai/OpenAIEmbeddings.kt
#	openai/src/commonMain/kotlin/com/xebia/functional/xef/auto/llm/openai/OpenAIRuntime.kt
#	scala/src/main/scala/com/xebia/functional/xef/scala/auto/package.scala
… local models. Local models can be use in the AI DSL and interleaved with any model.
Base automatically changed from gpt4all-java-bindings to main July 3, 2023 18:55
# Conflicts:
#	core/src/commonMain/kotlin/com/xebia/functional/xef/llm/Chat.kt
#	examples/kotlin/src/main/kotlin/com/xebia/functional/xef/auto/gpt4all/Chat.kt
#	gpt4all-kotlin/src/jvmMain/kotlin/com/xebia/functional/gpt4all/GPT4All.kt
#	openai/src/commonMain/kotlin/com/xebia/functional/xef/auto/llm/openai/OpenAIClient.kt
@raulraja
Copy link
Contributor Author

raulraja commented Jul 4, 2023

@xebia-functional/team-ai

@anamariamv anamariamv requested review from a team, serras and javipacheco and removed request for a team July 6, 2023 09:48
Yawolf
Yawolf previously approved these changes Jul 6, 2023
serras
serras previously approved these changes Jul 6, 2023
@@ -28,6 +29,9 @@ class MockOpenAIClient(
private val chatCompletion: (ChatCompletionRequest) -> ChatCompletionResponse = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we maybe split these methods in a different interface? I see more and more that a client implements the interface only partially.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They've already been split into its own interface in main inside Chat, ChatWithFunctions etc. The remaining place where it implements all of them like this is the MockClient. The AIClient interface in the main is already unused and should be removed. I'll push a commit to this PR to remove it.

@raulraja raulraja dismissed stale reviews from serras and Yawolf via 1e25e4d July 6, 2023 12:07
@raulraja raulraja merged commit 681dfc0 into main Jul 6, 2023
1 check passed
@raulraja raulraja deleted the streaming-responses branch July 6, 2023 12:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants