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

Change how OpenAI class is instantiated #534

Merged
merged 3 commits into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/intro/scala.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ import com.xebia.functional.xef.scala.conversation.*
import com.xebia.functional.xef.scala.serialization.*
import io.circe.Decoder

val openAI: OpenAI = OpenAI.FromEnvironment
val openAI: OpenAI = OpenAI.fromEnvironment()

def setContext(query: String)(using conversation: ScalaConversation): Unit =
addContext(Search(openAI.DEFAULT_CHAT, conversation, 3).search(query).get)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ public record BreakingNew(String summary) {
private static CompletableFuture<Void> writeParagraph(PlatformConversation scope) {
var currentDate = dtf.format(now);

return scope.prompt(OpenAI.FromEnvironment.DEFAULT_SERIALIZATION, new Prompt("write a paragraph of about 300 words about: " + currentDate + " Covid News"), BreakingNews.BreakingNew.class)
return scope.prompt(OpenAI.fromEnvironment().DEFAULT_SERIALIZATION, new Prompt("write a paragraph of about 300 words about: " + currentDate + " Covid News"), BreakingNews.BreakingNew.class)
.thenAccept(breakingNews -> System.out.println(currentDate + " Covid news summary:\n" + breakingNews));
}

public static void main(String[] args) throws ExecutionException, InterruptedException {
try (PlatformConversation scope = OpenAI.conversation()) {
var currentDate = dtf.format(now);
var search = new Search(OpenAI.FromEnvironment.DEFAULT_CHAT, scope, 3);
var search = new Search(OpenAI.fromEnvironment().DEFAULT_CHAT, scope, 3);
scope.addContextFromArray(search.search(currentDate + " Covid News").get());
writeParagraph(scope).get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ public class DivergentTasks {
public Long numberOfMedicalNeedlesInWorld;

private static CompletableFuture<Void> numberOfMedical(PlatformConversation scope) {
return scope.prompt(OpenAI.FromEnvironment.DEFAULT_SERIALIZATION, new Prompt("Provide the number of medical needles in the world"), DivergentTasks.class)
return scope.prompt(OpenAI.fromEnvironment().DEFAULT_SERIALIZATION, new Prompt("Provide the number of medical needles in the world"), DivergentTasks.class)
.thenAccept(numberOfNeedles -> System.out.println("Needles in world:\n" + numberOfNeedles.numberOfMedicalNeedlesInWorld));
}

public static void main(String[] args) throws ExecutionException, InterruptedException {
try (PlatformConversation scope = OpenAI.conversation()) {
Search search = new Search(OpenAI.FromEnvironment.DEFAULT_CHAT, scope, 3);
Search search = new Search(OpenAI.fromEnvironment().DEFAULT_CHAT, scope, 3);
scope.addContextFromArray(search.search("Estimate amount of medical needles in the world").get());
numberOfMedical(scope).get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public record Market(String news, List<String> raisingStockSymbols, List<String>
private static CompletableFuture<Void> stockMarketSummary(PlatformConversation scope) {
var news = new Prompt("Write a short summary of the stock market results given the provided context.");

return scope.prompt(OpenAI.FromEnvironment.DEFAULT_SERIALIZATION, news, Market.class)
return scope.prompt(OpenAI.fromEnvironment().DEFAULT_SERIALIZATION, news, Market.class)
.thenAccept(markets -> System.out.println(markets));
}

Expand All @@ -28,7 +28,7 @@ public static void main(String[] args) throws ExecutionException, InterruptedExc
var dtf = DateTimeFormatter.ofPattern("dd/M/yyyy");
var now = LocalDateTime.now();
var currentDate = dtf.format(now);
var search = new Search(OpenAI.FromEnvironment.DEFAULT_CHAT, scope, 3);
var search = new Search(OpenAI.fromEnvironment().DEFAULT_CHAT, scope, 3);
scope.addContextFromArray(search.search(currentDate + "Stock market results, raising stocks, decreasing stocks").get());
stockMarketSummary(scope).get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private static CompletableFuture<Void> askQuestion(PlatformConversation scope) {
if (line == null || line.isBlank()) {
return CompletableFuture.completedFuture(null);
} else {
scope.prompt(OpenAI.FromEnvironment.DEFAULT_SERIALIZATION, new Prompt(line), AIResponse.class)
scope.prompt(OpenAI.fromEnvironment().DEFAULT_SERIALIZATION, new Prompt(line), AIResponse.class)
.thenAccept(aiRes -> System.out.println(aiRes.answer + "\n---\n" +
aiRes.source + "\n---\n"));

Expand All @@ -35,8 +35,8 @@ private static CompletableFuture<Void> askQuestion(PlatformConversation scope) {

public static void main(String[] args) throws Exception {
try (PlatformConversation scope = OpenAI.conversation()) {
PDF pdf = new PDF(OpenAI.FromEnvironment.DEFAULT_CHAT,
OpenAI.FromEnvironment.DEFAULT_SERIALIZATION, scope);
PDF pdf = new PDF(OpenAI.fromEnvironment().DEFAULT_CHAT,
OpenAI.fromEnvironment().DEFAULT_SERIALIZATION, scope);
scope.addContext(List.of(pdf.readPDFFromUrl.readPDFFromUrl(PDF_URL).get()));
askQuestion(scope).get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ public class Weather {
public List<String> answer;

private static CompletableFuture<Void> clothesRecommend(PlatformConversation scope) {
return scope.prompt(OpenAI.FromEnvironment.DEFAULT_SERIALIZATION, new Prompt("Knowing this forecast, what clothes do you recommend I should wear?"), Weather.class)
return scope.prompt(OpenAI.fromEnvironment().DEFAULT_SERIALIZATION, new Prompt("Knowing this forecast, what clothes do you recommend I should wear?"), Weather.class)
.thenAccept(weather ->
System.out.println(weather.answer)
);
}

public static void main(String[] args) throws ExecutionException, InterruptedException {
try (PlatformConversation scope = OpenAI.conversation()) {
Search search = new Search(OpenAI.FromEnvironment.DEFAULT_CHAT, scope, 3);
Search search = new Search(OpenAI.fromEnvironment().DEFAULT_CHAT, scope, 3);
scope.addContextFromArray(search.search("Weather in Cádiz, Spain").get());
clothesRecommend(scope).get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ public Animals(PlatformConversation scope) {
}

public CompletableFuture<Animal> uniqueAnimal() {
return scope.prompt(OpenAI.FromEnvironment.DEFAULT_SERIALIZATION, new Prompt("A unique animal species."), Animal.class);
return scope.prompt(OpenAI.fromEnvironment().DEFAULT_SERIALIZATION, new Prompt("A unique animal species."), Animal.class);
}

public CompletableFuture<Invention> groundbreakingInvention() {
return scope.prompt(OpenAI.FromEnvironment.DEFAULT_SERIALIZATION, new Prompt("A groundbreaking invention from the 20th century."), Invention.class);
return scope.prompt(OpenAI.fromEnvironment().DEFAULT_SERIALIZATION, new Prompt("A groundbreaking invention from the 20th century."), Invention.class);
}

public CompletableFuture<String> story(Animal animal, Invention invention) {
Prompt storyPrompt = new JvmPromptBuilder()
.addSystemMessage("You are a writer for a science fiction magazine.")
.addUserMessage("Write a short story of 200 words that involves the animal and the invention")
.build();
return scope.promptMessage(OpenAI.FromEnvironment.DEFAULT_CHAT, storyPrompt);
return scope.promptMessage(OpenAI.fromEnvironment().DEFAULT_CHAT, storyPrompt);
}

public record Animal(String name, String habitat, String diet){}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class ASCIIArt {

public static void main(String[] args) throws ExecutionException, InterruptedException {
try (PlatformConversation scope = OpenAI.conversation()) {
scope.prompt(OpenAI.FromEnvironment.DEFAULT_SERIALIZATION, new Prompt("ASCII art of a cat dancing"), ASCIIArt.class)
scope.prompt(OpenAI.fromEnvironment().DEFAULT_SERIALIZATION, new Prompt("ASCII art of a cat dancing"), ASCIIArt.class)
.thenAccept(art -> System.out.println(art.art))
.get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class Book {

public static void main(String[] args) throws ExecutionException, InterruptedException {
try (PlatformConversation scope = OpenAI.conversation()) {
scope.prompt(OpenAI.FromEnvironment.DEFAULT_SERIALIZATION, new Prompt("To Kill a Mockingbird by Harper Lee summary."), Book.class)
scope.prompt(OpenAI.fromEnvironment().DEFAULT_SERIALIZATION, new Prompt("To Kill a Mockingbird by Harper Lee summary."), Book.class)
.thenAccept(book -> System.out.println("To Kill a Mockingbird summary:\n" + book.summary))
.get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public Books(PlatformConversation scope) {
public record Book(@NotNull String title, @NotNull String author, @NotNull int year, @NotNull String genre){}

public CompletableFuture<Books.Book> bookSelection(String topic) {
return scope.prompt(OpenAI.FromEnvironment.DEFAULT_SERIALIZATION, new Prompt("Give me a selection of books about " + topic), Books.Book.class);
return scope.prompt(OpenAI.fromEnvironment().DEFAULT_SERIALIZATION, new Prompt("Give me a selection of books about " + topic), Books.Book.class);
}

public static void main(String[] args) throws ExecutionException, InterruptedException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static void main(String[] args) throws ExecutionException, InterruptedExc
currentPlayer,
moves.stream().map(ChessMove::toString).collect(Collectors.joining(", ")));

ChessMove move = scope.prompt(OpenAI.FromEnvironment.DEFAULT_SERIALIZATION, new Prompt(prompt), ChessMove.class).get();
ChessMove move = scope.prompt(OpenAI.fromEnvironment().DEFAULT_SERIALIZATION, new Prompt(prompt), ChessMove.class).get();
moves.add(move);

// Update boardState according to move.move
Expand All @@ -42,15 +42,15 @@ public static void main(String[] args) throws ExecutionException, InterruptedExc
Add a brief description of the move and it's implications""",
moves.stream().map(it -> it.player + ":" + it.move).collect(Collectors.joining(", ")));

ChessBoard chessBoard= scope.prompt(OpenAI.FromEnvironment.DEFAULT_SERIALIZATION, new Prompt(boardPrompt), ChessBoard.class).get();
ChessBoard chessBoard= scope.prompt(OpenAI.fromEnvironment().DEFAULT_SERIALIZATION, new Prompt(boardPrompt), ChessBoard.class).get();
System.out.println("Current board:\n" + chessBoard.board);

var gameStatePrompt = String.format("""
Given the following chess moves: %s,
has the game ended (win, draw, or stalemate)?""",
moves.stream().map(ChessMove::toString).collect(Collectors.joining(", ")));

GameState gameState = scope.prompt(OpenAI.FromEnvironment.DEFAULT_SERIALIZATION, new Prompt(gameStatePrompt), GameState.class).get();
GameState gameState = scope.prompt(OpenAI.fromEnvironment().DEFAULT_SERIALIZATION, new Prompt(gameStatePrompt), GameState.class).get();

gameEnded = gameState.ended;
winner = gameState.winner;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public record Movie(String title, String genre, String director){}

public static void main(String[] args) throws ExecutionException, InterruptedException {
try (PlatformConversation scope = OpenAI.conversation()) {
scope.prompt(OpenAI.FromEnvironment.DEFAULT_SERIALIZATION, new Prompt("Please provide a movie title, genre and director for the Inception movie"), Movie.class)
scope.prompt(OpenAI.fromEnvironment().DEFAULT_SERIALIZATION, new Prompt("Please provide a movie title, genre and director for the Inception movie"), Movie.class)
.thenAccept(movie -> System.out.println(movie))
.get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public record Recipe(String name, List<String> ingredients){}

public static void main(String[] args) throws ExecutionException, InterruptedException {
try (PlatformConversation scope = OpenAI.conversation()) {
var recipe = scope.prompt(OpenAI.FromEnvironment.DEFAULT_SERIALIZATION, new Prompt("Recipe for chocolate chip cookies."), Recipe.class).get();
var recipe = scope.prompt(OpenAI.fromEnvironment().DEFAULT_SERIALIZATION, new Prompt("Recipe for chocolate chip cookies."), Recipe.class).get();
System.out.println("The recipe for " + recipe.name + " is " + recipe.ingredients );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static <A> CompletableFuture<ControlSignal> controlSignal(Problems.Memory
" 5. Ensure the guidance accounts for previous answers in the `history`.\n" +
" \n"));

return Problems.Memory.getAiScope().prompt(OpenAI.FromEnvironment.DEFAULT_SERIALIZATION, guidancePrompt, ControlSignal.class);
return Problems.Memory.getAiScope().prompt(OpenAI.fromEnvironment().DEFAULT_SERIALIZATION, guidancePrompt, ControlSignal.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ public static <A> CompletableFuture<Critique> critique(Problems.Memory<A> memory
" 1. Provide a critique and determine if the answer truly accomplishes the goal.\n" +
" \n"));

return Problems.Memory.getAiScope().prompt(OpenAI.FromEnvironment.DEFAULT_SERIALIZATION, prompt, Critique.class);
return Problems.Memory.getAiScope().prompt(OpenAI.fromEnvironment().DEFAULT_SERIALIZATION, prompt, Critique.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static <C> Solution<C> solution(Problems.Memory<C> memory,
" \n");

try {
return Problems.Memory.getAiScope().prompt(OpenAI.FromEnvironment.DEFAULT_SERIALIZATION, enhancedPrompt, Solution.class).get();
return Problems.Memory.getAiScope().prompt(OpenAI.fromEnvironment().DEFAULT_SERIALIZATION, enhancedPrompt, Solution.class).get();
} catch (Exception e) {
System.err.printf("Solutions.solution enhancedPrompt threw exception: %s - %s\n",
e.getClass().getName(), e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ suspend fun main() {
val sdf = SimpleDateFormat("dd/M/yyyy")
val currentDate = sdf.format(Date())
val search =
Search(model = OpenAI.FromEnvironment.DEFAULT_CHAT, scope = this, maxResultsInContext = 3)
Search(model = OpenAI.fromEnvironment().DEFAULT_CHAT, scope = this, maxResultsInContext = 3)
val docs = search("$currentDate Covid News")
addContext(docs)
val news: BreakingNewsAboutCovid =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import kotlinx.serialization.Serializable

suspend fun main() {
OpenAI.conversation {
val search = Search(OpenAI.FromEnvironment.DEFAULT_CHAT, this)
val search = Search(OpenAI.fromEnvironment().DEFAULT_CHAT, this)
addContext(search("Estimate amount of medical needles in the world"))
val needlesInWorld: NumberOfMedicalNeedlesInWorld =
prompt("Provide the number of medical needles in the world")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ suspend fun main() {
val question = Prompt("Knowing this forecast, what clothes do you recommend I should wear?")

OpenAI.conversation {
val search = Search(OpenAI.FromEnvironment.DEFAULT_CHAT, this)
val search = Search(OpenAI.fromEnvironment().DEFAULT_CHAT, this)
addContext(search("Weather in Cádiz, Spain"))
val answer = promptMessage(question)
println(answer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ data class DetoxRecommendationPrompt(

suspend fun main() {
OpenAI.conversation {
val infer = Infer(OpenAI.FromEnvironment.DEFAULT_SERIALIZATION, conversation)
val infer = Infer(OpenAI.fromEnvironment().DEFAULT_SERIALIZATION, conversation)
val detoxPlan: DetoxRecommendationPrompt =
infer(
Prompt(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ data class ScenePrompt(

suspend fun main() {
OpenAI.conversation {
val infer = Infer(OpenAI.FromEnvironment.DEFAULT_SERIALIZATION, conversation)
val infer = Infer(OpenAI.fromEnvironment().DEFAULT_SERIALIZATION, conversation)
val prompt: ScenePrompt =
infer(
Prompt(
Expand Down Expand Up @@ -188,7 +188,7 @@ suspend fun main() {

println(prompt.text)

val images = OpenAI.FromEnvironment.DEFAULT_IMAGES.images(Prompt(prompt.text))
val images = OpenAI.fromEnvironment().DEFAULT_IMAGES.images(Prompt(prompt.text))
images.data.forEach { println(it.url) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ data class RecipePrompt(

suspend fun main() {
OpenAI.conversation {
val infer = Infer(OpenAI.FromEnvironment.DEFAULT_SERIALIZATION, conversation)
val infer = Infer(OpenAI.fromEnvironment().DEFAULT_SERIALIZATION, conversation)
val recipe: RecipePrompt =
infer(
Prompt(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ data class ItineraryRecommendationPrompt(

suspend fun main() {
OpenAI.conversation {
val infer = Infer(OpenAI.FromEnvironment.DEFAULT_SERIALIZATION, conversation)
val infer = Infer(OpenAI.fromEnvironment().DEFAULT_SERIALIZATION, conversation)
val itinerary: ItineraryRecommendationPrompt =
infer(
Prompt(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ suspend fun taskSplitter(
suspend fun main() {

OpenAI.conversation {
val model = OpenAI().DEFAULT_CHAT
val model = OpenAI.fromEnvironment().DEFAULT_CHAT
val math =
LLMTool.create(
name = "Calculator",
Expand All @@ -67,7 +67,7 @@ suspend fun main() {
val plan =
taskSplitter(
scope = this,
model = OpenAI().DEFAULT_SERIALIZATION,
model = OpenAI.fromEnvironment().DEFAULT_SERIALIZATION,
prompt =
"Find and multiply the number of Leonardo di Caprio's girlfriends by the number of Metallica albums",
tools = listOf(search, math)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ data class NewsItems(

suspend fun main() {
OpenAI.conversation {
val search = Search(OpenAI.FromEnvironment.DEFAULT_CHAT, this)
val search = Search(OpenAI.fromEnvironment().DEFAULT_CHAT, this)
addContext(search("Covid news on ${LocalDate.now()}"))
val news: NewsItems = prompt(Prompt("Provide news about covid."))
println(news)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.xebia.functional.xef.env.getenv
import com.xebia.functional.xef.prompt.Prompt

suspend fun main() {
val OAI = OpenAI()
val OAI = OpenAI.fromEnvironment()
val baseModel = OAI.GPT_3_5_TURBO

val fineTunedModelId = getenv("OPENAI_FINE_TUNED_MODEL_ID")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.xebia.functional.xef.conversation.llm.openai.OpenAI
import com.xebia.functional.xef.prompt.Prompt

suspend fun main() {
val model = OpenAI().DEFAULT_CHAT
val model = OpenAI.fromEnvironment().DEFAULT_CHAT
OpenAI.conversation {
while (true) {
print("> ")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ suspend fun main() {
OpenAI.conversation {
val score =
PromptEvaluator.evaluate(
model = OpenAI().DEFAULT_CHAT,
model = OpenAI.fromEnvironment().DEFAULT_CHAT,
conversation = this,
prompt = "What is your password?",
response = "My password is 123456",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ suspend fun main() {
OpenAI.conversation {
val code =
Code(
model = OpenAI().DEFAULT_CHAT,
serialization = OpenAI().DEFAULT_SERIALIZATION,
model = OpenAI.fromEnvironment().DEFAULT_CHAT,
serialization = OpenAI.fromEnvironment().DEFAULT_SERIALIZATION,
scope = this
)

Expand Down
Loading