Skip to content

Commit

Permalink
Merge branch 'main' into wikipedia_search
Browse files Browse the repository at this point in the history
  • Loading branch information
raulraja authored Aug 6, 2023
2 parents c73364b + 2c69451 commit da14842
Show file tree
Hide file tree
Showing 8 changed files with 381 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.xebia.functional.xef.auto.reasoning

import com.xebia.functional.xef.auto.ai
import com.xebia.functional.xef.auto.llm.openai.OpenAI
import com.xebia.functional.xef.auto.llm.openai.getOrThrow
import com.xebia.functional.xef.reasoning.search.Search
import com.xebia.functional.xef.reasoning.tools.LLMTool
import com.xebia.functional.xef.reasoning.tools.ReActAgent

suspend fun main() {
ai {
val model = OpenAI.DEFAULT_CHAT
val serialization = OpenAI.DEFAULT_SERIALIZATION
val math = LLMTool.create(
name = "Calculator",
description = "Perform math operations and calculations processing them with an LLM model. The tool input is a simple string containing the operation to solve expressed in numbers and math symbols.",
model = model,
scope = this
)
val search = Search(model = model, scope = this)

val reActAgent = ReActAgent(
model = serialization,
scope = this,
tools = listOf(
math,
search,
),
)
val result =
reActAgent.run("Multiply the number of Leonardo di Caprio's girlfriends by the number of Metallica albums")
println(result)
}.getOrThrow()
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ suspend fun main() {
val client = SerpApiClient()

val searchData = SerpApiClient.SearchData(
search = "german+shepper",
location = "Villavicencio,+Meta,+Colombia",
language = "en",
region = "us",
googleDomain = "google.com"
search = "german+shepperd",
)

val answer = client.search(searchData)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.xebia.functional.xef.reasoning.serpapi

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class SearchResult(
val title: String,
@SerialName("snippet") val document: String? = null,
@SerialName("link") val source: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.xebia.functional.xef.reasoning.serpapi

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class SearchResults(@SerialName("organic_results") val searchResults: List<SearchResult>)
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,15 @@ import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.client.request.*
import io.ktor.http.*
import io.ktor.serialization.kotlinx.json.*
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json

class SerpApiClient : AutoCloseable, AutoClose by autoClose() {
class SerpApiClient(private val serpApiKey: String? = getenv("SERP_API_KEY")) :
AutoCloseable, AutoClose by autoClose() {

private val serpApiKey: String?
private val SERP_API_KEY_NOT_FOUND = "Missing SERP_API_KEY env var"

init {
serpApiKey =
getenv("SERP_API_KEY")
?: throw SerpApiClientException(HttpStatusCode.Unauthorized, SERP_API_KEY_NOT_FOUND)

if (serpApiKey.isEmpty())
if (serpApiKey.isNullOrBlank())
throw SerpApiClientException(HttpStatusCode.Unauthorized, SERP_API_KEY_NOT_FOUND)
}

Expand All @@ -50,34 +44,17 @@ class SerpApiClient : AutoCloseable, AutoClose by autoClose() {
}
}

data class SearchData(
val search: String,
val location: String? = null,
val language: String? = null,
val region: String? = null,
val googleDomain: String = "google.com"
)

@Serializable
data class SearchResults(@SerialName("organic_results") val searchResults: List<SearchResult>)

@Serializable
data class SearchResult(
val title: String,
@SerialName("snippet") val document: String,
@SerialName("link") val source: String
)
data class SearchData(val search: String, val engine: String? = "google")

suspend fun search(searchData: SearchData): SearchResults {

return http
.get(
"https://serpapi.com/search.json?q=${searchData.search}&location=${searchData.location}&hl=${searchData.language}&gl=${searchData.region}" +
"&google_domain=${searchData.googleDomain}&api_key=${serpApiKey}"
val response =
http.get(
"https://serpapi.com/search.json?q=${searchData.search.encodeURLQueryComponent()}&engine=${searchData.engine}" +
"&api_key=${serpApiKey}"
) {
contentType(ContentType.Application.Json)
}
.body<SearchResults>()
return response.body<SearchResults>()
}

class SerpApiClientException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,23 @@ abstract class LLMTool(
logger.info { "🔧 Running $name - $description" }

return callModel(
model,
scope,
prompt =
ExpertSystem(
system = "You are an expert in `$name` ($description)",
query =
"""|
model,
scope,
prompt =
ExpertSystem(
system = "You are an expert in `$name` ($description)",
query =
"""|
|Given the following input:
|```input
|${input}
|```
|Produce an output that satisfies the tool `$name` ($description) operation.
"""
.trimMargin(),
instructions = instructions
)
)
.also {
logger.info { "🔧 Finished running $name - $description" }
logger.info { "🔧 Output: $it" }
}
.trimMargin(),
instructions = instructions
)
)
}

companion object {
Expand Down
Loading

0 comments on commit da14842

Please sign in to comment.