Skip to content

Commit

Permalink
Merge pull request #59 from buhe/getlocation
Browse files Browse the repository at this point in the history
Tool of get location
  • Loading branch information
buhe authored Nov 8, 2023
2 parents f10221a + 20d754a commit 4da3576
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 29 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
![](https://p.ipic.vip/2qqnzz.png)
# 🐇 LangChain Swift
# 🐇 Langchain Swift
[![Swift](https://github.com/buhe/langchain-swift/actions/workflows/swift.yml/badge.svg)](https://github.com/buhe/langchain-swift/actions/workflows/swift.yml) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) ![Swift Package Manager](https://img.shields.io/badge/SwiftPM-compatible-brightgreen.svg) [![Twitter](https://img.shields.io/badge/[email protected]?style=flat)](http://twitter.com/buhe1986)

🚀 A langchain copy, for ios or macOS apps.
🚀 A Langchain copy, for iOS or macOS apps.


## Setup
Expand Down
2 changes: 1 addition & 1 deletion Sources/LangChain/agents/Agent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public class AgentExecutor: DefaultChain {
}
return (step, observation)
} catch {
// print("\(error) at run \(tool.name()) tool.")
print("\(error.localizedDescription) at run \(tool.name()) tool.")
let observation = try! await InvalidTool(tool_name: tool.name()).run(args: action.input)
return (step, observation)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/LangChain/tools/BaseTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public protocol Tool {

func _run(args: String) async throws -> String
}
public class BaseTool: Tool {
public class BaseTool: NSObject, Tool {
static let TOOL_REQ_ID = "tool_req_id"
static let TOOL_COST_KEY = "cost"
static let TOOL_NAME_KEY = "tool_name"
Expand Down
83 changes: 83 additions & 0 deletions Sources/LangChain/tools/GetLocationTool.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//
// File.swift
//
//
// Created by 顾艳华 on 2023/11/7.
//

import Foundation
import CoreLocation
// !! Add "Privacy - Location When In Use Usage Description" to Info.plist
public class GetLocationTool: BaseTool, CLLocationManagerDelegate {

let locationManager:CLLocationManager = CLLocationManager()
var authorizationStatus: CLAuthorizationStatus?
private var locationContinuation: CheckedContinuation<String, Error>?
public override init(callbacks: [BaseCallbackHandler] = []) {
super.init(callbacks: callbacks)
// callback locationManagerDidChangeAuthorization
locationManager.delegate = self
}
public override func name() -> String {
"GetLocation"
}

public override func description() -> String {
"""
Tool of get current location.
Input must be here.
Returns the current longitude and latitude, such as -78.4:38.5.
"""
}

public override func _run(args: String) async throws -> String {

locationManager.requestLocation()
//wait
return try await withCheckedThrowingContinuation { continuation in
locationContinuation = continuation
}

}

public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let currLocation:CLLocation = locations.last!
let longitude = currLocation.coordinate.longitude
let latitude = currLocation.coordinate.latitude
// signal
locationContinuation?.resume(returning: "\(longitude):\(latitude)")
}

public func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
switch manager.authorizationStatus {
case .authorizedWhenInUse: // Location services are available.
// Insert code here of what should happen when Location services are authorized
// authorizationStatus = .authorizedWhenInUse
// locationManager.requestLocation()
break

case .restricted: // Location services currently unavailable.
// Insert code here of what should happen when Location services are NOT authorized
authorizationStatus = .restricted
break

case .denied: // Location services currently unavailable.
// Insert code here of what should happen when Location services are NOT authorized
authorizationStatus = .denied
break

case .notDetermined: // Authorization not determined yet.
authorizationStatus = .notDetermined
manager.requestWhenInUseAuthorization()
break

default:
break
}
}

public func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("error: \(error.localizedDescription)")
locationContinuation?.resume(throwing: error)
}
}
50 changes: 25 additions & 25 deletions Tests/LangChainTests/langchain_swiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ May God bless you all. May God protect our troops.
func testBilibiliLoader() async throws {
let loader = BilibiliLoader(videoId: "BV1iP411y7Vs")
let doc = await loader.load()
print(doc.first!.metadata["thumbnail"]!)
// print(doc.first!.metadata["thumbnail"]!)
XCTAssertFalse(doc.isEmpty)
XCTAssertNotEqual("", doc.first!.page_content)
}
Expand Down Expand Up @@ -851,8 +851,8 @@ May God bless you all. May God protect our troops.
let plain = String(buffer: try await response.body.collect(upTo: 10240 * 1024))
let loader = HtmlLoader(html: plain, url: url)
let doc = await loader.load()
print("thumbnail: \(doc.first!.metadata["thumbnail"]!)")
print("title: \(doc.first!.metadata["title"]!)")
// print("thumbnail: \(doc.first!.metadata["thumbnail"]!)")
// print("title: \(doc.first!.metadata["title"]!)")

XCTAssertFalse(doc.isEmpty)
XCTAssertNotEqual("", doc.first!.page_content)
Expand Down Expand Up @@ -915,7 +915,7 @@ May God bless you all. May God protect our troops.
try? httpClient.syncShutdown()
}
let result = await BaiduClient.ocrImage(ak: "vjLPbepeMfSIjZyzpuMCufhv", sk: "WAANBg7crEIlozpwPfplPagNzspx49Gy", httpClient: httpClient, image: imageData)
print("ocr: \(result!)")
// print("ocr: \(result!)")
XCTAssertNotNil(result)
XCTAssertNotNil(result!["words_result"])
}
Expand Down Expand Up @@ -971,15 +971,15 @@ May God bless you all. May God protect our troops.
}
let demo = Book(title: 1.1, content: 2.2, isBuy: true,unit: [Unit(num: 1)])
let s = String(data: try! JSONEncoder().encode(demo), encoding: .utf8)!
print("json: \(s)")
// print("json: \(s)")
// let book = Book(title: "a", content: "b")
// let mirror = Mirror(reflecting: book)
// guard let types = getTypesOfProperties(inClass: Book.self) else { return }
var parser = ObjectOutputParser<Book>(demo: demo)
let i = parser.get_format_instructions()
let b = parser.parse(text: s)
print("\(b)")
print("i: \(i)")
// print("\(b)")
// print("i: \(i)")
switch b {
case Parsed.object(let o):
XCTAssertNotNil(o)
Expand All @@ -999,15 +999,15 @@ May God bless you all. May God protect our troops.
}
let demo = Book(title: "a", content: "b", unit: Unit(num: 1))
let s = String(data: try! JSONEncoder().encode(demo), encoding: .utf8)!
print("json: \(s)")
// print("json: \(s)")
// let book = Book(title: "a", content: "b")
// let mirror = Mirror(reflecting: book)
// guard let types = getTypesOfProperties(inClass: Book.self) else { return }
var parser = ObjectOutputParser<Book>(demo: demo)
let i = parser.get_format_instructions()
let b = parser.parse(text: s)
print("\(b)")
print("i: \(i)")
// print("\(b)")
// print("i: \(i)")
switch b {
case Parsed.object(let o):
XCTAssertNotNil(o)
Expand Down Expand Up @@ -1035,8 +1035,8 @@ May God bless you all. May God protect our troops.
let plain = String(buffer: try await response.body.collect(upTo: 10240 * 1024))
let loader = HtmlLoader(html: plain, url: url)
let doc = await loader.load()
print("thumbnail: \(doc.first!.metadata["thumbnail"]!)")
print("title: \(doc.first!.metadata["title"]!)")
// print("thumbnail: \(doc.first!.metadata["thumbnail"]!)")
// print("title: \(doc.first!.metadata["title"]!)")

XCTAssertFalse(doc.isEmpty)
XCTAssertNotEqual("", doc.first!.page_content)
Expand Down Expand Up @@ -1070,7 +1070,7 @@ May God bless you all. May God protect our troops.

let llmResult = await simpleSequentialChain._call(args: "0")

print("llm: \(llmResult.0!.llm_output!)")
// print("llm: \(llmResult.0!.llm_output!)")

XCTAssertEqual("0_A_B", llmResult.0!.llm_output!)
}
Expand All @@ -1096,7 +1096,7 @@ May God bless you all. May God protect our troops.

let result = try await sequentialChain.predict(args: "0")

print("llm: \(result)")
// print("llm: \(result)")
XCTAssertEqual("0_A", result["_A_"]!)
XCTAssertEqual("0_A_B", result["_B_"]!)
}
Expand All @@ -1108,20 +1108,20 @@ May God bless you all. May God protect our troops.
}

let result = await tc._call(args: "HO")
print("llm: \(result.0!.llm_output!)")
// print("llm: \(result.0!.llm_output!)")
XCTAssertEqual("HO_T", result.0!.llm_output!)
}

func testDatetimePrompt() async throws {
let datetimeParse = DateOutputParser()
let prompt = datetimeParse.get_format_instructions()
print(prompt)
// print(prompt)
}

func testDatetimeParse() async throws {
let datetimeParse = DateOutputParser()
let res = datetimeParse.parse(text: "2001 10 13")
print(res)
// print(res)
}

func testLoaderThrow() async throws {
Expand All @@ -1137,49 +1137,49 @@ Action: the action to take, should be one of [%@]
Action Input: the input to the action
"""
let a = p.parse(text: inputString)
print(a)
// print(a)

}
func testWikipediaSearchAPI() async throws {
let client = WikipediaAPIWrapper()
let wikis = try await client.search(query: "abc")
print(wikis)
// print(wikis)
XCTAssertEqual(wikis.count, 3)
}

func testWikipediaFetchPageContentAPI() async throws {
let page = WikipediaPage(title: "American Broadcasting Company", pageid: 62027)
let content = try await page.content()
print(content)
// print(content)
XCTAssertNotEqual(content.count, 0)
}

func testWikipediaSearchLoad() async throws {
let client = WikipediaAPIWrapper()
let docs = try await client.load(query: "abc")
print(docs)
// print(docs)
XCTAssertEqual(docs.count, 3)
}

func testPubmedSearchAPI() async throws {
let client = PubmedAPIWrapper()
let pubmeds = try await client.search(query: "ai")
print(pubmeds)
// print(pubmeds)
XCTAssertEqual(pubmeds.count, 5)
}

func testPubmedFetchPageContentAPI() async throws {
let page = PubmedPage(uid: "37926277", webenv: "MCID_6548a476fc7406607a2fa42d")
let content = try await page.content()
print(content)
print(content.count)
// print(content)
// print(content.count)
XCTAssertNotEqual(content.count, 0)
}

func testPubmedSearchLoad() async throws {
let client = PubmedAPIWrapper()
let docs = try await client.load(query: "ai")
print(docs)
// print(docs)
XCTAssertEqual(docs.count, 5)
}
//
Expand Down

0 comments on commit 4da3576

Please sign in to comment.