Skip to content

Commit

Permalink
Merge pull request HamaWhiteGG#31 from HamaWhiteGG/dev
Browse files Browse the repository at this point in the history
add langchain-examples
  • Loading branch information
HamaWhiteGG authored Jul 6, 2023
2 parents a57f58c + 5c1964f commit 49872e4
Show file tree
Hide file tree
Showing 39 changed files with 1,149 additions and 264 deletions.
297 changes: 205 additions & 92 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion langchain-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>io.github.hamawhitegg</groupId>
<artifactId>langchain-java</artifactId>
<version>0.1.6</version>
<version>0.1.8</version>
</parent>

<artifactId>langchain-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ public class ChatOpenAI extends BaseChatModel {
protected String openaiProxy;

/**
* Timeout for requests to OpenAI completion API. Default is 10 seconds.
* Timeout for requests to OpenAI completion API. Default is 16 seconds.
*/
@Builder.Default
protected long requestTimeout = 10;
protected long requestTimeout = 16;

/**
* Maximum number of retries to make when generating.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ public class OpenAIEmbeddings implements Embeddings {
private int maxRetries = 6;

/**
* Timeout for requests to OpenAI completion API. Default is 10 seconds.
* Timeout for requests to OpenAI completion API. Default is 16 seconds.
*/
@Builder.Default
protected long requestTimeout = 10;
protected long requestTimeout = 16;

/**
* Validate parameters and init client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public String predict(String text) {

@Override
public String predict(String text, List<String> stop) {
return null;
return call(text, stop);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ public class BaseOpenAI extends BaseLLM {
protected int batchSize = 20;

/**
* Timeout for requests to OpenAI completion API. Default is 10 seconds.
* Timeout for requests to OpenAI completion API. Default is 16 seconds.
*/
@Builder.Default
protected long requestTimeout = 10;
protected long requestTimeout = 16;

/**
* Adjust the probability of specific tokens being generated.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ public class OpenAIChat extends BaseLLM {
private List<Message> prefixMessages = new ArrayList<>();

/**
* Timeout for requests to OpenAI completion API. Default is 10 seconds.
* Timeout for requests to OpenAI completion API. Default is 16 seconds.
*/
@Builder.Default
protected long requestTimeout = 10;
protected long requestTimeout = 16;

/**
* Adjust the probability of specific tokens being generated.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
*/
public class AIMessage extends BaseMessage {

private boolean example;

public AIMessage(String content) {
super(content);
}
Expand All @@ -34,4 +32,12 @@ public AIMessage(String content) {
public String type() {
return "ai";
}

@Override
public String toString() {
return "AIMessage{" +
"content='" + content + '\'' +
", additionalKwargs=" + additionalKwargs +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
*/
public class HumanMessage extends BaseMessage {

private boolean example;

public HumanMessage(String content) {
super(content);
}
Expand All @@ -34,4 +32,12 @@ public HumanMessage(String content) {
public String type() {
return "human";
}

@Override
public String toString() {
return "HumanMessage{" +
"content='" + content + '\'' +
", additionalKwargs=" + additionalKwargs +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,12 @@ public SystemMessage(String content) {
public String type() {
return "system";
}

@Override
public String toString() {
return "SystemMessage{" +
"content='" + content + '\'' +
", additionalKwargs=" + additionalKwargs +
'}';
}
}
144 changes: 0 additions & 144 deletions langchain-core/src/test/java/com/hw/langchain/QuickStart.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class OpenAITest {
void testOpenAICall() {
OpenAI llm = OpenAI.builder()
.maxTokens(16)
.requestTimeout(15)
.requestTimeout(16)
.build()
.init();

Expand Down
59 changes: 59 additions & 0 deletions langchain-examples/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.github.hamawhitegg</groupId>
<artifactId>langchain-java</artifactId>
<version>0.1.8</version>
</parent>

<artifactId>langchain-examples</artifactId>

<dependencies>
<dependency>
<groupId>io.github.hamawhitegg</groupId>
<artifactId>langchain-core</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>

<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hw.langchain.examples.agents;

import com.hw.langchain.agents.agent.types.AgentType;
import com.hw.langchain.chat.models.openai.ChatOpenAI;
import com.hw.langchain.llms.openai.OpenAI;

import java.util.List;

import static com.hw.langchain.agents.initialize.Initialize.initializeAgent;
import static com.hw.langchain.agents.load.tools.LoadTools.loadTools;

/**
* @author HamaWhite
*/
public class ChatAgentExample {

public static void main(String[] args) {
// The language model we're going to use to control the agent.
var chat = ChatOpenAI.builder().temperature(0).build().init();

// The tools we'll give the Agent access to. Note that the 'llm-math' tool uses an LLM, so we need to pass that
// in.
var llm = OpenAI.builder().temperature(0).build().init();
var tools = loadTools(List.of("serpapi", "llm-math"), llm);

// Finally, let's initialize an agent with the tools, the language model, and the type of agent we want to use.
var agent = initializeAgent(tools, chat, AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION);

// Now let's test it out!
agent.run("Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?");
}
}
Loading

0 comments on commit 49872e4

Please sign in to comment.