Scala implicit3/15/2023 ![]() ![]() In particular, you will find bidirectional conversions between the following types. It is quite easy to do this, because Scala offers implicit conversions between all the major collection types in the JavaConverters object. Or you might want to pass one of Scala’s collections to a Java method that expects its Java counterpart. For instance, you might want to access an existing Java collection as if it were a Scala collection. Sometimes you might need to pass from one collection framework to the other. In particular, the Scala libraries put much more emphasis on immutable collections, and provide many more operations that transform a collection into a new one. But there are also important differences. For instance, both libraries know iterators, iterables, sets, maps, and sequences. There are many similarities between the two. Like Scala, Java also has a rich collections library. Info: JavaScript is currently disabled, code tabs will still work, We don’t want to be exhaustive here, since in total 20 functions are provided, but it’s hopefully enough for you get an idea: each function’s signature consists of an input (text or image files) and optional settings.This page has a new version. Here’s my address 2111 Ash Lane, Crestview CA 92002 |Best, |Maya """.stripMargin service.createCompletion( text, settings = CreateCompletionSettings( model = ModelId.text_davinci_001, max_tokens = Some(1500), temperature = Some(0.9), presence_penalty = Some(0.2), frequency_penalty = Some(0.2) ) ).map(completion => println() ) Val text = """Extract the name and mailing address from this email: |Dear Kelly, |It was great to talk to you at the seminar. Create completion with a custom setting.Here’s my address 2111 Ash Lane, Crestview CA 92002 |Best, |Maya """.stripMargin service.createCompletion(text).map(completion => println() ) Service.retrieveModel(ModelId.text_davinci_003).map(model => println(model.getOrElse("N/A")) ) Since all the calls are async they return responses wrapped in Future. Via dependency injection (requires openai-scala-guice lib)Ĭlass M圜lass (openAIService: OpenAIService) įull documentation of each call with its respective inputs and settings is provided in OpenAIService. ![]() Unless the call site explicitly provides arguments for those parameters, Scala will look for implicitly available given (or implicit in Scala 2) values of the correct type. Parameter lists starting with the keyword using (or implicit in Scala 2) mark contextual parameters. A method can have contextual parameters, also called implicit parameters, or more concisely implicits. package object of the type, companion object of the type itself, of its type constructor if any, of its parameters if any, and also of its supertype and supertraits). Contextual Parameters, aka Implicit Parameters. Val service = OpenAIServiceFactory( apiKey = "your_api_key", orgId = Some("your_org_id") // if you have one ) 2) implicit scope, which contains all sort of companion objects and package object that bear some relation to the implicit's type which we search for (i.e. (note that here a config can be also passed through DI, rather than manually created) Val config = ConfigFactory.load("path_to_my_custom_config") val service = OpenAIServiceFactory(config) variable(s) to be set as defined in Config section) Then you can obtain a service in one of the following ways: The simplest way is for instance: implicit val ec = ExecutionContext.global implicit val materializer = Materializer(ActorSystem()) Usage □□īefore you can even think of doing something (as in the most Scala programs) you must define an implicit execution context as well as akka materializer. Alternatively, you can provide your own config file and override the configuration options (including timeouts) as specified in the default config nf. The most important environmental variable that you need to set is OPENAI_SCALA_CLIENT_API_KEY and optionally also OPENAI_SCALA_CLIENT_ORG_ID (if you have one). I find it quite extraordinary and consider myself lucky that Cequence embraced Scala and the functional-programming-mindset in general for its mission to transform Contract Management into automated, effective environment via ML/NLP-powered features such as contract analytics, risk flagging and negotiation assistant. Especially because it was in line with the mission of my company Cequence, where I work as CSO. That kept bugging me to the point that I’ve decided to code a client on my own. Frankly, it worked well, however, it was lacking some idiomatic Scala features (case classes, traits, etc.) and most importantly it was not async. As one can expect, I gave a Java client, which can be integrated to Scala (as any other JVM language), a try. Long story short, when I was browsing the OpenAI API community libs page earlier this year, to see, which languages are supported, I’ve found out, quite surprisingly, that Scala, my preferred language for business logic and integration, was missing. This is my very first post on Medium! Hooray! Coincidentally, it is related to the most hyped thing these days, ChatGPT and the company behind it - OpenAI. ![]()
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |